如何管理gtable()的t、b、l、r坐标以正确绘制次y轴的标签和记号

如何管理gtable()的t、b、l、r坐标以正确绘制次y轴的标签和记号,r,ggplot2,r-grid,gtable,R,Ggplot2,R Grid,Gtable,我正在使用facet_wrap,还能够绘制辅助y轴。但是,标签不会在轴附近打印,而是打印得很远。我意识到,如果我了解如何操作GROB的GTTable t、b、l、r的坐标系,这一切都会得到解决。有人能解释一下他们到底是怎么描述的吗-t:r=c4,8,4,4意味着什么 带有ggplot的辅助yaxis有许多链接,但是当nrow/ncol大于1时,它们会失败。所以,请教我网格几何和网格位置管理的基础知识 编辑:代码 this is the final code written by me : li

我正在使用facet_wrap,还能够绘制辅助y轴。但是,标签不会在轴附近打印,而是打印得很远。我意识到,如果我了解如何操作GROB的GTTable t、b、l、r的坐标系,这一切都会得到解决。有人能解释一下他们到底是怎么描述的吗-t:r=c4,8,4,4意味着什么

带有ggplot的辅助yaxis有许多链接,但是当nrow/ncol大于1时,它们会失败。所以,请教我网格几何和网格位置管理的基础知识

编辑:代码

this is the final code written by me :

library(ggplot2)
library(gtable)
library(grid)
library(data.table)
library(scales)

# Data  
diamonds$cut <- sample(letters[1:13], nrow(diamonds), replace = TRUE)
dt.diamonds <- as.data.table(diamonds) 
d1 <- dt.diamonds[,list(revenue = sum(price),
                    stones = length(price)),
              by=c("clarity", "cut")]
setkey(d1, clarity, cut)

# The facet_wrap plots
p1 <- ggplot(d1, aes(x = clarity, y = revenue, fill = cut)) +
geom_bar(stat = "identity") +
labs(x = "clarity", y = "revenue") +
facet_wrap( ~ cut) +
scale_y_continuous(labels = dollar, expand = c(0, 0)) + 
theme(axis.text.x = element_text(angle = 90, hjust = 1),
    axis.text.y = element_text(colour = "#4B92DB"), 
    legend.position = "bottom")

p2 <- ggplot(d1, aes(x = clarity, y = stones, colour = "red")) +
  geom_point(size = 4) + 
  labs(x = "", y = "number of stones") + expand_limits(y = 0) +
  scale_y_continuous(labels = comma, expand = c(0, 0)) +
  scale_colour_manual(name = '', values = c("red", "green"),                                 
     labels =       c("Number of Stones"))+
  facet_wrap( ~ cut) +
  theme(axis.text.y = element_text(colour = "red")) +
  theme(panel.background = element_rect(fill = NA),
       panel.grid.major = element_blank(),
       panel.grid.minor = element_blank(),
       panel.border = element_rect(fill = NA, colour = "grey50"),
       legend.position = "bottom")


# Get the ggplot grobs
xx <- ggplot_build(p1)
g1 <- ggplot_gtable(xx)

yy <- ggplot_build(p2)
g2 <- ggplot_gtable(yy)

nrow = length(unique(xx$panel$layout$ROW))
ncol = length(unique(xx$panel$layout$COL))
npanel = length(xx$panel$layout$PANEL)

pp <- c(subset(g1$layout, grepl("panel", g1$layout$name), se = t:r))
g <- gtable_add_grob(g1, g2$grobs[grepl("panel", g1$layout$name)], 
                     pp$t, pp$l, pp$b, pp$l)

hinvert_title_grob <- function(grob){
  widths <- grob$widths
  grob$widths[1] <- widths[3]
  grob$widths[3] <- widths[1]
  grob$vp[[1]]$layout$widths[1] <- widths[3]
  grob$vp[[1]]$layout$widths[3] <- widths[1]

  grob$children[[1]]$hjust <- 1 - grob$children[[1]]$hjust 
  grob$children[[1]]$vjust <- 1 - grob$children[[1]]$vjust 
  grob$children[[1]]$x <- unit(1, "npc") - grob$children[[1]]$x
  grob
}

