3
votes

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
1
please provide a reproducible example (i.e.the data)Cyrus Mohammadian
@CyrusMohammadian The code above uses standard ggplot2 libraries and packages i.e. economics and presidential. Are you seeing any errors?watchtower
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 stackoverflow.com/questions/5963269/…Cyrus Mohammadian
@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
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 packageCyrus Mohammadian

1 Answers

4
votes
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.