adh*_*dhg 1 r graph normal-distribution ggplot2 gplots
考虑此代码给出的下图:
require(ggplot2)
my_data<-c(70, 71, 75, 78, 78, 79, 80, 81, 84, 85, 87, 87, 90, 91, 95, 95, 96, 96, 97, 98, 98, 100, 101, 102, 102, 102, 102, 104, 104, 104, 107, 107, 109, 110, 110, 110, 111, 112, 113, 113, 114, 115, 118, 118, 118, 120, 124, 131, 137, 137, 139, 145, 158, 160, 162, 165, 169, 177, 179, 180)
qplot(my_data,dist, geom="line")+xlab("x values")+ylab("Density")+
geom_point()+
ggtitle("cool graph Distribution") +
geom_line(color="black", size=0.1) +
geom_line(stat = "vline", xintercept = "mean", colour = "red", size=1.1)
Run Code Online (Sandbox Code Playgroud)
结果图如下所示:

我的目标是在图中添加另一条蓝线:
可视化它,它应该是这样的:

我知道如何添加 geom_line 但它是自下而上的,我希望与曲线有“一点交点”。
这真的或多或少是一样的。我更喜欢annotate用于这样的事情。(我个人也会用注释做你的红线。)只需计算你想要的东西去哪里并将它们添加到图中:
qplot(my_data,dist, geom="line")+xlab("x values")+ylab("Density")+
geom_point()+
ggtitle("cool graph Distribution") +
geom_line(color="black", size=0.1) +
geom_line(stat = "vline", xintercept = "mean", colour = "red", size=1.1) +
annotate(geom = "segment", x = 98, xend = 98, y = 0,
yend = dnorm(98, mean = mean(my_data), sd = sd(my_data)),
color = "blue") +
annotate(geom = "text", x = 98, y = -.02 * max(dist), label = "98")
Run Code Online (Sandbox Code Playgroud)
我将标题留在顶部作为“读者练习”,根据已经存在的部分,它应该非常简单。