Lactuca
Lactuca

Reputation: 115

Apply scale gradient colour to barplot in R without legend

I am trying to apply a gradient colour to these two barplots that share the same values:

The barplot on the left shows the average unemployment rate over a 1 year period across 9 English regions, while the one on the right shows the unemployment rate change over the same year.

enter image description here

Desired output: a colour gradient low = yellow, high = red to highlight the regions (=bars) with the highest / lowest unemployment rate average and volatility.#

Here's the code I am using:

  unemployment_data %>% 
  tidyr::pivot_longer(-Region) %>% 
  ggplot(aes(x=Region, y=value)) +
  geom_bar(stat='identity') +
  scale_y_continuous(name = "Unemployment Rate (%)") +
  theme(axis.text.x=element_text(angle =- 90, vjust = 0.5)) +
  facet_wrap(~name)

And here's the structure:

        structure(list(
Region = c("SE", "SW", "EASTof", "LDN", "E_Mid", 
  "W_Mid", "Yorks", "NW", "NE"), 
Unemployment.rate.average = c(3.21824018447441, 
  3.05119829228833, 3.44297331188711, 4.7077689256265, 4.23448912295878, 
  4.67588411516106, 4.45220611226308, 4.06868093338517, 5.69591652090698
  ), 
Unemployment.rate.change = c(0.736310637450949, 1.46412090540429, 2.12686434777655, 
  1.45717316978403, 0.637927880295394, 0.332156994549906, -0.0793646423962819, 
  1.1068637830205, 1.51263456155816)), 
class = "data.frame", row.names = c(NA, -9L))

All the examples I found online use the legend as a gradient, but in my case I have two bar plots whose Y values are taken from two different columns of the same data frame - the "Average" column and the "Change" column, and I don't really need a legend in my plot.

Thanks in advance for the help!

Upvotes: 0

Views: 191

Answers (1)

DaveArmstrong
DaveArmstrong

Reputation: 21937

Something like this:

unemployment_data %>% 
  tidyr::pivot_longer(-Region) %>% 
  ggplot(aes(x=Region, y=value, fill=value)) +
  geom_bar(stat='identity', show.legend=FALSE) +
  scale_fill_gradient(low="yellow", high = "red") + 
  scale_y_continuous(name = "Unemployment Rate (%)") +
  theme(axis.text.x=element_text(angle =- 90, vjust = 0.5)) +
  facet_wrap(~name)

enter image description here


EDIT: Different Ramp for Each Panel

In response to the comment about making the ramp different in each panel, there is probably a more elegant way of doing this, but you could do it with two plots:

library(dplyr)
library(ggplot2)
library(gridExtra)

p1 <- unemployment_data %>% 
  mutate(name1 = "Unemployment Rate Average") %>% 
  ggplot(aes(x=Region, 
             y=Unemployment.rate.average, 
             fill=Unemployment.rate.average)) +
  geom_bar(stat='identity', show.legend=FALSE) +
  scale_fill_gradient(low="yellow", high = "red") + 
  labs(y = "Unemployment Rate (%)", x="") +
  theme(axis.text.x=element_text(angle =- 90, vjust = 0.5)) +
  facet_wrap(~name1) + 
  coord_cartesian(ylim=c(0,6), expand=0)

p2 <- unemployment_data %>% 
  mutate(name2 = "Unemployment Rate Change") %>% 
  ggplot(aes(x=Region, 
             y=Unemployment.rate.change, 
             fill=Unemployment.rate.change)) +
  geom_bar(stat='identity', show.legend=FALSE) +
  scale_fill_gradient(low="yellow", high = "red") + 
  labs(x="", y="") + 
  theme(axis.text.x=element_text(angle =- 90, vjust = 0.5), 
        axis.title.y=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank()) +
  facet_wrap(~name2) + 
  coord_cartesian(ylim=c(0,6), expand=0)

grid.arrange(p1, p2, nrow=1, bottom="Region")

enter image description here

Upvotes: 1

Related Questions