gpt4 book ai didi

r - 避免 ggplot 分面网格中的 x 轴标签重叠

转载 作者:行者123 更新时间:2023-12-02 01:39:12 29 4
gpt4 key购买 nike

我正在尝试绘制生长图的小面网格,并且每个图末尾的标签彼此重叠。以下是使用 mpg 数据的示例代码:

print(ggplot(data = aggregate(hwy~class+year, data=mpg, mean), aes(x = year, y=hwy))+
geom_line(aes(group = 1))+
geom_point()+
facet_wrap(~class, nrow = 2)+
xlab("Year")+
scale_x_discrete(limits=unique(mpg$year)))

如何防止这种重叠,也许可以通过从绘图边缘移动刻度线和标签来实现。我尝试使用主题内的边距,但也没有成功。感谢您的帮助。

最佳答案

我想您想要的是在主题中使用panel.spacing.x调整小平面面板之间的水平间距(使用ggplot2_3.0.0测试)。

ggplot(data = aggregate(hwy~class+year, data=mpg, mean), aes(x = year, y=hwy))+
geom_line(aes(group = 1))+
geom_point()+
facet_wrap(~class, nrow = 2)+
xlab("Year")+
scale_x_discrete(limits=unique(mpg$year)) +
theme(panel.spacing.x = unit(4, "mm"))

之前

enter image description here

之后 - 使用 panel.spacing.x()

enter image description here

关于r - 避免 ggplot 分面网格中的 x 轴标签重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41225294/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com