With plot and lines I can make multiple plots overlapped in the same range in R. For example, I can plot the density of a data and then simulate a density distribution in the same plot as follows:plot(density(mtcars$mpg), col = "red")polygon(density(mtcars$mpg), col = "red")x <- seq(0, 50, length=1000)hxn <- dnorm(x,mean=mean(mtcars$mpg), sd = sd(mtcars$mpg))lines(x,hxn, col="green")获取如何使用ggplot执行相同操作(在同一图中同时引入数据mtcars$mpg和模拟(x,hxn)的密度)?我将从How can I do the same (introduce both the density of the data mtcars$mpg and the simulation (x,hxn) in the same plot) with ggplot?I would start bylibrary(ggplot2)ggplot(mtcars,aes(x=mpg))+geom_density(color = "red", fill = "red")+xlim(0,40)但是我不知道如何覆盖x,hxn数据.but then I do not know how to overlay the x,hxn data.谢谢!推荐答案您可以执行以下操作:ggplot(mtcars,aes(x=mpg))+ geom_density(color = "red", fill = "red")+ xlim(0,40) + stat_function(fun = dnorm, args = list(mean = mean(mtcars$mpg), sd = sd(mtcars$mpg))) 这篇关于R中的图+线的ggplot应该如何等效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-12 14:47