- 3 Minutes Wednesdays
- Posts
- 3MW (Fix your plot size before you export it)
3MW (Fix your plot size before you export it)
Guten Tag!
Many greetings from Ulm, Germany. This week, I cannot share a new YT video because I was banging away at my keyboard to finish my dissertation over the weekend. Even though my weekend was basically only one long stretch of fixing typos and reformulating stuff, it was all worth it as I got to hand in my dissertation today 🥳
This means that I have a more free time in the next couple of months to work on new content. So stay tuned and expect more content on YouTube and Twitter. Speaking about content, let me show you what we’re going to do this week:
Did you ever run into a situation where you create a neat ggplot like this:
But once you export it using ggsave()
it’s all messed up and looks like this?
This used to drive me crazy before I learned how to fixed that. Today I’m showing the one trick that solved this problem. As always the code for this can be found on GitHub.
The problem
Before I show you the solution to this problem, let me try to explain what’s going on in a couple of sentences.
Basically, all of your sizes and especially text sizes depend on two things:
The canvas size of the image (width and height)
The resolution of the image (dots per inch, dpi for short)
As you probably know, when you work in RStudio and drag around the plots pane, you actually change the canvas size of the image that you see. The resolution of your plot does not change but still the plot looks different.
And even worse, when you use ggsave()
to export your image, you do not only change the width and height of your image (unless you somehow measured the exact size of your Plots pane) but you also change the resolution.
The thing is that the plots window uses a default resolution of 72 dpi whereas ggsave()
uses a resolution of 300 dpi. This latter quantity is pretty standard and makes for high-quality images so you shouldn’t decrease it to 72. In summary, all of these dimensions and resolution changes mess up the way your plot looks.
The solution
Now, it’s actually pretty easy to circumvent this whole ordeal. What you can do is fix both the canvas size and the resolution that you see in RStudio. I use the {camcorder}
package for this.
Basically, when I start working on a dataviz I call camcorder::gg_record()
and set the dimensions and resolution like so:
And then I just work on my plots like I normally would. The great thing now is that all plots will immediately be exported as a .png
-file to the directory I specified and displayed in the viewer and not in the plots pane in RStudio. Thus, neither your dimensions nor your resolution will change as long as you don’t call gg_record()
again with different settings.
That’s it. It’s a pretty easy fix once you know it. And you don’t even have to call ggsave()
to export the image at the end anymore. You can just take your newest image from the directory you specified.
One small caveat
There is one small thing that may confuse you when you use this approach. If you’re using {ragg}
to get custom fonts into your plot, then you should be fine. But if you use {showtext}
, then your fonts may look weird. The reason for that is that you have to tell {showtext}
that you’re using a resolution of 300 dpi now. Just call showtext_opts(dpi = 300)
to do just that.
That’s it for today. Hope you’ve enjoyed this week’s newsletter. If you want to reach out to me, just reply to this mail or find me on Twitter.
See next week,
Albert đź‘‹
If you like my content, you may also enjoy these:
Reply