用于正态分布的ggplot - 将数据添加到图表中

时间:2015-04-15 19:03:03

标签: r normal-distribution data-visualization ggplot2

我正在尝试为我的情节添加一些便于用户使用的数据。我的分发图来自此代码:

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)    
dist <- dnorm(my_data,mean=mean(my_data), sd(my_data))
qplot(my_data,dist,geom="line")+xlab("x values")+ylab("Density")+ ggtitle("cool graph Distribution") + geom_line(color="black")

结果是:

enter image description here

我的目标是向ggplot2添加更多数据:

  1. 平均值
  2. 说我有一个样本:80。我想在与图形相交的x值之间绘制一条直线。
  3. 将图表分成2个sigmas(或者3个)并添加一个区域(下图中的示例显示了4个区域:价格异常低,价格高且价格便宜)。
  4. 感谢您的任何指示!

    期望的结果: enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用geom_line向图表添加各种线条,我认为只需将线条放在要在图表上突出显示的不同点(均值等): enter image description here

qplot(my_data,dist,geom="line") + 
    xlab("x values") + 
    ylab("Density") + 
    ggtitle("cool graph Distribution") + 
    geom_line(color="black") +
    geom_line(stat = "hline", yintercept = "mean", colour = "blue") +
    geom_line(stat = "vline", xintercept = "mean", colour = "red")
相关问题