Home > other >  ggplot: No legend when using scale_fill_brewer with geom_contour_filled
ggplot: No legend when using scale_fill_brewer with geom_contour_filled

Time:10-12

I've plotted a specific set of meteorological data using ggplot as described in the R code below. However, when I use scale_fill_brewer to specific the fill color, a legend does not appear.

What changes are necessary for the legend to appear?

library(tidyverse)
library(lubridate)
library(ggplot2)
library(RColorBrewer)


qurl <- "https://www.geo.fu-berlin.de/met/ag/strat/produkte/qbo/singapore.dat"

sing <- read_table(qurl, skip=4)

# the data file adds a 100mb data row starting in 1997 increasing the number of rows per year from 
# 14 to 15.   So, one calcuation must be applied to rnum <140 and a different to rnum >140.

sing2 <- sing %>% separate(1,into=c('hpa','JAN'),sep='\\s ') %>%  drop_na() %>% 
  subset(hpa != 'hPa') %>%  
  mutate(rnum = row_number(),
         hpa=as.integer(hpa)) %>% 
  mutate(year = case_when(rnum <=140 ~ 1987   floor(rnum/14),  # the last year with 14 rows of data
                          rnum >=141 ~ 1987   floor(rnum 10/15))) %>%  # the first year with 15 rows of data
  relocate(year, .before='hpa') %>% arrange(year,hpa) %>%  
  pivot_longer(cols=3:14, names_to='month',values_to='qbo') %>%  
  mutate(date=ymd(paste0(year,'-',month,'-15')),
         hpa=as.integer(hpa),
         qbo=as.numeric(qbo))

sing2 <- sing %>% separate(1,into=c('hpa','JAN'),sep='\\s ') %>%  drop_na() %>% 
         subset(hpa != 'hPa') %>%  
         mutate(year=1987 floor(row_number()/15),
                hpa=as.integer(hpa)) %>%  
         relocate(year, .before='hpa') %>% arrange(year,hpa) %>%  
         pivot_longer(cols=2:13, names_to='month',values_to='qbo') %>%  
         mutate(date=ymd(paste0(year,'-',month,'-15')),
                hpa=as.integer(hpa),
                qbo=as.numeric(qbo))
# End Data Massaging.   It's ready to be graphed
# A simple call to ggplot with geom_contour_filled generates a legend
sing2 %>% 
  ggplot(aes(x=date,y=hpa))   
  geom_contour_filled(aes(z=qbo*0.1))  
  scale_y_reverse()

# Adding scale_fill_brewer removes the legend. 
# Adding show.legend = TRUE to the geom_countour_filled options has no effect. 

limits = c(-1,1)*max(abs(sing2$qbo),na.rm=TRUE)
zCuts <-  round(seq(limits[1], limits[2], length.out = 11), digits=0)

sing2 %>% 
  ggplot()  
  geom_contour_filled(aes(x=date,y=hpa, z = qbo*0.1),breaks=zCuts*0.1)  
  scale_y_reverse(expand=c(0,0))  
  scale_x_date(expand=c(0,0), date_breaks = '1 year', date_labels = '%Y')   
  scale_fill_brewer(palette = 5,type='div',breaks=zCuts)  
  theme_bw()   
  theme(legend.position = 'right',
        axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

enter image description here

CodePudding user response:

OP, I don't have a direct answer for you, given that your example is not able to be replicated (unable to access the data you gave). In place, I can give you a bit of advice on debugging, since it seems the issue is related to the breaks= argument of scale_fill_brewer(). As you mention, you get a legend when using geom_contour_filled(), but not when you add the scale_fill_brewer() part.

Let me use the example from the enter image description here

If you do the same thing, but add in a "nonsensical" breaks argument, you get the same plot, but without a legend (like you are seeing):

v   geom_contour_filled()  
    scale_fill_brewer(palette = 5, type='div', breaks=1:4)

enter image description here

For me, this is good evidence that the issue in your code relates to the value for breaks= not being within the range expected. Is this just a typo? Note that breaks=zCuts in scale_fill_brewer(), yet breaks=zCuts*0.1 in geom_contour_filled(). This would put each value for your color scale to be 10 times outside the range of the breaks for the contours themselves. I'd be willing to bet that this change to that scale_fill_brewer() line will do the trick:

# earlier plot code
...  
scale_fill_brewer(palette = 5,type='div',breaks=zCuts*0.1)  
... 
# remaining plot code
  • Related