Reputation: 580
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:
Here is graph 2:
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.
Upvotes: 5
Views: 6129
Reputation: 16178
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
Upvotes: 8
Reputation: 448
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:
Upvotes: 3