Geom_rect not displaying colors I want

I am trying to use geom_rect() with dates on the x-axis to show which US political party held the presidency. The default assigns Red to the Democrats and Blue to the Republicans which is the opposite of what I want. I've tried several approaches and no luck

Here is a glimpse of the data. I'm using party, observation_date, and end_date to build the rectangles.

> glimpse(presidential_party)
Observations: 20
Variables: 4
$ president        <chr> "Roosevelt", "Taft", "Wilson", "Harding", "Coolidge", "Hoover", "FDR", "Truman", "Eisenh…
$ party            <fct> Republican, Republican, Democratic, Republican, Republican, Republican, Democratic, Demo…
$ observation_date <date> 1901-09-01, 1909-03-01, 1913-03-01, 1921-03-01, 1923-08-01, 1929-03-01, 1933-03-01, 194…
$ end_date         <date> 1909-03-01, 1913-03-01, 1921-03-01, 1923-08-01, 1929-03-01, 1933-03-01, 1945-04-01, 195…

This is the code that I have

ggplot(deficit, aes(x=observation_date)) +
   geom_line(aes(y = summ_surplus_deficit)) +
   geom_rect(data = presidential_party, 
             aes(xmin=observation_date, xmax = end_date, 
                 ymin = -Inf, ymax = Inf, 
                 fill = party, alpha = .10)) +
   scale_x_date(date_breaks = "10 years", date_labels = "%Y", 
                limits = c(min(deficit$observation_date), max(deficit$observation_date)))
  

How do I tell fill to use Blue for Dems and Red for Reps

I also tried this with similar results:

pal <- brewer.pal(3, "Set1")

repub <- presidential_party %>%
  filter(party == "Republican")
dem <- presidential_party %>%
  filter(party == "Democratic")

ggplot(deficit, aes(x=observation_date)) +
  geom_line(aes(x=observation_date, y = summ_surplus_deficit)) +
  geom_rect(data = repub, 
            aes(xmin=observation_date, xmax = end_date, 
                ymin = -Inf, ymax = Inf, 
                fill = pal[1], alpha = .10)) +
  geom_rect(data = dem, 
            aes(xmin=observation_date, xmax = end_date, 
                ymin = -Inf, ymax = Inf, 
                fill = pal[2], alpha = .10)) +
  scale_x_date(date_breaks = "10 years", date_labels = "%Y", 
               limits = c(min(deficit$observation_date), max(deficit$observation_date)))

Strange thing is that I remember there being some code in the original ggplot2 book that did exactly this but I lent it out and never got it back.

Thanks in advance.

1 Like

There are two simple ways of dealing with this:

  1. Redefine the factor levels (by default it's alphabetical, so D before R)
  2. Use scale_fill_manual()
1 Like

This isn't so bad, you just need to define the colours. By chance the defaults are red and blue.
As said above use scale_fill_manual(),
e.g. scale_fill_manual(values = c("firebrick", "blue3")
Play around until the order is the correct one.
Alternatively you can use a named vector, so the colours are always assigned to a certain condition, regardless their sorting.
scale_fill_manual(values = c("Republican" = "firebrick",
"Democratic" = "blue3"))

Additionally you should put the "alpha = .10" outside of the aes, so it doesn't appear in the legend.

2 Likes

Worked perfectly. I swear I tried that before but I'll take it any way I can get it.

1 Like

still yoy should remove the alpha = .10" outside of the aes.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.