Besides (1) increasing the colors of the legend bar, or making the colors in the figure more distinct, I have another question about how to change the shape of the color bar.
I would like to make the legend bar longer and narrower, so that there will be more colors to show. I use the code and the source below, but could not make it to work in my case. Thanks.
source: Continuous colour bar guide — guide_colourbar • ggplot2
And (2) how to display months as "Jan, Feb, Mar, Apr, ..." rather than "1, 2, 3, 4, ..."? (3) How to add all four frames for each monthly sub-panel? This means that I want to have the left, bottom, and right frames, rather than leaving them out. The datasets and code are below:
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))
library(tidyverse)
bind_rows(df1,df2,df3,df4,df5,df6,df7,df8,df9,df10,df11,df12,.id = "Month") %>%
mutate(Month = as.numeric(Month)) %>% # Convert the categorical variable to numeric
ggplot(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') +
#coord_quickmap(xlim=range(df1$lon), ylim=range(df1$lat)) + # The proportions of the facets are afected by this limits
facet_wrap(vars(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")))