ggplot2的x轴上有双重标签

时间:2015-02-19 20:16:19

标签: r ggplot2 axis-labels

我正在使用ggplot2在同一个图上绘制两个不同的离散变量,并且我为两个变量获得的断点明显不同。我想在x轴上用中心标签的叠加值来标记断点,而不是彼此相邻,我设法做到了,但实际上并没有在小图上工作(标签重叠)。

这是我正在使用的代码:

plot<-ggplot(data, aes(xcut,modx, group=1))+ 
  geom_smooth(aes(color="red"), se=F, linetype="dotted", size=1.25)+ 
  geom_line(data=data,aes(xcut,lowerx, color="red4"), size=1.5)+
  geom_line(data=data,aes(xcut,upperx, color="red4"), size=1.5)+
  geom_ribbon(data=data, aes(xcut,ymin=lowerx,ymax=upperx), fill="lightpink", alpha=0.4)+

  geom_smooth(data=databis, aes(xcut,modx, group=1, color="green"), se=F, linetype="dotted", size=1.25)+ 
  geom_line(data=databis,aes(xcut,upperx, color="green4"), size=1.5)+
  geom_line(data=databis,aes(xcut,lowerx, color="green4"), size=1.5)+
  geom_ribbon(data=databis, aes(xcut,ymin=lowerx,ymax=upperx), fill="chartreuse1", alpha=0.4)+

  ylim(min(data$lowerx,databis$lowerx),max(data$upperx,databis$upperx))+

  scale_colour_manual(name = 'Legend', 
                      values =c('red'='red','green'='green', 'green4'='green4', 'red4'='red4'), labels = c('','','',''))+
  scale_size_area() + 
  xlab("") +
  ylab("")+
  labs(title='')

plot<-plot + scale_x_discrete(breaks=c(1:10), labels=c(label6Ex$label))

由于需要大量数据,我很抱歉无法提供实际示例。基本上我使用&#34; x&#34;变量,相关的&#34; modx&#34;变量和离散变量&#34; xcut&#34;只是&#34; x&#34;十分休息。我有两个不同的数据帧(数据和数据集),其中所有变量都已经类似地定义,但显然两个dfs的标签都不同。我最后添加的标签已经单独确定;正如我所说,我试图为每个休息添加两个标签:

plot<-plot + scale_x_discrete(breaks=c(1:10), labels=c(paste(labeldatax$label,labeldatabisx$label,sep=" - ")))

这给了我graph for the x and xbis variables with both labels

但是让两个标签垂直叠加对我来说会更方便。欢迎任何有关此事的帮助!

提前致谢。

1 个答案:

答案 0 :(得分:0)

只需将sep=" - "替换为sep="\n"