Create Density Curves in R with ggplot2

In R programming language, you can add a kernel density curve using the geom_density() function. Here is a simple example:

library(ggplot2)

# 创建一个数据框
data <- data.frame(x = rnorm(100))

# 创建一个基础的散点图
plot <- ggplot(data, aes(x = x)) + geom_point()

# 添加核密度曲线
plot + geom_density()

In the code above, a scatter plot is first created using the ggplot2 library, and then a kernel density curve is added using the geom_density() function. You can also customize the style of the kernel density curve as needed, such as changing parameters like line color and line type.

bannerAds