简体   繁体   中英

Labeling a geom_rect() in ggplot2

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:

图形

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 (ie 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 (ie 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 ie 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
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="")

在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM