adhg
adhg

Reputation: 10893

ggplot for normal distribution - add data to graph

I'm trying add to my plot some data that will facilitate users. My distribution graph comes from this code:

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")

and the result is:

enter image description here

What I'm aiming to do is to add more data to the ggplot2:

  1. the mean
  2. say I have a sample: 80. I'd like to draw a line between the x values straight up intersecting with the graph.
  3. Divide the graph into parts by 2 sigmas (or maybe 3) and add a region (example in the graph below shows 4 regions: unusually low price, great price and forth).

Thanks for any pointers!

desired result: enter image description here

Upvotes: 0

Views: 554

Answers (1)

Konrad
Konrad

Reputation: 18657

You can add various lines to the chart using geom_line, I reckon that it's only a matter of placing lines at different points that you want to highlight on the chart (mean, etc.): 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")

Upvotes: 1

Related Questions