每当我使用ggsave和scale保存绘图时,绘图的大小会增加,但文本的大小不会增加。
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)
这是图表1:
这是图表2:
如何让它同时缩放图形大小和字体?我不想手动设置高度和宽度。
发布于 2019-12-07 02:28:55
ggsave
的scale
参数似乎只影响绘图区域,而不影响字体。要修改字体大小,就像您在代码中所做的那样,您必须在axis.title.x
或axis.title.y
中传递参数。
解决此问题的一种方法是设置比例因子,并在theme
函数和ggsave
中使用它。像这样的东西应该能起到作用:
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)
如果这是你要找的,请告诉我。
发布于 2019-12-07 02:33:32
文本在两者中的大小是相同的。请看一个古老的讨论:R, how to set the size of ggsave exactly。
第一个ggsave
创建了一个14 x 7.54的图像。第二个创建20.9 x 11.3的图像。当你在像Photos
这样的查看器中打开它们时,你正在以扭曲的物理尺寸查看它们。如果你用powerpoint之类的东西打开它们,以正确的物理大小查看它们,你会看到字体是一样的。
请参见下图,进行并排比较:
https://stackoverflow.com/questions/59218385
复制相似问题