这个解决方案似乎已经死了。它是在 9 年零 10 个月前回答的,到那时它可以被中间的几个更新所取代。现在,举个例子,我想使用dput
from this question(这是我的数据)。我当前创建绘图的代码版本如下所示:
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 中,输出如下所示:
然而, ggsave 使用后的输出device = "pdf"
给出了该结果:
如我们所见,x 轴的数字标签重叠。尝试使用element_text
likesize
或被vjust
ggsave 忽略的各种选项。
这就引出了一个问题:
device = "pdf"
如何以不扭曲 x 轴数字标签的方式使用 ggsave ?
编辑
感谢@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 轴编号标签的右侧,因此标签仍然失真。