Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot mean and sd of dataset per x value using ggplot2

Tags:

r

ggplot2

I have a dataset that looks a little like this:

a <- data.frame(x=rep(c(1,2,3,5,7,10,15,20), 5),
                y=rnorm(40, sd=2) + rep(c(4,3.5,3,2.5,2,1.5,1,0.5), 5))
ggplot(a, aes(x=x,y=y)) + geom_point() +geom_smooth()

graph output

I want the same output as that plot, but instead of smooth curve, I just want to take line segments between the mean/sd values for each set of x values. The graph should look similar to the above graph, but jagged, instead of curved.

I tried this, but it fails, even though the x values aren't unique:

ggplot(a, aes(x=x,y=y)) + geom_point() +stat_smooth(aes(group=x, y=y, x=x))
geom_smooth: Only one unique x value each group.Maybe you want aes(group = 1)?
like image 729
naught101 Avatar asked Aug 20 '12 06:08

naught101


People also ask

How do you find the mean and standard deviation in R?

Calculating an average and standard deviation in R is straightforward. The mean() function calculates the average and the sd() function calculates the standard deviation.

What does GG plot do in R?

ggplot2 is a plotting package that provides helpful commands to create complex plots from data in a data frame. It provides a more programmatic interface for specifying what variables to plot, how they are displayed, and general visual properties.


2 Answers

?stat_summary is what you should look at.

Here is an example

# functions to calculate the upper and lower CI bounds
uci <- function(y,.alpha){mean(y) + qnorm(abs(.alpha)/2) * sd(y)}
lci <- function(y,.alpha){mean(y) - qnorm(abs(.alpha)/2) * sd(y)}
ggplot(a, aes(x=x,y=y))  + stat_summary(fun.y = mean, geom = 'line', colour = 'blue') + 
            stat_summary(fun.y = mean, geom = 'ribbon',fun.ymax = uci, fun.ymin = lci, .alpha = 0.05, alpha = 0.5)

enter image description here

like image 192
mnel Avatar answered Oct 21 '22 01:10

mnel


You can use one of the built-in summary functions mean_sdl. The code is shown below

ggplot(a, aes(x=x,y=y)) + 
 stat_summary(fun.y = 'mean', colour = 'blue', geom = 'line')
 stat_summary(fun.data = 'mean_sdl', geom = 'ribbon', alpha = 0.2)
like image 28
Ramnath Avatar answered Oct 21 '22 01:10

Ramnath