How to remove the title for sub-panels and use fewer bins for the color bar

Hi all,
I'm working with the figures and have questions. The data and code are provided below. In the resulting figure, how to remove the title ('Jan', 'Feb', 'Mar', etc) for each sub-panel but keep their orders? Because the titles take too much space and are not very clear, thus I want to edit it stand-alone. Second, how to use fewer bins for the color bar and throughout the sub-panels? For example, 0.00-0.50 uses one color, 0.50-1.00 uses a second color, and values >1.00 uses a third color. This will make the values in the panels and colors easier to recognize. Thanks for your help.

library(tidyverse)
library(ggplot2)

df1 = data.frame( lon= c(59.75, 60.25, 60.75, 61.25, 61.75, 62.25 ,62.75, 63.25, 44.25, 44.75, 45.25),
                  lat = c(26.25, 26.25, 26.25, 26.25, 26.25, 26.25, 26.25, 26.25, 26.75, 26.75, 26.75),
                  P = c(0.1, 0.1, 0.3, 0.7, 0.6, 0.4, 0.02, NA, NA, NA, NA) )

df2 = data.frame( lon= c(59.75, 60.25, 60.75, 61.25, 61.75, 62.25 ,62.75, 63.25, 44.25, 44.75, 45.25),
                  lat = c(26.25, 26.25, 26.25, 26.25, 26.25, 26.25, 26.25, 26.25, 26.75, 26.75, 26.75),
                  P= c(0, 0.1, 0.5, 1.1, 0.9, 0.5, 0.1, NA, NA, NA, NA) )

df3= data.frame( lon= c(59.75, 60.25, 60.75, 61.25, 61.75, 62.25 ,62.75, 63.25, 44.25, 44.75, 45.25),
                 lat = c(26.25, 26.25, 26.25, 26.25, 26.25, 26.25, 26.25, 26.25, 26.75, 26.75, 26.75),
                 P= c(0.3, 0.3, 0.5, 0.8, 0.9, 0.6, 0.4, NA, NA, NA, NA))

df4= data.frame( lon =c(59.75, 60.25, 60.75, 61.25, 61.75, 62.25 ,62.75, 63.25, 44.25, 44.75, 45.25),
                 lat= c(26.25, 26.25, 26.25, 26.25, 26.25, 26.25, 26.25, 26.25, 26.75, 26.75, 26.75),
                 P= c(0, 0, 0, 0.1, 0.4, 0.6, 0.3, NA, NA, NA, NA))

df5= data.frame( lon =c(59.75, 60.25, 60.75, 61.25, 61.75, 62.25 ,62.75, 63.25, 44.25, 44.75, 45.25),
                 lat= c(26.25, 26.25, 26.25, 26.25, 26.25, 26.25, 26.25, 26.25, 26.75, 26.75, 26.75),
                 P= c(0, 0, 0, 0, 0, 0.7, 1.1, NA, NA, NA, NA))

df6 = data.frame( lon =c(59.75, 60.25, 60.75, 61.25, 61.75, 62.25 ,62.75, 63.25, 44.25, 44.75, 45.25),
                  lat= c(26.25, 26.25, 26.25, 26.25, 26.25, 26.25, 26.25, 26.25, 26.75, 26.75, 26.75),
                  P= c(0, 0, 0.1, 0, 0, 0, 1.3, NA, NA, NA, NA))

df7= data.frame( lon =c(59.75, 60.25, 60.75, 61.25, 61.75, 62.25 ,62.75, 63.25, 44.25, 44.75, 45.25),
                 lat= c(26.25, 26.25, 26.25, 26.25, 26.25, 26.25, 26.25, 26.25, 26.75, 26.75, 26.75),
                 P= c(0, 0, 0.5, 0.5, 0, 0, 1, NA, NA, NA, NA))

df8 = data.frame( lon =c(59.75, 60.25, 60.75, 61.25, 61.75, 62.25 ,62.75, 63.25, 44.25, 44.75, 45.25),
                  lat= c(26.25, 26.25, 26.25, 26.25, 26.25, 26.25, 26.25, 26.25, 26.75, 26.75, 26.75),
                  P= c(0, 0.6, 0.4, 0.2, 0, 0, 0, NA, NA, NA, NA))

df9= data.frame( lon =c(59.75, 60.25, 60.75, 61.25, 61.75, 62.25 ,62.75, 63.25, 44.25, 44.75, 45.25),
                 lat= c(26.25, 26.25, 26.25, 26.25, 26.25, 26.25, 26.25, 26.25, 26.75, 26.75, 26.75),
                 P= c(0, 0, 0.5, 0.5, 0, 0, 0, NA, NA, NA, NA))

