4

Whenever I save a plot using ggsave and scale, the size of the plot goes up, but not the size of the text.

ggplot(economics, aes(date, unemploy)) + 
  geom_line(color="#2fb2ab") +
  theme_ipsum() +
  theme(
    text = element_text(family="Georgia"),
    axis.title.x = element_text(hjust=0.5, size=13, family="Georgia"),
    axis.title.y = element_text(hjust=0.5, size=13, family="Georgia"),
    panel.border = element_rect(colour = "black", fill=NA))+
  ylab("Unemployment") +
  xlab("Date")

ggsave("sample_graph.png", scale = 2)
ggsave("sample_graph2.png", scale = 3)

Here is graph 1:

graph 1, scale = 2

Here is graph 2:

graph 2, scale = 3

How do I get it to scale both the graph size and the font? I don't want to manually set the height and width.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
user3710004
  • 511
  • 1
  • 6
  • 15
  • Note that most good pre-built themes take base size and family arguments. Rather than setting the font family and size in individual customizations, you can do `theme_ipsum(base_family = "Georgia", base_size = 13)`. Other font sizes will be adjusted relative to the 13 base value. – Gregor Thomas Dec 06 '19 at 18:38
  • Yes, but if I set base_size = scale_factor * 13 then the rectangle around the plot becomes twice as thick, which is not what I want. – user3710004 Dec 06 '19 at 19:04

2 Answers2

7

The scale argument of ggsave seems to affect only the plot area and not the fonts. For modifying fonts size, as you did in your code, you have to pass the argument in axis.title.x or axis.title.y.

One way to circumvent this issue is to set a scale factor and use it in your theme function and in ggsave. Something like that should do the trick:

library(ggplot2)
scale_factor = 3
ggplot(economics, aes(date, unemploy)) + 
  geom_line(color="#2fb2ab") +
  theme(
    text = element_text(family="Georgia"),
    axis.title.x = element_text(hjust=0.5, size= scale_factor * 13, family="Georgia"),
    axis.title.y = element_text(hjust=0.5, size= scale_factor * 13, family="Georgia"),
    panel.border = element_rect(colour = "black", fill=NA))+
  ylab("Unemployment") +
  xlab("Date")

ggsave("sample_graph.png", scale = scale_factor)

Let me know if it is what you are looking for

Prradep
  • 5,506
  • 5
  • 43
  • 84
dc37
  • 15,840
  • 4
  • 15
  • 32
3

The text is the same size in both. See an old discussion: R, how to set the size of ggsave exactly.

The first ggsave creates a 14 x 7.54 image. The second creates a 20.9 x 11.3 image. When you open them in a viewer like Photos, you're viewing them at a distorted physical size. If you open them in something like powerpoint, viewing them at their correct physical size, you'll see the fonts are the same.

See the picture below for side by side comparison: sidebyside

AHart
  • 448
  • 3
  • 10
  • 1
    Fonts are the same but based on the size of the image, they are reduced right ? (look for the distance between 8000 and 12000). So, I agree fonts size are the same but they are not following the scale of the area. Am i right ? – dc37 Dec 06 '19 at 18:38
  • 1
    @dc37 The fonts are the same size, and font size in independent of area. Size 10 font is big relative to a notecard, and small relative to a poster. The `scale` parameter changes the size of the paper in this analogy. If you print either image at the standard settings (or open them in a viewer that respects these settings), the images will be different sizes but the fonts will be the same size. – Gregor Thomas Dec 06 '19 at 18:42
  • 1
    In a raster format like `png` or `jpeg`, if you want more pixels but a constant font size relative to the area, rather than using `scale` you should express the dimensions you want (in inches, cm, whatever) and set the `dpi` you want. – Gregor Thomas Dec 06 '19 at 18:45
  • 2
    I have to say I find all of these solutions extremely cumbersome... – altabq Oct 17 '20 at 21:18