gpt4 book ai didi

r - 如何在 gplot 中添加样本垂直线?

转载 作者:行者123 更新时间:2023-12-01 22:29:40 25 4
gpt4 key购买 nike

考虑这段代码给出的下图:

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)

结果图如下所示: enter image description here

我的目标是向图中添加另一条蓝线:

  1. 与曲线相交
  2. 在X轴上显示值
  3. 在上面添加标题

可视化它,它应该是这样的: enter image description here

我知道如何添加 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")

我将把标题留在顶部作为“读者的练习”,它应该非常简单,基于已有的部分。

关于r - 如何在 gplot 中添加样本垂直线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30087087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com