4

New to R, new to stackoverflow, so forgive me....

I'm trying to do a timeseries plot in R using ggplot2. I want to show two line graphs which are filled below their value for a given date. I've been trying to do this with the geom_area(position="identity") function.

However, only one color shows up on my graph (though both show in the legend). I started by melting my data using melt() and am now working with three columns (X=time, variable=groundwater well, value=groundwater elevation). Below is a simplified version of my code, and a screenshot at what I get.

Bank01MWtest<-data.frame(X=(c(1,2,2,1)),variable=(c("MW-01A","MW-01A","MW-01B","MW-01B")),value=(c(576,571,584,580)))

ggplot(data=Bank01MWtest, aes(x=X, y=value,group=variable))+geom_area(position="identity", aes(fill=variable))+geom_line(aes(color=variable))+coord_cartesian(ylim=c(570,590))

I want to show two colors. One color below MW.01A line and one below MW.01B line.

Elevation Data

Updated to be reproducible

Any help?

Uwe
  • 41,420
  • 11
  • 90
  • 134
BenO
  • 43
  • 4
  • 2
    You should provide a minimal [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data so it's more clear exactly what's going on. If we can copy/paste the code into R to test it, it will be easier to help you. – MrFlick Oct 11 '16 at 20:28
  • 3
    You have both colors, but when you put one transparent color on top of another they combine into a different color. Remove the alpha to see. If needed to see both groups, you can reverse which variable is drawn first using `fill = rev(variable)`. – aosmith Oct 11 '16 at 20:51
  • Sorry. I updated the OP. A new image has also been updated. – BenO Oct 11 '16 at 22:38

2 Answers2

3

Try this with geom_area, with some synthetically generated Bank01MWtest dataset:

head(Bank01MWtest)
    Time variable    value
1 2016-07-01   MW-01A 582.5482
2 2016-07-02   MW-01A 580.5652
3 2016-07-03   MW-01A 582.3305
4 2016-07-04   MW-01A 583.3122
5 2016-07-05   MW-01A 576.3432
6 2016-07-06   MW-01A 584.4086

tail(Bank01MWtest)
        Time variable    value
195 2016-10-03   MW-01B 573.8355
196 2016-10-04   MW-01B 575.3218
197 2016-10-05   MW-01B 570.8007
198 2016-10-06   MW-01B 572.3415
199 2016-10-07   MW-01B 575.3291
200 2016-10-08   MW-01B 578.0055

ggplot(data=Bank01MWtest, aes(x=Time, y=value,group=variable))+
  geom_area(position='identity', aes(fill=variable), alpha=0.2)+
  scale_x_date(date_breaks= "1 month", date_minor_breaks = "15 days", date_labels = "%b", 
               limits = c(min(Bank01MWtest$Time),max(Bank01MWtest$Time))) +
  geom_line(aes(color=variable))+coord_cartesian(ylim=c(570,590))

enter image description here

Sandipan Dey
  • 21,482
  • 2
  • 51
  • 63
0

I believe geom_area is being replaced by geom_ribbon in ggplot2, so I'll use the latter in my solution. You'll also need to restructure your data from long to wide for this solution, giving each of the legend categories their own column. I'll do this with the dcast function within the reshape2 package.

The idea here is to add layers with different ymax variables, assign legend labels with the fill option, and then add a legend with colors using the scale_fill_manual function.

library(ggplot2)
library(reshape2)

Bank01MWtest<-data.frame(X=sample(c(1,1,2,2)),
                     variable=sample(c("MW01A","MW01A","MW01B","MW01B")),
                     value=sample(c(576,571,584,580)))

### Note above I modified your category labels by getting rid of the "-" sign 
### so that they can be used as variable names below.

dat = dcast(Bank01MWtest, X~variable)

ggplot(data=dat, aes(x=X)) + 
  geom_ribbon(aes(ymin=0, ymax=MW01A, fill="MW01A")) + 
  geom_ribbon(aes(ymin=0, ymax=MW01B, fill="MW01B")) +
  scale_fill_manual("", values=c("green", "blue")) +
  coord_cartesian(ylim=c(570,590))
rocket1906
  • 83
  • 7
  • This is perfect, thank you. I had been so fixated on using the geom_area to get the plot, I didn't even think about transforming the data back to use geom_ribbon. Still not sure why geom_area can't work though, and it will keep driving me nuts! Even when I reverse the variables (as suggested above) the color switches but you still can't see both sets. – BenO Oct 12 '16 at 04:07