thebioengineer / camcorder

Record plots generated during an R session and replay as a gif!
https://thebioengineer.github.io/camcorder/
Other
201 stars 7 forks source link

resulting png file does not reflect requested image width and height in inches #57

Closed carolinarr closed 11 months ago

carolinarr commented 11 months ago

I've been exploring the camcorder package, and am puzzled by the fact that when I start gg_record with a certain size request, the resulting saved files do not match the size requested. For instance, my gg_record() statement looks like this:

library(ggplot2)
library(camcorder)

camcorder::gg_record(
    dir = 'path/to/my/folder',
    device = "png",
    scale = 1,
    width = 3.5,
    height = 2.5,
    units = "in",
    dpi = 300,
    bg = 'white',
)

# example plot
ggplot(d = ToothGrowth, aes(x = len)) +
  geom_histogram(bins = 10) 

When I open the file in the Preview application or InDesign, the size is width 14.58 inches and height 10.42 inches at 72 pixels/inch. Am I missing something?

gkaramanis commented 11 months ago

You’re not missing anything, the short answer is that it’s complicated!

Here’s a blog post that explains how dimensions and resolution work, I hope it’s helpful:

https://www.christophenicault.com/post/understand_size_dimension_ggplot2

carolinarr commented 11 months ago

That blog post is extremely helpful–thank you so much for the link!

I guess I had the wrong impression here; I thought I was avoiding those issues when using camcorder() over ggsave(), but is camcorder() essentially using ggsave() each time it records?

gkaramanis commented 11 months ago

Nice to hear it helped! And you're correct, camcorder uses ggsave. I'm not sure there is a way to avoid the issues, my understanding is that this is how images work in general... 🤷

carolinarr commented 11 months ago

Ok thank you, great to confirm. Very much appreciate your responses!