【问题标题】:add second axis label to facetted plot将第二个轴标签添加到多面图
【发布时间】:2015-04-10 21:01:11
【问题描述】:

如何为多面图添加第二个轴标签? 我意识到在大多数情况下它们应该是相同的,但我有一个不同模型的技能指标的顶行,而底行是技能的差异,所以我想在现有轴标签下方包含bquote(Delta*.(costlong)),并第二行刻面的左侧。 我试过了

`labs(y=bquote(Delta*.(costlong)*"                      "*.(costlong)*"               "))+` 

但它不可能很好地居中并且在导出时它会移动。

我也玩过

+annotation_custom(textGrob('bquote(Delta*.(costlong))),xmin=-20,xmax=-10,ymin=0,ymax=.2)

但它没有显示出来。我将 x 轴从 0 限制到 160。

这是一些数据和绘图代码:

costlong=bquote(r^2)
skill.m=data.frame(doy=seq(1,160),yr=2000:2001,variable=factor(c('skill_phvfull','skill_phvrcnfull','fulldiff'),levels=c('skill_phvfull','skill_phvrcnfull','fulldiff')),value=runif(2*3*160,0,.6))
skill.m$set=ifelse(grepl('skill',as.character(skill.m$variable)),'data','diff')
skill.m$set=as.factor(skill.m$set)
skill.m[grepl('diff',as.character(skill.m$variable)),'value']=skill.m[grepl('diff',as.character(skill.m$variable)),'value']/3
ggplot(skill.m)+      
     geom_point(aes(x=as.numeric(doy),y=value,colour=variable),alpha=.75,size=1)+
     facet_grid(set~yr,scale='free',space='free')+
     labs(y=costlong)+
     scale_colour_brewer(name='',palette='Set1',labels=c('PHV (blended)','PHV+RCN (blended)','Blended Skill Difference'))+
     scale_x_continuous('day of year',limits=c(1,160),labels=c(1,seq(30,160,30)),breaks=c(1,seq(30,160,30)))+
     scale_y_continuous(breaks=seq(0,.7,.1),labels=seq(0,.7,.1))+
     guides(colour=guide_legend(override.aes=list(alpha=1,size=2)))+
     theme_bw()+
     theme(axis.line=element_line(colour='grey10'),
           strip.background=element_rect(fill='white',colour='white'),
           strip.text=element_text(face='bold',size='12'),
           axis.text.x=element_text(size=8,angle=90,hjust=1,vjust=.5),
           axis.text.y=element_text(size=8,angle=0),
           legend.key=element_rect(colour='white'),
           legend.position = "bottom", 
           legend.box = "horizontal")

有什么建议吗?

【问题讨论】:

  • 你想达到什么目的?在纵坐标或横坐标上添加第二个轴标签?只有标签或值?给出你所拥有的情节和缺失元素的 gimped/toshop/paint 圆圈会很有帮助!

标签: r ggplot2


【解决方案1】:

一种方法是使用gtable 函数。绘制带有空白 y 标签的 ggplot。然后将 ggplot 转换为 grob,为两个标签(上组件和下组件)构造两个文本 grobs,然后将文本 grobs 插入到布局中。

library(ggplot2)
library(gtable)
library(grid)

costlong=bquote(r^2)
skill.m=data.frame(doy=seq(1,160),yr=2000:2001,variable=factor(c('skill_phvfull','skill_phvrcnfull','fulldiff'),levels=c('skill_phvfull','skill_phvrcnfull','fulldiff')),value=runif(2*3*160,0,.6))
skill.m$set=ifelse(grepl('skill',as.character(skill.m$variable)),'data','diff')
skill.m$set=as.factor(skill.m$set)
skill.m[grepl('diff',as.character(skill.m$variable)),'value']=skill.m[grepl('diff',as.character(skill.m$variable)),'value']/3

p = ggplot(skill.m)+      
     geom_point(aes(x=as.numeric(doy),y=value,colour=variable),alpha=.75,size=1)+
     facet_grid(set~yr,scale='free',space='free')+
     labs(y="")+
     scale_colour_brewer(name='',palette='Set1',labels=c('PHV (blended)','PHV+RCN (blended)','Blended Skill Difference'))+
     scale_x_continuous('day of year',limits=c(1,160),labels=c(1,seq(30,160,30)),breaks=c(1,seq(30,160,30)))+
     scale_y_continuous(breaks=seq(0,.7,.1),labels=seq(0,.7,.1))+
     guides(colour=guide_legend(override.aes=list(alpha=1,size=2)))+
     theme_bw()+
     theme(axis.line=element_line(colour='grey10'),
           strip.background=element_rect(fill=NA,colour=NA),
           strip.text=element_text(face='bold',size='12'),
           axis.text.x=element_text(size=8,angle=90,hjust=1,vjust=.5),
           axis.text.y=element_text(size=8,angle=0),
           legend.key=element_rect(colour='white'),
           legend.position = "bottom", 
           legend.box = "horizontal")

# Convert the plot to a grob
gt = ggplotGrob(p)

# Check the layout
gtable_show_layout(gt)  # To manually find the row and column for the labels

# Construct text grobs - one for each label
labL = textGrob(expression(bold(Delta * r^2)), rot = 90,
        gp = gpar(fontsize = 12))
labU = textGrob(expression(bold(r^2)), rot = 90,
        gp = gpar(fontsize = 12))

# Insert the text grobs into the layout
gt <- gtable_add_grob(gt, labL, t = 9, l =  2)
gt <- gtable_add_grob(gt, labU, t = 7, l =  2)

# Make the column a little wider
gt$widths[2] = unit(2, "lines")

# Draw it
grid.newpage()
grid.draw(gt)

【讨论】:

  • 谢谢!直到下周才有时间玩这个,但它看起来像我所追求的。也是使用 grobs 的好例子……我最初试图独立制作顶部和底部的图,但不知道如何控制图的大小。
  • @Dominik 您可以单独绘制图表,然后调整它们的大小 - 请参阅 this answer
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-04
  • 2017-08-03
  • 1970-01-01
相关资源
最近更新 更多