How to preview plots at specific dimensions in RStudio
If you've ever saved plots made in ggplot to a file, you've probably noticed that the dimensions of your plots you saw in RStudio does not correspond to their dimensions outside of RStudio. I recently learned a tip from Nicola Rennie for how to deal with this. Using the ggrecord()
function from the camcorder
package, you can preview plots in RStudio so that they have the exact same dimensions as when you save them with ggsave()
.
Code shown in video
library(tidyverse)
library(palmerpenguins)
penguins |>
count(island) |>
ggplot(
aes(
x = island,
y = n
)
) +
geom_col()
ggsave(
filename = "penguins-by-island.png",
width = 4,
height = 8,
unit = "in"
)
library(camcorder)
gg_record(
device = "png",
width = 4,
height = 8,
unit = "in"
)
penguins |>
count(island) |>
ggplot(
aes(
x = island,
y = n
)
) +
geom_col()
Sign up for the newsletter
Get blog posts like this delivered straight to your inbox.