j = 1
k = 0

for(i in 1:npanel){
  if ((i %% ncol == 0) || (i == npanel)){
    k = k + 1
    index <- which(g2$layout$name == "axis_l-1")  # Which grob
    yaxis <- g2$grobs[[index]]                    # Extract the grob
    ticks <- yaxis$children[[2]]
    ticks$widths <- rev(ticks$widths)
    ticks$grobs <- rev(ticks$grobs)
    ticks$grobs[[1]]$x <- ticks$grobs[[1]]$x - unit(1, "npc")
    ticks$grobs[[2]] <- hinvert_title_grob(ticks$grobs[[2]])
    yaxis$children[[2]] <- ticks
    if (k == 1)#to ensure just once d secondary axisis printed 
      g <- gtable_add_cols(g,g2$widths[g2$layout[index,]$l],
              max(pp$r[j:i]))
      g <- gtable_add_grob(g,yaxis,max(pp$t[j:i]),max(pp$r[j:i])+1,
                 max(pp$b[j:i])
                     , max(pp$r[j:i]) + 1, clip = "off", name = "2ndaxis")
     j = i + 1
  }
}

# inserts the label for 2nd y-axis 
loc_1st_yaxis_label <- c(subset(g$layout, grepl("ylab", g$layout$name), se  
                       = t:r)) 
loc_2nd_yaxis_max_r <- c(subset(g$layout, grepl("2ndaxis", g$layout$name), 
                      se = t:r))
zz <- max(loc_2nd_yaxis_max_r$r)+1
loc_1st_yaxis_label$l <- zz
loc_1st_yaxis_label$r <- zz

index <- which(g2$layout$name == "ylab") 
ylab <- g2$grobs[[index]]                # Extract that grob
ylab <- hinvert_title_grob(ylab)  
ylab$children[[1]]$rot <- ylab$children[[1]]$rot + 180
g <- gtable_add_grob(g, ylab, loc_1st_yaxis_label$t, loc_1st_yaxis_label$l
                     , loc_1st_yaxis_label$b, loc_1st_yaxis_label$r
                     , clip = "off", name = "2ndylab")
grid.draw(g)
@桑迪,这是密码和密码


唯一的问题是,在最后一行中,辅助y轴标签位于面板内部。我试图解决此问题,但无法解决问题。您的gtable_add_cols和gtable_add_grob命令存在问题。我在下面添加了评论

更新至ggplot2 v2.2.0

2布局数据框。g$layout返回一个数据框,其中包含绘图中包含的GROB的名称及其在GTTable中的位置:t、l、b和r代表顶部、左侧、右侧和底部。例如,请注意,面板位于t=3、l=4、b=3、r=4处。这与从上图中获得的面板位置相同

 g$layout
布局图试图给出行和列的高度和宽度,但它们往往重叠。相反,使用g$宽度和g$高度。1null width and height是绘图面板的宽度和高度。请注意,1null是第三个高度和第四个宽度-再次为3和4

现在绘制一个镶嵌面环绕图和镶嵌面网格图

p1 <- ggplot(mtcars, aes(x = mpg, y = disp)) + geom_point() +
   facet_wrap(~ carb, nrow = 1)

p2 <- ggplot(mtcars, aes(x = mpg, y = disp)) + geom_point() +
   facet_grid(. ~ carb)

g1 <- ggplotGrob(p1)
g2 <- ggplotGrob(p2)
像以前一样浏览绘图,但也要将布局数据框子集,以包含面板的索引

pp3 <- subset(g3$layout, grepl("panel", g3$layout$name), select = t:r)
正如您所期望的,pp3告诉您,绘图面板位于第4、7和10列以及第4和第8行中

将行或列添加到GTTable以及将GROB添加到GTTable时,将使用这些索引。使用?gtable_add_rows和gtable_add_grob检查这些命令


另外,学习一些网格,特别是如何构造grob,以及单元的使用。这里的r-grid标签中给出了一些资源,以此类推

您的gtable\u add\u cols和gtable\u add\u grob命令出现问题。我在下面添加了评论

