Ewoud Brouwer
Ewoud Brouwer

Reputation: 45

How to change the legend barheight with scale_fill_gradientn() in R to be able to see the lower-end colors in the legend bar?

I'm trying to create a map with ggplot and fill the countries with scale_fill_gradientn, because I want to manually decide the color palette. The data includes number of passengers of the Holland America Line per country in a given time period (WOI). To be clear, I want to fill the countries by number of passengers from that country. The problem is that the majority of the data is very low compared to the maximum. So, if you don't adjust the scale values manually, you don't see any color difference in the countries with a lower number of passengers.

The dataset of the non-na value countries looks like this:

ID Region Number of Passengers geometry
HR Croatia 1 list(list(c(16.59681, 16.85476, 16.87604, 16.95796 [...]
LT Lithuania 3 list(list(c(25.82139, 25.86938, 26.04615, 26.3854, [...]
RO Romania 5 list(list(c(27.39117, 27.44355, 27.47784, 27.55157 [...]
HU Hungary 9 list(list(c(22.12108, 22.15531, 22.24829, 22.36416 [...]
DE Germany 21 list(list(c(8.63593, 9.1131, 9.22352, 9.27178, 9.3 [...]
PL Poland 73 list(list(c(18.95003, 19.35966, 19.63903, 19.6485, [...]
AT Austria 122 list(list(c(15.54245, 15.75363, 15.84752, 16.07489 [...]
F France 158 list(list(c(55.64788, 55.68341, 55.70701, 55.78207 [...]
IT Italy 592 list(list(c(12.47792, 12.69064, 12.73139, 12.80836 [...]
UK UK 2941 list(list(c(-0.24042, -0.22283, -0.1984, -0.19125, [...]
NL Netherlands 35694 list(list(c(6.87491, 6.91836, 6.99302, 7.0784, 7.0 [...]

First I created I shape file to get the geometrics of europe and merged it with the passengers info.

library(eurostat)
Europe_shp <- get_eurostat_geospatial(resolution = 10, 
                                      nuts_level = 0, 
                                      year = 2016)

European_Countries_WOI <- merge(country_passengers_europe_code_2_WOI, Europe_shp, 
                                             all.x = TRUE, all.y = TRUE,
                                by.x = "ID", by.y = "id")

I created a map like this and adjusted the scaling.

vector_colors_custom <- c("#6495ED", "#2B386F", "#6317a9", "#9933cc","#A629C2", "#b20000")

map_WOI_1 <- European_Countries_WOI %>% 
  ggplot(aes(fill = number_of_passengers)) +
  aes(geometry = geometry) +
  geom_sf(size = 0.1, color = "#F3F3F3") +
  scale_fill_gradientn(colours = vector_colors_custom,
                       values = scales::rescale(c(1, 500, 600, 36000)))+
scale_x_continuous(limits = c(-10, 27)) +
  scale_y_continuous(limits = c(33, 70)) +
  labs(
    title = "Travellers from Europe",
    subtitle = "Number of HAL Passengers per Country During Word War I",
    caption = "Data: Stadsarchief Rotterdam",
    fill = "Number of Passengers"
  ) +
  theme_void() +
  theme(
    legend.position = c(1.16, 0.5)
  )
map_WOI_1

enter image description here

However, the bar is not large enough to show the blue colors. How can I change the barheight? I know it is possible with scale_fill_gradient2_tableau. I want something like below (which I did with scale_fill_gradient2_tableau), but with multiple colors and be able to adjust the colors manually. Or just be able to rescale the colors in scale_fill_gradient2_tableau, and choose a palette.

enter image description here

Upvotes: 0

Views: 456

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 173793

The problem is not with the size of the bar, but how you have rescaled the values within scale_fill_gradientn. Perhaps you would be better off with a simple log10 scale, mapping the log of the number of passengers to the fill aesthetic, then specifying the labels appropriately to reconvert the log value back to the original:

European_Countries_WOI %>% 
  ggplot(aes(fill = log10(number_of_passengers))) +
  aes(geometry = geometry) +
  geom_sf(size = 0.1, color = "#F3F3F3") +
  scale_fill_gradientn(colours = vector_colors_custom,
                       labels = ~scales::comma(10^.x)) +
  scale_x_continuous(limits = c(-10, 27)) +
  scale_y_continuous(limits = c(33, 70)) +
  labs(
    title = "Travellers from Europe",
    subtitle = "Number of HAL Passengers per Country During Word War I",
    caption = "Data: Stadsarchief Rotterdam",
    fill = "Number of Passengers"
  ) +
  theme_void() +
  theme(plot.margin = margin(20, 0, 20, 20))

enter image description here

You can increase the bar size too if you want, but this will not affect its limits:

European_Countries_WOI %>% 
  ggplot(aes(fill = log10(number_of_passengers))) +
  aes(geometry = geometry) +
  geom_sf(size = 0.1, color = "#F3F3F3") +
  scale_fill_gradientn(colours = vector_colors_custom,
                       labels = ~scales::comma(10^.x)) +
  scale_x_continuous(limits = c(-10, 27)) +
  scale_y_continuous(limits = c(33, 70)) +
  labs(
    title = "Travellers from Europe",
    subtitle = "Number of HAL Passengers per Country During Word War I",
    caption = "Data: Stadsarchief Rotterdam",
    fill = "Number of Passengers"
  ) +
  theme_void() +
  theme(plot.margin = margin(20, 0, 20, 20),
        legend.key.height = unit(0.15, 'npc'))

enter image description here

Upvotes: 2

Related Questions