R-使用gtable_add_grob时未显示辅助y轴线

R-使用gtable_add_grob时未显示辅助y轴线,r,plot,ggplot2,axis-labels,R,Plot,Ggplot2,Axis Labels,我试图将降水数据叠加到我收集的水质数据上。我已经分别绘制了水质数据和降水图,现在正尝试使用GTTable_add_grob(la)将它们结合起来。我几乎完成了绘图,看起来不错,但是我遇到了一个问题,第二个y轴没有显示。我的代码如下(例如): 此外,我需要清晰的背景,以便显示这些数据(如果是这样,是否有解决办法?)。我一步一步地浏览了代码的组合图部分,它似乎按预期工作。我对组合图的输出是 > gnew TableGrob (6 x 6) "layout": 10 grobs z

我试图将降水数据叠加到我收集的水质数据上。我已经分别绘制了水质数据和降水图,现在正尝试使用GTTable_add_grob(la)将它们结合起来。我几乎完成了绘图,看起来不错,但是我遇到了一个问题,第二个y轴没有显示。我的代码如下(例如):

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

> 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轴线不显示有什么想法吗

因此,如果稍后有人对此感到好奇,我会在合并图时在右y轴附近找到一个工作

使用gridExtra包,我使用borderGrob在第一个绘图的右侧创建了一个手动边框(请参见)。在应用边界后单独绘制第一个图时,第一个图可能看起来有点“愚蠢”,但我的目标是合并图,这样第一个独立的图就不会真正影响我了。此外,我注意到示例代码中有一些错误类型,我没有更正我在具体工作中使用的代码的复制和粘贴,因此,如果有人试图提供帮助,并且无法复制示例,我深表歉意!更正后的代码如下所示:

##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)
##抱歉没有在问题中添加这些内容
图书馆(GG2)
图书馆(比例尺)
图书馆(gtable)
图书馆(gridExtra)
y=(1:12)
y2=(12:1)
x=序号(截止日期(“2014-01-01”)、截止日期(“2014-12-31”)和“月份”)
df=数据帧(x,y)
df2=数据帧(x,y2)
#图1
G
> 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]
##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)