【问题标题】:R - Secondary y-axis line not showing up when using gtable_add_grobR - 使用 gtable_add_grob 时未显示辅助 y 轴线
【发布时间】:2014-03-08 23:16:58
【问题描述】:

我正在尝试将降水数据与我一直在收集的水质数据相叠加。我已经分别制作了水质数据和降水图,现在正尝试使用 gtable_add_grob 将它们组合起来(a la http://rpubs.com/kohske/dual_axis_in_ggplot2)。我的情节几乎完成并且看起来不错,但是遇到了辅助 y 轴不显示的问题。我的代码如下(例如):

y=(1:12)
y2=(12:1)
x=seq(as.Date("2014-01-01"), as.Date("2014-12-31"), by="months")
df=data.frame(x,y)
df2=data.frame(x,y2)

#plot1
g<-ggplot(df,aes(x,y))
g<-g+geom_bar(stat="identity",alpha=0.4)
g<-g+scale_y_reverse()
g<-g+theme(panel.grid = element_blank())
g<-g+theme(panel.background = element_blank())
g<-g+scale_x_date(labels = date_format("%b-%y"),breaks = date_breaks("months"))
g<-g+theme(axis.text.x = element_text(angle=45,hjust=1,color="black"))
g<-g+theme(axis.text.y = element_text(color="black"))
g<-g+theme(panel.grid = element_blank())
g<-g+theme(axis.line=element_line(colour="black"))
#print(g) #looks fine with axes lines

#plot2
g2<-ggplot(df,aes(x,y))
g2<-g2+geom_line()
g2<-g2+theme(panel.grid = element_blank())
g2<-g2+theme(panel.background = element_blank())
g2<-g2+scale_x_date(labels = date_format("%b-%y"),breaks = date_breaks("months"))
g2<-g2+theme(axis.text.x = element_text(angle=45,hjust=1,color="black"))
g2<-g2+theme(axis.text.y = element_text(color="black"))
g2<-g2+theme(panel.grid = element_blank())
g2<-g2+theme(axis.line=element_line(colour="black"))
#print(g2) #looks fine with axes lines

#combining them
gnew1<-ggplot_gtable(ggplot_build(g))
gnew2<-ggplot_gtable(ggplot_build(g2))
gg<-c(subset(gnew1$layout,name=="panel",se=t:r))
gnew<-gtable_add_grob(gnew2,gnew1$grobs[[which(gnew1$layout$name=="panel")]],pp$t,pp$l,pp$b,pp$l)
#attempted secondary axis    
ia<-which(gnew1$layout$name=="axis-l")
ga<-gnew1$grobs[[ia]]
ax<-ga$children[[2]]
ax$widths<-rev(ax$widths)
ax$grobs<-rev(ax$grobs)
ax$grobs[[1]]$x<-ax$grobs[[1]]$x - unit(1,"npc") + unit(0.15, "cm")
gnew<-gtable_add_cols(gnew,p1$widths[p1$layout[ia, ]$l], length(g1$widths) - 1)
gnew<-gtable_add_grob(gnew, ax, pp$t,length(gnew$widths)-1)
grid.draw(gnew)

这给了我一个看起来像这样的情节:

我的问题是我希望辅助 y 轴线也显示出来 - 你可以看到它在这里不见了。我最初的怀疑是,这与我将两个图表的面板网格和背景设置为空白有关,但独立图表上的轴线在使用后绘制得很好

axis.line=element_line(colour="black")

此外,我需要清晰的背景来显示这些数据的方式(所以如果是这样,是否有解决方法?)。我逐步浏览了代码的组合图部分,它似乎按预期工作。我对组合图的输出是

> gnew
TableGrob (6 x 6) "layout": 10 grobs
   z     cells       name                                 grob
1  0 (1-6,1-6) background      rect[plot.background.rect.1102]
2  3 (3-3,3-3)     axis-l absoluteGrob[GRID.absoluteGrob.1094]
3  1 (4-4,3-3)     spacer                       zeroGrob[NULL]
4  2 (3-3,4-4)      panel               gTree[GRID.gTree.1080]
5  4 (4-4,4-4)     axis-b absoluteGrob[GRID.absoluteGrob.1087]
6  5 (5-5,4-4)       xlab         text[axis.title.x.text.1096]
7  6 (3-3,2-2)       ylab         text[axis.title.y.text.1098]
8  7 (2-2,4-4)      title           text[plot.title.text.1100]
9  8 (3-3,4-4)     layout               gTree[GRID.gTree.1048]
10 9 (3-3,5-5)     layout                         gtable[axis]

这类似于我自己数据的组合图的输出。关于为什么不显示辅助 y 轴线的任何想法?

【问题讨论】:

    标签: r plot ggplot2 axis-labels


    【解决方案1】:

    因此,如果有人稍后看到并且好奇的话,我在组合绘图时发现了一个在右侧 y 轴上的解决方法。

    使用 gridExtra 包,我使用borderGrob 在第一个图的右侧创建了一个手动边框(请参阅http://rgm3.lab.nig.ac.jp/RGM/R_rdfile?f=gridExtra/man/borderGrob.Rd&d=R_CC)。第一个情节在应用边界后单独绘制时可能看起来有点“傻”,但我的目标是合并情节,所以第一个独立情节并不真正关心我。此外,我注意到示例代码中有一些错误类型,我没有更正我用于特定工作的代码中的复制和粘贴,如果有人试图提供帮助但无法重现该示例,我深表歉意!更正后的代码如下:

    ##apologies for not adding these in the question 
    library(ggplot2)
    library(scales)
    library(gtable)
    library(gridExtra)
    
    y=(1:12)
    y2=(12:1)
    x=seq(as.Date("2014-01-01"), as.Date("2014-12-31"), by="months")
    df=data.frame(x,y)
    df2=data.frame(x,y2)
    
    #plot1
    g<-ggplot(df,aes(x,y))
    g<-g+geom_bar(stat="identity",alpha=0.4)
    g<-g+scale_y_reverse()
    g<-g+theme(panel.grid = element_blank())
    g<-g+theme(panel.background = element_blank())
    g<-g+scale_x_date(labels = date_format("%b-%y"),breaks = date_breaks("months"))
    g<-g+theme(axis.text.x = element_text(angle=45,hjust=1,color="black"))
    g<-g+theme(axis.text.y = element_text(color="black"))
    ##creating a border on the right side (type=3) ##make sure colour is spelled with a u!
    gg<-borderGrob(type=3,colour="black",lwd=1)
    ##adding the new border
    g<-g+annotation_custom(gg)
    g<-g+theme(axis.line=element_line(colour="black"))
    #print(g) ##now plotted with 3 axis lines (left, bottom, right)
    
    #plot2
    g2<-ggplot(df,aes(x,y))
    g2<-g2+geom_line()
    g2<-g2+theme(panel.grid = element_blank())
    g2<-g2+theme(panel.background = element_blank())
    g2<-g2+scale_x_date(labels = date_format("%b-%y"),breaks = date_breaks("months"))
    g2<-g2+theme(axis.text.x = element_text(angle=45,hjust=1,color="black"))
    g2<-g2+theme(axis.text.y = element_text(color="black"))
    g2<-g2+theme(panel.grid = element_blank())
    g2<-g2+theme(axis.line=element_line(colour="black"))
    #print(g2) 
    
    #combining them
    gnew1<-ggplot_gtable(ggplot_build(g))
    gnew2<-ggplot_gtable(ggplot_build(g2))
    gg<-c(subset(gnew1$layout,name=="panel",se=t:r))
    gnew<-gtable_add_grob(gnew2,gnew1$grobs[[which(gnew1$layout$name=="panel")]],gg$t,gg$l,gg$b,gg$l) ##fixed pp->gg
    #extracting the axis from plot1  
    ia<-which(gnew1$layout$name=="axis-l")
    ga<-gnew1$grobs[[ia]]
    ax<-ga$children[[2]]
    ax$widths<-rev(ax$widths)
    ax$grobs<-rev(ax$grobs)
    ax$grobs[[1]]$x<-ax$grobs[[1]]$x - unit(1,"npc") + unit(0.15, "cm")
    gnew<-gtable_add_cols(gnew,gnew1$widths[gnew1$layout[ia, ]$l], length(gnew2$widths) - 1) ##fixed g1->gnew ##fixed p1->gnew2 (twice)
    gnew<-gtable_add_grob(gnew, ax, gg$t,length(gnew$widths)-1) ##fixed pp->gg
    grid.draw(gnew)
    

    这会产生:

    这个!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多