Aspect ratio of a plot produced with ggplot2:coord_quickmap

Hi all,

I'm using ggplot2 a lot to produce maps of meteorological variables (longitude/latitude coordinates). Thus, I use coord_map or coord_quickmap which puts the plot into the right proportion. When I save the plot with ggsave, the resulting file exhibits empty space (see examples below): top/bottom or left/right depending on the size of the device (RStudio) at the time the command is executed. I could of course fiddle about with width/height parameters of ggsave. Is there a better way to determine the aspect ratio of a plot?

Any help would be very much appreciated.
Best regards,
Matt

Example:

library(ggplot2)
data("quakes")

dir.temp <- "~/Data/test"

p.quickmap <- ggplot(quakes, aes(long, lat)) + geom_point() + 
  coord_quickmap() + ggtitle("Quickmap") + theme_bw(base_size = 32)

p.cartesian <- ggplot(quakes, aes(long, lat)) + geom_point() + 
  coord_cartesian() + ggtitle("Cartesian") + theme_bw(base_size = 32)

dev.size("in")
#> [1] 7 5

ggsave("p.quickmap.png", plot = p.quickmap, path = dir.temp, dpi = 72)
#> Saving 7 x 5 in image

ggsave("p.cartesian.png", plot = p.cartesian, path = dir.temp,dpi = 72)
#> Saving 7 x 5 in image

Hi all,

I found a workaround for the problem I outlined above. I use coord_quickmap to estimate the aspect ratio for the plot. With this information I define the the paper size. In the following example the paper width is fixed and the height is derived from the aspect ratio. Titles, legends need extra adjustments to the aspect ratio (see below). Finally, I use coord_cartesian to plot the graph which fills the paper (no blank space). There might be small distortions compared to coord_quickmap, but it suits the purpose pretty well.

Best, Matt

library(ggplot2)
library(gridExtra)
data("quakes")

dir.temp <- "~/Data/test"

dpi <- 144
plot.width <- 148 # Format DIN A6 landscape mm
plot.height <- 105 # Format DIN A6 landscape mm
base_size <- 16 # Points
aps.correction <- .9 # Scaling that accounts for spaced you by title

# Let coord_quickmap figure out the aspect ratio:
coord <- coord_quickmap(xlim = range(quakes$long), ylim = range(quakes$lat), expand = F)
asp <- coord$aspect(list(x.range = range(quakes$long), y.range = range(quakes$lat)))
asp
#> [1] 1.365346

# Calculate height
plot.height.new <- plot.width * asp * aps.correction

p.quickmap <- ggplot(quakes, aes(long, lat)) + geom_point() + 
  coord_quickmap() + ggtitle("Quickmap") + theme_bw(base_size = base_size) + 
  theme(plot.background = element_rect(fill = "red"))

p.cartesian <- ggplot(quakes, aes(long, lat)) + geom_point() + 
  coord_cartesian() + ggtitle("Cartesian") + theme_bw(base_size = base_size) +
  theme(plot.background = element_rect(fill = "red"))

# Output
ggsave("p_quickmap.png", plot = p.quickmap, path = dir.temp, dpi = dpi, 
  width = plot.width, height = plot.height, units = "mm")

ggsave("p_cartesian.png", plot = p.cartesian, path = dir.temp, dpi = dpi, 
  width = plot.width, height = plot.height, units = "mm")

ggsave("p_cartesian_mod.png", plot = p.cartesian, path = dir.temp, dpi = dpi, 
  width = plot.width, height = plot.height.new, units = "mm")

Created on 2018-06-11 by the reprex package (v0.2.0).

1 Like