A non-trivial ggplot
challenge
I compare the spatial distribution of a variable at two moments using maps with a fixed color scale - to show the actual change. It would be very nice to add the distributions of the variables near the legend bars as jitter points.
The desired plot should look something like the picture: the supposed red jitter points are added manually (I just used paint.net) to the plot produced by R
.
Reproduce the maps
To reproduce the maps, an R
object called fortIT
is required. This is a fortified (using ggplot2::fortify
) SpatialPolygonsDataFrame of Italian NUTS-2 regions with the data attached. The RData file can be downloaded here [89KB]
And the code for the maps:
require(dplyr)
require(ggplot2)
require(ggthemes)
require(gridExtra)
require(rgeos)
require(maptools)
require(cowplot)
require(viridis)
# load the data
load(url("https://ikashnitsky.github.io/share/1602-so-q-map-jitter/fortIT.RData"))
# produce the first map
gIT1 <- ggplot()+
geom_polygon(data = fortIT, aes(x=long, y=lat, group=group, fill=tsr03),
color='grey30',size=.1)+
scale_fill_viridis('TSR\n2003',limits=range(fortIT[,9:10]))+ # !!! limits fix the color scale
coord_equal(xlim=c(4000000, 5500000), ylim=c(1500000,3000000))+
guides(fill = guide_colorbar(barwidth = 1.5, barheight = 15))+
theme_map()+
theme(panel.border=element_rect(color = 'black',size=.5,fill = NA),
legend.position = c(1, 1),
legend.justification = c(1, 1),
legend.background = element_rect(colour = NA, fill = NA),
legend.title = element_text(size=15),
legend.text = element_text(size=15))+
scale_x_continuous(expand=c(0,0)) +
scale_y_continuous(expand=c(0,0)) +
labs(x = NULL, y = NULL)
# produce the second map
gIT2 <- ggplot()+
geom_polygon(data = fortIT, aes(x=long, y=lat, group=group, fill=tsr43),
color='grey30',size=.1)+
scale_fill_viridis('TSR\n2043',limits=range(fortIT[,9:10]))+
coord_equal(xlim=c(4000000, 5500000), ylim=c(1500000,3000000))+
guides(fill = guide_colorbar(barwidth = 1.5, barheight = 15))+
theme_map()+
theme(panel.border=element_rect(color = 'black',size=.5,fill = NA),
legend.position = c(1, 1),
legend.justification = c(1, 1),
legend.background = element_rect(colour = NA, fill = NA),
legend.title = element_text(size=15),
legend.text = element_text(size=15))+
scale_x_continuous(expand=c(0,0)) +
scale_y_continuous(expand=c(0,0)) +
labs(x = NULL, y = NULL)
# align both maps side by side
gIT <- plot_grid(gIT1,gIT2,ncol=2,labels=LETTERS[1:2],label_size=20)
ggsave('italy.png',gIT,width=12,height=7,dpi=192)
Additional information
The variables visualized in the maps are Total Support Ratios in 2003 (panel A) and 2043 (panel B, Eurostat regional projection). Total Support Ratio is the ratio of working-age population (15-64) to non-working-age population (younger than 15 and older than 65).