【问题标题】:Overlapping axis number labels when using 'device = "pdf"' in ggplot2在ggplot2中使用'device =“pdf”'时重叠轴号标签
【发布时间】:2021-08-11 09:11:41
【问题描述】:

This solution 似乎已经死了。它在 9 年零 10 个月前得到了回答,到那时它可以被中间的几个更新所取代。现在,举个例子,我想使用来自this questiondput(这是我的数据)。我当前创建绘图的代码版本如下所示:

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_textsizevjust 的各种选项。

这导致我们的问题:

如何使用 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


【解决方案1】:

对于那些碰巧和我有同样问题的人,我想在这里给出我在处理 ggplot2 一段时间后得到的解决方案。创建图表的代码是:

ggplot(GoatMerged, aes(date, goat_pos, color = as.factor(GoatName), alpha=as.factor(EarStatus))) +
geom_line() +
geom_point() +
scale_alpha_manual(values = c(0.25, 1)) +
scale_x_datetime(date_breaks = '1 day', date_labels = "%d.%m.%Y") + #, guide = guide_axis(n.dodge = 2)) +
labs(color = 'Individual', alpha='some_category', x='Time points', y='Position') +
theme_bw() +
theme(legend.position = "bottom", legend.text=element_text(size = unit(11, "pt")), legend.title=element_text(size = unit(11, "pt"), face="bold"), legend.box = "vertical", legend.direction = "horizontal") +
theme(axis.title.y = element_text(size=unit(11, "pt"), face="bold", vjust=2)) +
theme(axis.title.x = element_text(size=unit(11, "pt"), face="bold", vjust=-2)) +
theme(axis.text=element_text(size=unit(8, "pt"))) +
theme(axis.text.x = element_text(colour = "black", face="bold")) +
theme(axis.text.y = element_text(colour = "black", face="bold")) +
theme(aspect.ratio=9/16) +
theme(plot.margin = unit(c(0,1.5,0,0.5), "cm"))
ggsave(filename="GoatPosition.pdf", device = "pdf", width = 17, height = 13, units = "cm", dpi = 600)

基于 PDF 查看器中的图形如下所示:

我们可以看到日期没有重叠,最后一个日期完全显示在页面上并且没有被切断,并且所有标签都完全存在。 事实证明,将 ggplot2 的纵横比设置为与 ggsave 中以宽度和高度表示的比例一致对于不得到扭曲的图形至关重要。

【讨论】:

    猜你喜欢
    • 2015-08-16
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-10
    • 2020-11-14
    • 2021-07-23
    • 2011-10-10
    • 1970-01-01
    相关资源
    最近更新 更多