首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用gtable_add_grob时不显示r次y轴线

使用gtable_add_grob时不显示r次y轴线
EN

Stack Overflow用户
提问于 2014-03-08 23:16:58
回答 1查看 1.1K关注 0票数 2

我正在尝试将降水数据覆盖在我收集的水质数据之上。我已经分别制作了水质数据和降水图,现在我正在尝试使用gtable_add_grob (a la ggplot2)将它们结合起来。我的情节差不多完成了,看上去不错,但我遇到了一个问题,第二个y轴没有显示。我的代码如下(例如):

代码语言:javascript
运行
复制
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轴线也显示出来,你可以看到它在这里丢失了。我最初的怀疑是,这与我使两个图形的面板网格和背景为空白有关,但是独立图上的轴线在使用

代码语言:javascript
运行
复制
axis.line=element_line(colour="black")

此外,我需要清晰的背景来显示这些数据(如果是这样的话,周围有工作吗?)我一步一步地浏览了代码的组合图部分,它似乎正在按预期工作。组合图的输出是

代码语言:javascript
运行
复制
> 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轴线不显示有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-12 03:24:15

所以我找到了一个关于右y轴的方法,如果有人稍后会看到这个,并且很好奇的话,我就会把情节组合在一起。

使用gridExtra包,我使用borderGrob (参见CC)在第一个绘图的右侧创建了一个手动边框。第一个情节可能看起来有点“愚蠢”,当它自己绘制后,适用边界,但我的目标是合并图,所以第一个独立的情节并不真正关心我。此外,我还注意到示例代码中有几个错误类型,其中没有更正用于特定工作的代码中的复制和粘贴,因此,如果有人试图帮助并且无法复制该示例,我非常抱歉!修正后的代码如下:

代码语言:javascript
运行
复制
##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)

这就产生了:

这!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22276333

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档