df10 = data.frame( lon =c(59.75, 60.25, 60.75, 61.25, 61.75, 62.25 ,62.75, 63.25, 44.25, 44.75, 45.25),
                   lat= c(26.25, 26.25, 26.25, 26.25, 26.25, 26.25, 26.25, 26.25, 26.75, 26.75, 26.75),
                   P= c(0, 0, 0, 0, 0, 0, 0, NA, NA, NA, NA))

df11 = data.frame( lon =c(59.75, 60.25, 60.75, 61.25, 61.75, 62.25 ,62.75, 63.25, 44.25, 44.75, 45.25),
                   lat= c(26.25, 26.25, 26.25, 26.25, 26.25, 26.25, 26.25, 26.25, 26.75, 26.75, 26.75),
                   P= c(0, 0, 0.5, 1.1, 0, 0, 0, NA, NA, NA, NA))

df12= data.frame( lon =c(59.75, 60.25, 60.75, 61.25, 61.75, 62.25 ,62.75, 63.25, 44.25, 44.75, 45.25),
                  lat= c(26.25, 26.25, 26.25, 26.25, 26.25, 26.25, 26.25, 26.25, 26.75, 26.75, 26.75),
                  P= c(0, 0, 0.5, 0.7, 0.1, 0.1, 0, NA, NA, NA, NA))

data <- bind_rows(df1, df2, df3,  df4, df5, df6, df7, df8, df9, df10, df11, df12, .id = "Month") %>%
  mutate(Month = lubridate::month(as.numeric(Month), label = TRUE)) 

ggplot(data, aes(lon, lat, fill = P)) +
  geom_tile() + theme_classic() +
  scale_fill_distiller('P (mm)', palette = 'Spectral') +
  borders('world',xlim = range(df1$lon),
    ylim = range(df1$lat),colour = 'black') +
  facet_wrap(. ~ Month) +
  xlab('Longitude') + ylab('Latitude') +
  theme(legend.position = "bottom",legend.text = element_text(angle = 45),
    strip.text.x = element_text(size = 8, margin = margin(0, 0, 0, 0, "cm"))) +
  guides(fill = guide_colorbar(barwidth = 20, barheight = 0.5))

For removing the facet labels (which kind of goes against the logic of faceting, but it is your call) consider theme(strip.text = element_blank())
For bining the values to 3 discrete colors consider cut from base R, with vector of breaks in the breaks argument.

Thanks, this works out. But how to do with the second question? I don't know the order of using 'cut' and 'breaks'. Thanks again.

I use this:

data$brks <- cut(data$P, breaks=c(0,0.5, 1,max(data$P,na.rm=T)),
labels=c("0.00-0.50", "0.50-1.00", ">1.00"))

And this code below

scale_color_viridis(name="", discrete=TRUE)+

instead of this

scale_fill_distiller('P (mm)', palette = 'Spectral') +

But it says

Error in scale_color_viridis(name = "", discrete = TRUE) :
could not find function "scale_color_viridis"

I think you are looking for https://www.rdocumentation.org/packages/ggplot2/versions/3.2.0/topics/scale_colour_viridis_d - it seems to have been renamed in recent ggplot

It says "Error: Discrete value supplied to continuous scale".

I can not replicate the behaviour without your data, but you are looking for something along these lines:

library(ggplot2)

# modified mpg via cut with breaks
mtcars$mpg_mod <- cut(mtcars$mpg, breaks = c(-Inf, 20, 30, Inf))

# point data uses color aestethic, but consider also scale_fill_(something)
ggplot(mtcars, aes(x = disp, y = hp, color = mpg_mod)) +
  geom_point()+
  scale_color_viridis_d()

Thanks, it works. How to display the legend the same as the color bar, but with distinct labels?

I am not 100% sure I understand your question right; guide_colourbar makes sense for continuous variables, guide_legend for discrete ones. I don't get the reason for mixing of the two.

But if you must you can consider something like mapping the discrete variable to geom_point(), which has only color attribute, and the continuous variable to fill - which will not show on the points, but will force a legend.

library(ggplot2)

# modified mpg via cut with breaks
mtcars$mpg_mod <- cut(mtcars$mpg, breaks = c(-Inf, 20, 30, Inf))

# point data uses color aestethic, fill is just for the legend
ggplot(mtcars, aes(x = disp, y = hp, color = mpg_mod, fill = mpg)) +
  geom_point()+
  scale_color_viridis_d() +
  scale_fill_viridis_c()

Thanks for your response.
I didn't mean to mix the two, but to have discrete legend like a color bar. For example, in the attached figure, but in my case the legend can put at the bottom.
%E6%8D%95%E8%8E%B7

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