【发布时间】:2021-08-11 09:11:41
【问题描述】:
This solution 似乎已经死了。它在 9 年零 10 个月前得到了回答,到那时它可以被中间的几个更新所取代。现在,举个例子,我想使用来自this question 的dput(这是我的数据)。我当前创建绘图的代码版本如下所示:
ggplot(GoatMerged, aes(date, goat_pos, color = as.factor(GoatName))) +
geom_line() +
scale_x_datetime(date_breaks = '1 day') +
labs(color = 'Goats', x='Time', y='Positions') +
theme(axis.text=element_text(size=6)) +
#theme(aspect.ratio=16/9) +
#coord_fixed(ratio=3/4) +
#theme(axis.title.y = element_text(size=40, vjust=2)) +
#theme(axis.title.x = element_text(size=40, vjust=-0.05)) +
theme_classic()
ggsave(filename="GoatPositionBosca.pdf", device = "pdf", width = 12, height = 7, units = "cm")
被注释掉的行表明我的其他尝试使情节发挥作用。现在,在 Windows 下的 R GUI 中,输出如下所示:
使用device = "pdf" ggsave 之后的输出却给出了这个结果:
我们可以看到,x 轴的数字标签是重叠的。 ggsave 忽略了使用 element_text 等 size 或 vjust 的各种选项。
这导致我们的问题:
如何使用 ggsave 和 device = "pdf" 以保证 x 轴数字标签不失真?
编辑
感谢@elielink 的评论,我可以进行一些更改,并通过这样做也实现其他一些东西:
ggplot(GoatMerged, aes(date, goat_pos, color = as.factor(GoatName))) +
geom_line() +
scale_x_datetime(date_breaks = '1 day', guide = guide_axis(n.dodge = 2)) +
labs(color = 'Goats', x='Time', y='Positions') +
theme_classic() +
theme(axis.text=element_text(size=6)) +
theme(legend.position = "bottom")
ggsave(filename="GoatPositionBosca.pdf", device = "pdf", width = 12, height = 9, units = "cm")
这给出了结果:
这里我们可以看到PDF剪掉了x轴编号标签的右侧,因此标签仍然是扭曲的。
【问题讨论】:
-
我认为元素文本可能会被忽略,因为在之前的主题修改之后有这个终极
theme_classic()。按此顺序尝试,theme_classic()+ theme(axis.text=element_text(size=6)) + ggsave(filename="GoatPositionBosca.pdf", device = "pdf", width = 12, height = 7, units = "cm")并告诉我它是否有效
标签: r pdf ggplot2 data-visualization ggsave