2

I am working with some National Climatic Data Center data of minimum and maximum temperatures. What I want to do is to put two winters worth of data on top of each other in one ribbon graph . . . whereby the x-axis is the month and day for each set and year is ultimately something like a factor. Below is one year of data. I would want the second year to be on this same graph.

This is one year of data above plotting min temperature to max temperature in a ribbon plot

Here is a set of example code very similar to the real data, though the geom_ribbon function is not yet returning a graph exactly like the one above any help and thoughts are appreciated:

dates <- c(20110101,    20110102,   20110103,   20110104,   20110105,   20110106,   20110107,   20120101,   20120102,   20120103,   20120104,   20120105,   20120106,   20120107)
Tmin <- c(-5,   -4, -2, -10,    -2, -1, 2,  -10,    -11,    -12,    -10,    -5, -3, -1)
Tmax <- c(3,    5,  6,  4, 2, 7,    9,  -1, 0,  1,  2,  1,  4,  8)
winter <- c("ONE","ONE","ONE","ONE","ONE","ONE","ONE","TWO","TWO","TWO","TWO","TWO","TWO","TWO")

df <- data.frame(cbind(dates, Tmin, Tmax, winter))
df$dates <- strptime(df$dates, format="%Y%m%d")
df$Tmax <- as.numeric(df$Tmax)
df$Tmin <- as.numeric(df$Tmin)
str(df)

ggplot(df, aes(x = dates, y =Tmax))+
  geom_ribbon(aes(ymin=Tmax, ymax=Tmin), fill ="darkgrey", color="dark grey")
Jeff
  • 105
  • 1
  • 8

1 Answers1

3

How about two overlapping ribbons with different colors?

library(lubridate)
df$yday <- yday(df$dates)
df$year <- year(df$dates)
ggplot(df, aes(yday)) + geom_ribbon(aes(ymax=Tmax, ymin=Tmin, fill=factor(year)), alpha=.5)
Ben B-L
  • 426
  • 3
  • 4
  • This works really well, except if I want to group by a set winter period (October 1 - May 1) this doesn't quite do that for the data set. – Jeff Mar 24 '16 at 18:39
  • The follow up question would be is there any way to force ggplot and lubridate to set an x-axis that goes from 275 - 120? – Jeff Mar 24 '16 at 18:45
  • Ended up combining Ben B-L's solution with this thread: http://stackoverflow.com/questions/28503262/using-lubridate-and-ggplot2-effectively-for-date-axis By adding a dummy date variable and fill by factor this works pretty well, not elegant and doesn't actually use lubridate, but works. A more elegant solution is always welcome, – Jeff Mar 24 '16 at 19:07