0

I'm trying to shade the background of the plot between points along the x-axis. These points are defined by a variable (x). Here's some data:

mydata <- data.frame(year = (2000:2010), 
    x = c(0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1), 
    y = (0:10))

So I want to have year as the x-axis and have a line connecting y points. I then want the background to be shaded if x=1, and to be normal otherwise. Any thoughts?

EDIT: Bonus points if you can stop if from connecting the two points when x changes.

Alex
  • 1,997
  • 1
  • 15
  • 32
  • The background of what exactly? Can you sketch what the desired plot would look like for this given data? I can't picture what you're trying to describe. – MrFlick Dec 11 '14 at 20:25
  • 4
    I wonder if I'm the only person who gets seriously annoyed when people suggest "bonus points". Just describe the desired result. (I had not trouble imagining the desired plot but you're never going to know if I'm right b/c I'm going to do something else.) – IRTFM Dec 11 '14 at 20:26
  • Sorry if unclear. So I'd like the background of the image to be shaded darker for the years when x=1. So 2003-2006, and 2009 - 2010 should be shaded. For example: [here](http://www.upjohn.org/sites/default/files/May%2013%20Chart%202.PNG) – Alex Dec 11 '14 at 20:28
  • Don't have time to do the full answer, but for anyone who does using `group = c(0, cumsum(abs(diff(x)))` as a group aesthetic for `geom_line` will take care of the "bonus points". – Gregor Thomas Dec 11 '14 at 20:33
  • For the non-bonus points part, I'd be tempted to mark as a duplicate of this: http://stackoverflow.com/q/9178024/903061 – Gregor Thomas Dec 11 '14 at 20:38
  • See also http://stackoverflow.com/q/10542326/903061, http://stackoverflow.com/q/26346228/903061 – Gregor Thomas Dec 11 '14 at 20:41
  • Thanks, @Gregor. However, none of those three draw the box automatically when x=1. They all have to specify the values for which the box gets drawn. My dataset is very large in reality, and this would not be feasible to do. – Alex Dec 11 '14 at 20:57
  • @Alex it's just a transformation of your data... – Gregor Thomas Dec 11 '14 at 22:29

1 Answers1

1

This might not be very elegant, but it does what I understand you want to achieve.

mydata$y <- -5:5

mydata$x <- mydata$x*max(mydata$y)

ggplot(mydata, aes(x=year, y=y)) + geom_line() + geom_rect(aes(xmin=year-.5, xmax=year+.5, ymin=x*min(y), ymax=x*max(y)), alpha=.4)

Note: I edited the y variable to reflect your data characteristics

enter image description here

msoftrain
  • 1,017
  • 8
  • 23
  • Thanks, but this doesn't work for me because my y variable isn't bounded nice and neat like it is in this example. – Alex Dec 11 '14 at 20:52
  • Does the edit: `mydata$x <- mydata$x*max(mydata$y)` help address that issue? – msoftrain Dec 11 '14 at 20:57
  • Unfortunately, no because y can take on negative values. So it shades the positive portion of the graph, but not the negative region. – Alex Dec 11 '14 at 21:02
  • New try: does it work now? (with `geom_rect`)? (gives the same result with your sample data) – msoftrain Dec 11 '14 at 21:11