4

I am a beginner so I apologize if my question is too basic. It's been about 4 days since I typed my first ggplot2 command. I read How can I apply a gradient fill to a geom_rect object in ggplot2? before posting, but this post seems to be focusing on producing a gradient rect, which I did already.

Objective: I want to highlight which US presidents had unemployment > 10000 (units are not important because my focus is ability to plot graphs.

presidential <- subset(presidential, start > economics$date[1])
ggplot(economics) +
       geom_rect(
         aes(xmin = start, xmax = end, fill = party),
         ymin = -Inf, ymax = Inf, alpha = 0.2,
         data = presidential
       ) +
       geom_vline(
         aes(xintercept = as.numeric(start)),
         data = presidential,
         colour = "grey50", alpha = 0.5
       ) +
       geom_text(
         aes(x = start, y = 2500, label = name),
         data = presidential,
         size = 3, vjust = 0, hjust = 0, nudge_x = 50, check_overlap = TRUE
       ) +
       geom_line(aes(date, unemploy)) +
       geom_rect(
         aes(xmin = start, xmax = end),
         ymin = 10000, ymax = Inf, alpha = 0.4, fill = "chartreuse",
         data = presidential
       ) +
       geom_text(
         aes(x = as.Date("1993-01-20"), y = 12000, label = "High unemployment"),
         size = 3, vjust = 0, hjust = 0, color = "forestgreen"
       )+
       scale_fill_manual(values = c("blue", "red")) 

The graph output is:

Graph

As we can see I was able to create a label for the green rectangle that shows unemployment > 10000. However, I am unhappy with this approach because this is just a quick fix (i.e. I got it to work by adjusting x, y, nudges etc. parameters). What if the scale of axis changes? The text will get distorted or possibly be not visible. I have two questions:

Question 1: Is there anyway we can programmatically label the green rectangle such that unemployment > 10000? I am not too concerned whether we use text, label or a legend. I am assuming that using geom_text() would require quick-fix (i.e. a lot of runs and re-runs to ensure that text appears correctly by constantly adjusting x, y, vjust, hjust and nudges) but a setting legend or label might be automatic.

Question 2 This is a conceptual question--when I call scale_fill_manual(), how would ggplot2 know whether I want red and blue colors for vertical rectangle or horizontal rectangle? I'm curious. Why isn't it asking me to provide colors also for horizontal and vertical rectangles? Is it that I have already provided a constant color for horizontal rectangle using color = forestgreen so it only needs color for the remaining vertical rectangle pairs i.e. red and blue?

I am a beginner so I am sorry if my question is too basic for some of you. I'd appreciate any help.


Update:

Here's the dput of the data:

structure(list(name = c("Nixon", "Ford", "Carter", "Reagan", 
"Bush", "Clinton", "Bush", "Obama"), start = structure(c(-346L, 
1681L, 2576L, 4037L, 6959L, 8420L, 11342L, 14264L), class = "Date"), 
    end = structure(c(1681L, 2576L, 4037L, 6959L, 8420L, 11342L, 
    14264L, 17186L), class = "Date"), party = c("Republican", 
    "Republican", "Democratic", "Republican", "Republican", "Democratic", 
    "Republican", "Democratic")), .Names = c("name", "start", 
"end", "party"), row.names = c(NA, -8L), class = c("tbl_df", 
"tbl", "data.frame"))

Economics data is publicly available with ggplot2 package

 head(economics)
# A tibble: 6 x 6
        date   pce    pop psavert uempmed unemploy
      <date> <dbl>  <int>   <dbl>   <dbl>    <int>
1 1967-07-01 507.4 198712    12.5     4.5     2944
2 1967-08-01 510.5 198911    12.5     4.7     2945
3 1967-09-01 516.3 199113    11.7     4.6     2958
4 1967-10-01 512.9 199311    12.5     4.9     3143
5 1967-11-01 518.1 199498    12.5     4.7     3066
6 1967-12-01 525.8 199657    12.1     4.8     3018
watchtower
  • 4,140
  • 14
  • 50
  • 92
  • please provide a reproducible example (i.e.the data) – Cyrus Mohammadian Aug 27 '16 at 06:04
  • @CyrusMohammadian The code above uses standard ggplot2 libraries and packages i.e. economics and presidential. Are you seeing any errors? – watchtower Aug 27 '16 at 06:10
  • Your question isn't about an error. I know you're using ``ggplot2``, what I want is your original data so I can help address the questions you have. See this guide for posting questions on SO http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Cyrus Mohammadian Aug 27 '16 at 06:24
  • @CyrusMohammadian I cleared the memory and ran the code posted above. I was able to replicate the graphs. I am not sure where the disconnect is, so I have posted `dput()` of my data and `head(economics)` – watchtower Aug 27 '16 at 06:30
  • yeah that wasn't clear to me, you should delete the dput data bc its only the head (its not your actual data). Just mention its data loaded from that package – Cyrus Mohammadian Aug 27 '16 at 06:56

1 Answers1

5
presidential <- subset(presidential, start > economics$date[1])
ggplot(economics) +
       geom_rect(
         aes(xmin = start, xmax = end, fill = party),
         ymin = -Inf, ymax = Inf, alpha = 0.2,
         data = presidential
       ) +
       geom_vline(
         aes(xintercept = as.numeric(start)),
         data = presidential,
         colour = "grey50", alpha = 0.5
       ) +
       geom_text(
         aes(x = start, y = 2500, label = name),
         data = presidential,
         size = 3, vjust = 0, hjust = 0, nudge_x = 50, check_overlap = TRUE
       ) +
       geom_line(aes(date, unemploy)) +
       geom_rect(
         aes(xmin = start, xmax = end, fill = "chartreuse"),
         ymin = 10000, ymax = Inf, alpha = 0.4,
         data = presidential
       ) +
       geom_text(
         aes(x = as.Date("1993-01-20"), y = 12000, label = "High unemployment"),
         size = 3, vjust = 0, hjust = 0, color = "forestgreen"
       )+
       scale_fill_manual(values = c("chartreuse", "red", "blue"), labels=c("High Unemployment","Democrat","Republican"))+labs(fill="")

enter image description here

As far as your second question goes, the color is mapped in a vertical direction because your initial call...

geom_rect(
         aes(xmin = start, xmax = end, fill = party),
         ymin = -Inf, ymax = Inf, alpha = 0.2,
         data = presidential
       ) 

...identifies the start of the rectangle at the start date and its end at the end date but for the y-axis the dimensions are set to Inf thus the color is mapped vertically.

Cyrus Mohammadian
  • 4,982
  • 6
  • 33
  • 62
  • Terrific response! I didn't quite get the second part of the question. I understand the limits about `geom_rect()`. I think my question is that how does `ggplot2` distribute colors between the "High unemployment" rectangle and "Democrat or republican" rectangle? Essentially, how do I know the order in which ggplot2 assigns colors? i.e. `scale_fill_manual(values = c("chartreuse", "red", "blue")` could be assigned to {unempl, dem, rep} OR {dem, rep, unempl} OR {rep, dem, unempl} etc (i.e. any one of the six combinations). Could you please help me? – watchtower Aug 27 '16 at 07:32
  • you set them using the ``values`` and ``labels`` options in the ``scale_fill_manual()`` call – Cyrus Mohammadian Aug 27 '16 at 07:37
  • Ok. If you change the last line to `scale_fill_manual(values = c("blue", "red","chartreuse"), labels=c("Democrat","Republican","High Unemployment"))+labs(fill="")` in your code, then you will see that the color assignment is distorted. i.e. Blue color goes where chartreuse should have been. Ditto with Chartreuse color. Any thoughts? – watchtower Aug 27 '16 at 07:42