更新至ggplot2 v2.2.0

2布局数据框。g$layout返回一个数据框,其中包含绘图中包含的GROB的名称及其在GTTable中的位置:t、l、b和r代表顶部、左侧、右侧和底部。例如,请注意,面板位于t=3、l=4、b=3、r=4处。这与从上图中获得的面板位置相同

 g$layout
布局图试图给出行和列的高度和宽度,但它们往往重叠。相反,使用g$宽度和g$高度。1null width and height是绘图面板的宽度和高度。请注意,1null是第三个高度和第四个宽度-再次为3和4

现在绘制一个镶嵌面环绕图和镶嵌面网格图

p1 <- ggplot(mtcars, aes(x = mpg, y = disp)) + geom_point() +
   facet_wrap(~ carb, nrow = 1)

p2 <- ggplot(mtcars, aes(x = mpg, y = disp)) + geom_point() +
   facet_grid(. ~ carb)

g1 <- ggplotGrob(p1)
g2 <- ggplotGrob(p2)
像以前一样浏览绘图,但也要将布局数据框子集,以包含面板的索引

pp3 <- subset(g3$layout, grepl("panel", g3$layout$name), select = t:r)
正如您所期望的,pp3告诉您,绘图面板位于第4、7和10列以及第4和第8行中

将行或列添加到GTTable以及将GROB添加到GTTable时,将使用这些索引。使用?gtable_add_rows和gtable_add_grob检查这些命令


另外,学习一些网格,特别是如何构造grob,以及单元的使用。这里的r-grid标签中给出了一些资源,以此类推

我意识到,如果我知道如何操纵GROB的gridt,b,l,r坐标系,这一切都会得到解决。我对此表示怀疑。我从来没有为这样的任务编辑过这些。@Roland那么应该怎么做,先生?我想学习d基础。请建议并指导我正确的步骤。你可能可以从@Sandy那里学到一些东西。我实际上参考了你的代码,学到了很多!!但是,我想通过从facet_wrap中删除nrow=1参数来概括您的情况,这样对于每一个新行,我都有辅助y轴记号标记和记号标记标签以及一个公共y轴标签。在这里,代码失败了,我想学习如何克服它。@Sandy你能解释一下使用maxpp$t和类似的b、l、r并向其中添加1的逻辑吗。我想了解你的代码背后的逻辑。我意识到如果我了解如何操纵grobs的gridt,b,l,r坐标系,这一切都会得到解决。我对此表示怀疑。我从来没有为这样的任务编辑过这些。@Roland那么应该怎么做,先生?我想学习d基础。请建议并指导我正确的步骤。你可能可以从@Sandy那里学到一些东西。我实际上参考了你的代码,学到了很多!!不过,我想概括一下
通过从facet_wrap中删除nrow=1参数,使每一新行都有次y轴记号和记号标签以及一个公共y轴标签。在这里,代码失败了,我想学习如何克服它。@Sandy你能解释一下使用maxpp$t和类似的b、l、r并向其中添加1的逻辑吗。我想了解你的代码背后的逻辑。我把我的输出作为我的答案贴在下面。您能帮我缩小次轴标签和记号标记标签之间的间隙吗?是否有办法使用gtable::cbind_gtable来替换以合并2个gtables图例?因为在帮助页面中?':'他们不喜欢使用三重冒号运算符。我已将我的输出作为我的答案发布在下面。您能帮我缩小次轴标签和记号标记标签之间的间隙吗?是否有办法使用gtable::cbind_gtable来替换以合并2个gtables图例?因为在“帮助”页面中?“:”他们不喜欢使用三重冒号运算符。
pp1 <- subset(g1$layout, grepl("panel", g1$layout$name), select = t:r)
pp2 <- subset(g2$layout, grepl("panel", g2$layout$name), select = t:r)
p3 <- ggplot(mtcars, aes(x = mpg, y = disp)) + geom_point() +
   facet_wrap(~ carb, nrow = 2)
g3 <- ggplotGrob(p3)
pp3 <- subset(g3$layout, grepl("panel", g3$layout$name), select = t:r)