1

I need to produce a high resolution (1200dpi) image for a journal. However, using cowplot::draw_plot to overlay some insets seems to lower the resolution of the basemap (see resolution of "basemap_test.pdf" vs "cowplot_test.pdf"). There is a similar question here:

The draw_image() function from cowplot results in blurred pdfs

but that one is to do with bringing in outside images; this seems more straightforward, working with a ggplot object in the R environment. Is there a way I can stop the basemap blurring?

snip from basemap pdf snip from ggdraw pdf

Sorry for the length of the reprex - wanted it as close as possible to my plot

if (!require(pacman)) install.packages('pacman')
library(pacman)
pacman::p_load(tidyverse,cowplot,raster,sf,maptools,fasterize)

eckIV = st_crs("ESRI:54012")

data("wrld_simpl")
wrld = st_as_sf(wrld_simpl) %>% 
  st_transform(eckIV)

#inset a
a_lims <- c(-9266960,-7503720,4725508,5796774)
a_sf <- st_bbox(extent(a_lims)) %>% st_as_sfc %>% st_sf
#inset b
b_lims <- c(-5203519,-3440279,-3705917,-2634651)
b_sf <- st_bbox(extent(b_lims)) %>% st_as_sfc %>% st_sf
#inset c
c_lims <- c(-43965.2,1719274.8,5759670.1,6830936.1)
c_sf <- st_bbox(extent(c_lims)) %>% st_as_sfc %>% st_sf
#inset d
d_lims <- c(9213988,10977228,3481996,4553262)
d_sf <- st_bbox(extent(d_lims)) %>% st_as_sfc %>% st_sf

insetBoxes <- rbind(a_sf,b_sf,c_sf,d_sf) %>% 
  st_set_crs(eckIV)

#dummy raster data
rst = raster(ext=extent(wrld),res=30000,crs=eckIV$proj4string)
values(rst) <- runif(n=ncell(rst),0,1)
rst = mask(rst,fasterize(wrld,rst))
rst_df = as.data.frame(rst, xy=TRUE)

#basemap
map <- ggplot() +
  geom_sf(data=wrld, col=NA) +
  geom_raster(data = rst_df, aes(fill = layer, x = x, y = y)) +
  geom_sf(data=insetBoxes, fill=NA, size=0.5) +
  scale_fill_gradientn(colours=terrain.colors(100),na.value = "transparent") +
  theme_void() +
  theme(legend.position = "none")

#insets
theme_inset <- theme_void() + 
  theme(legend.position="none",
        panel.border = element_rect(colour = "black", fill = NA))

inset1 <- ggplot() +
  geom_sf(data=wrld, col=NA) +
  geom_raster(data = rst_df, aes(fill = layer, x = x, y = y)) +
  scale_fill_gradientn(colours=terrain.colors(100),na.value = "transparent") +
  geom_sf(data=wrld, fill=NA, size=0.1) +
  theme_inset +
  coord_sf(xlim = c(c_lims[[1]], c_lims[[2]]),
           ylim = c(c_lims[[3]], c_lims[[4]]),
           expand = FALSE)

inset2 <- ggplot() +
  geom_sf(data=wrld, col=NA) +
  geom_raster(data = rst_df, aes(fill = layer, x = x, y = y)) +
  scale_fill_gradientn(colours=terrain.colors(100),na.value = "transparent") +
  geom_sf(data=wrld, fill=NA, size=0.1) +
  theme_inset +
  coord_sf(xlim = c(d_lims[[1]], d_lims[[2]]),
           ylim = c(d_lims[[3]], d_lims[[4]]),
           expand = FALSE)

inset3 <- ggplot() +
  geom_sf(data=wrld, col=NA) +
  geom_raster(data = rst_df, aes(fill = layer, x = x, y = y)) +
  scale_fill_gradientn(colours=terrain.colors(100),na.value = "transparent") +
  geom_sf(data=wrld, fill=NA, size=0.1) +
  theme_inset +
  coord_sf(xlim = c(a_lims[[1]], a_lims[[2]]),
           ylim = c(a_lims[[3]], a_lims[[4]]),
           expand = FALSE)

inset4 <- ggplot() +
  geom_sf(data=wrld, col=NA) +
  geom_raster(data = rst_df, aes(fill = layer, x = x, y = y)) +
  scale_fill_gradientn(colours=terrain.colors(100),na.value = "transparent") +
  geom_sf(data=wrld, fill=NA, size=0.1) +
  theme_inset +
  coord_sf(xlim = c(b_lims[[1]], b_lims[[2]]),
           ylim = c(b_lims[[3]], b_lims[[4]]),
           expand = FALSE)

#combine
world_map = map %>%
  cowplot::ggdraw() +
  cowplot::draw_plot(inset1 + theme(plot.background = element_rect(fill = "white", colour = NA)),
                     width = 0.25,
                     height = 0.25,
                     x = 0.75,
                     y = 0.7) + 
  cowplot::draw_plot(inset2 + theme(plot.background = element_rect(fill = "white", colour = NA)),
                     width = 0.25,
                     height = 0.25,
                     x = 0.75,
                     y = 0.4) + 
  cowplot::draw_plot(inset3 + theme(plot.background = element_rect(fill = "white", colour = NA)),
                     width = 0.25,
                     height = 0.25,
                     x = 0,
                     y = 0.7) + 
  cowplot::draw_plot(inset4 + theme(plot.background = element_rect(fill = "white", colour = NA)),
                     width = 0.25,
                     height = 0.25,
                     x = 0,
                     y = 0.4)

rtn.plot = cowplot::plot_grid(world_map,world_map,ncol=1, labels = letters[1:2])

ggsave(plot=rtn.plot,
       filename="cowplot_test.pdf",
       width=22,height=18,unit="cm",device="pdf", dpi=1200)

ggsave(plot=map,
       filename="basemap_test.pdf",
       width=22,height=18,unit="cm",device="pdf", dpi=1200)
sebdunnett
  • 21
  • 2

0 Answers0