gpt4 book ai didi

r - 增加辅助 y 轴与其标题之间的间距

转载 作者:行者123 更新时间:2023-12-02 19:35:33 24 4
gpt4 key购买 nike

我绘制了一个带有两个 y 轴的图表。然而,根据我的口味,轴的标题有点太接近了。我已经设法拉开主轴标题的距离,但不知道如何对次轴进行同样的操作。这是我使用过的代码:

ggplot(data1, aes(x=year))+
geom_line(aes(y= data1$`export share`, colour= "export share"), size = 2)+
geom_line(aes(y=data1$pm25/coeff, colour= "pm25"), size = 2)+
scale_x_continuous(breaks = scales::pretty_breaks(n = 17), name = "Year")+
scale_y_continuous(name="Export Share of GDP", labels = scales::percent, sec.axis=sec_axis(trans = ~.*coeff, name = "PM2.5"))+
theme(axis.title.y = element_text(margin = margin(t = 0, r = 20, b = 0, l = 0)), axis.text.x = element_text(vjust = 5) ,legend.title = element_blank(), legend.box.margin = margin(0,0,0,10), text = element_text(size = 30)) +
scale_colour_discrete("",
breaks=c("export share", "pm25"),
labels=c("Export Share of GDP", "PM2.5")) +
geom_vline(xintercept=2006) +
geom_vline(xintercept = 2000)

And here's a picture of the graph

最佳答案

您可以通过增加plot.margin的第二个参数来增加右侧的边距

+ theme(plot.margin=unit(c(1,3,1,1), "cm"))

然后增加axis.title.y.rightvjust参数:

+ theme(axis.title.y.right = element_text(vjust=2 or 3 or 4))

vjust 的数字越大,离轴越远。当然,您可以将上述两个“主题”命令合并为一个。

关于r - 增加辅助 y 轴与其标题之间的间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61080134/

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