Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 增加次y轴与其标题之间的间距_R_Ggplot2_Axis Labels_Yaxis - Fatal编程技术网

R 增加次y轴与其标题之间的间距

R 增加次y轴与其标题之间的间距,r,ggplot2,axis-labels,yaxis,R,Ggplot2,Axis Labels,Yaxis,我画了一个有两个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_

我画了一个有两个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)

您可以通过增加绘图的第二个参数来增加右边的边距。边距

+ theme(plot.margin=unit(c(1,3,1,1), "cm"))
然后增加axis.title.y.right的
vjust
参数:

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

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

是的,不幸的是,它似乎没有改变任何东西,只是在主轴左侧创建了一个边距。最好的胜利是我们从未打过的战斗。我建议完全避免使用辅助y轴。两个图形一个在另一个下面更合适。次y轴可能会导致视觉混乱。我建议这样做,我会避免使用双y轴。在大多数情况下,这不是一个好主意。我会把你的数据放长一点,将测量值索引到某个起始数字(2000年的值),然后将它们标在同一个轴上。