Plot levels as a series

I have a dataset that looks like this:

id factor H1 H2 H3 H4 H5 H6 434543 lev3 0.8 0.7 0.7 0.5 0.6 0.8 434544 lev2 0.5 0.7 0.9 0.7 0.7 0.1 434545 lev3 0.7 0.7 0.8 0.5 0.7 0.7 434546 lev2 0.4 0.6 0.5 0.8 0.7 0.2 434547 lev3 0.6 0.7 0.8 0.8 0.8 0.7 434548 lev2 0.7 0.7 0.6 0.7 0.8 0.4 434549 lev2 0.8 0.8 0.8 0.7 0.6 0.5 434550 lev1 0.3 0.3 0.4 0.3 0.4 0.5 434551 lev1 0.0 0.3 0.4 0.3 0.2 0.4 434552 lev3 0.6 0.8 0.8 0.8 0.6 0.7 434553 lev2 0.6 0.8 0.5 0.2 0.5 0.8 

I would like to plot the mean and SD for the level at each point in time (H1 ... H6), but draw a solid line through the tools, not the line art. What would be the best way to do this?

+4
source share
1 answer

Here is one way to do this with the excellent ggplot2 package.

 require(ggplot2) require(reshape) # Load data data = read.table('data.txt', header=T) # Format data data = melt(data, id.vars=c('id', 'factor'), variable_name='time') data$time = as.numeric(gsub('H(.+)', '\\1', data$time)) # Function to summarize y at each x getband <- function(y.in){ ymax = mean(y.in) + sd(y.in) ymin = mean(y.in) - sd(y.in) data.frame(ymax, ymin) } # Plot dev.new(width=5, height=4) qplot(time, value, group=factor, geom='point', color=factor, fill=factor, data=data) + stat_summary(color=0, fun.data=getband, geom='ribbon', alpha=0.2) + stat_summary(fun.y=mean, geom='line') 

enter image description here

+5
source

All Articles