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

标签 r graph ggplot2 normal-distribution 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)

结果图如下所示: 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/

相关文章:

r - 为什么我从图中得到了错误的邻居

javascript - 在javascript中,测试深度嵌套在对象图中的属性?

r - 拼凑而成 'ncol' 错误 : Theme element `ncol` is not defined in the element hierarchy

css - ShinyDashboard-使用CSS缩放整个浏览器

r - ggplot GLM 拟合曲线,无交互作用

jquery - 使用js函数或jQ插件创建钟形图

data-visualization - 深度剖析可视化

r - ggplot找不到在函数内部创建的对象

r - 使用 lapply 将函数应用于 tibble 中的每一行

用 Tidyverse 替换 R 中的子集