[R] plot many dfs in ggplot (and other ggplot questions)

Zach Simpson zpsimpso at gmail.com
Wed Aug 3 19:58:26 CEST 2016


In regards to your first question:

>Hi R users,

>I have a dataframe, with daily precipitation data, is it possible to plot
>annual mean or annual sum values directly? Thanks for your help.

>year   month   day   precip       time
>2010     1          1        0.5     2010-01-01
>2010     1          2        0.8     2010-01-02
>2010     1          3        1.0     2010-01-03
>2010     1          4        0.9     2010-01-04

>fig1 = ggplot()+ geom_path(data=df, aes(x=time, y= precip)

I could not find a nice/simple solution (there may be one out there
though). However I made a quick example using some simple for loops
and ggplot's annotate function. Perhaps someone else here has a more
elegant method.
#create sample dataset
start <- as.POSIXct("2010-01-01")
end <- as.POSIXct("2010-12-31")

dates <- seq.POSIXt(start,end,"day")
rain <- rnorm(365, mean = 3.5, sd =1)#pretend this is Vietnam
df <- data.frame(dates,rain)

movec <- strftime(dates, format="%m") #get vector of months
movec <- as.numeric(movec) #character to numeric
#initialize monthly dataframe
moframe <- data.frame(xmins=as.POSIXct(character()),

for (ii in 1:12){
  moframe[ii,3] <- mean(df[which(movec == ii),2])
  moframe[ii,1] <- min(df[which(movec ==ii),1])
  moframe[ii,2] <- max(df[which(movec ==ii),1])

p <- ggplot(df, aes(x=dates, y=rain))+geom_path()
for (jj in 1:12){
  p <- p + annotate("segment", x = moframe[jj,1], xend =
moframe[jj,2], y=moframe[jj,3], yend=moframe[jj,3])
p #voila!

As to your other questions:

>I have another question. There are several dataframes, each has the same
>columns: time, varA, varB, varC, etc. If I want to plot time ~ varA of each
>dataframe, where different dataframe names use different colors. How to do
>this in ggplot? Thanks for your help.

You can "melt" these dataframes into one frame that identifies each
dataset with some ID variable. Then, when you make your plot, you can
use "colour=ID.var" in the aes() call. The below link is a good


Your last question:

>Another question is, if I want to shade the range between the maximum and
>minimum values for daily or annual values, how to do it? Thanks again.

Check out the following which shows how to use geom_area and geom_ribbon:

Another great ggplot resource is the R graphics cookbook

One quick note, in future posts to the list it may be better to
compile your (related questions) into just one message to avoid
cluttering the list.

Hope this helps,

Zach Simpson

More information about the R-help mailing list