Hi: This isn't very difficult if you use a little imagination. We want three separate plots of monthly means by variable with attached error bars. This requires faceting, so we need to create a factor whose levels are the variable names. We also need to generate enough data to summarize by mean and standard deviation per month in order to generate the point/line/error bar plots. Here's one attempt.
# Generate fake data: if you don't have an example, make one up! :) # 20 obs. per month, simulate from normal distribution with a # given vector of monthly means and standard deviations ########## Set up time variable, monthly means and SDs ########## # I'm lazy: I just used the three letter month abbreviations in month.abb() # to use as my 'time' variable Month <- factor(rep(month.abb, each = 20), levels = month.abb) # monthly means mmeans <- c(12, 15, 18, 20, 24, 30, 26, 23, 18, 15, 12, 10) # monthly sd's msd <- rep(c(3, 5, 4, 3), each = 3) ################# Generate data ########################### # Simulate 240 observations for each of the three variables C <- rnorm(240, m = rep(mmeans, each = 20), s = rep(msd, each = 20)) K <- rnorm(240, m = rep(mmeans+2, each = 20), s = rep(msd+1, each = 20)) S <- rnorm(240, m = rep(mmeans-2, each = 20), s = rep(msd+2, each = 20)) # Combine into a data frame d <- data.frame(Month, C, K, S) library(ggplot2) # plyr and reshape get loaded with ggplot2 #### Process data: get monthly means/SDs for each variable ###### # Use ddply to get monthly means and sd's for each variable # (Yes, there are more efficient ways to do this, but there are only three...) md <- ddply(d, .(Month), summarise, C_avg = mean(C), C_stdev = sd(C), K_avg = mean(K), K_stdev = sd(K), S_avg = mean(S), S_stdev = sd(S)) # Melt the data from 'wide' to 'long' - the idea is to stack C, K and S values # and use their names as a factor variable. This is a very useful trick for # faceting or grouping. # grep() is used to select variable names that end in 'avg' or 'stdev'; the $ # sign in a regular expression indicates that action. # Thanks to Kohske Takahashi for the clue to melting multiple groups of variables # in a post on the ggplot2 list. dmelt <- data.frame( melt(md, id = 'Month', measure = c(grep('avg$', names(md)))), sd = melt(md, id = 'Month', measure = c(grep('stdev$', names(md))))$value ) # Some housecleaning: change value to Mean in names and create a new variable that # only uses the variable name (C, K, S) as a factor level, to be used for labeling # the facets. This reduces the amount of ggplot() code we need to write. names(dmelt)[3] <- 'Mean' dmelt$Variable <- substring(dmelt$variable, 1, 1) # Should be straightforward - scale code is used to avoid overlapping labels in a # confined graphics space. g <- ggplot(dmelt, aes(x = Month, y = Mean)) g + geom_point() + geom_line(aes(group = 1)) + geom_errorbar(aes(ymin = Mean - sd, ymax = Mean + sd), width = 0.4) + facet_wrap(~ Variable, nrow = 1) + scale_x_discrete(breaks = levels(Month), labels = substring(month.abb, 1, 1)) # Just for the heck of it, here's a monthly plot of each variable's means (no error bars) as an alternative: g + geom_point(aes(colour = Variable), size = 3) + geom_line(aes(colour = Variable, group = Variable), size = 1) Notice that by limiting the aesthetics in g, I was able to insert additional aesthetics ymin and xmin into geom_errorbar() in the first plot and colour + group in the second plot and still use the same g as a foundation. HTH, Dennis On Thu, Nov 4, 2010 at 7:32 AM, ashz <a...@walla.co.il> wrote: > > Dear Thierry, > > Your solution looks very elgant but I can not find a proper example. > > Can you provide me one? > > Thx > > -- > View this message in context: > http://r.789695.n4.nabble.com/ggplot-output-tp3027026p3027108.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.