在facet上标注小平面标题 [英] Annotating facet title as strip over facet

查看:159
本文介绍了在facet上标注小平面标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 ggplot2 中的小平面图上添加小平面标题。我的MWE引发错误。任何帮助将不胜感激。谢谢

  library(ggplot2)
library(gtable)
p < - ggplot(mtcars,aes( mpg,wt))+ geom_point()
p <-p + facet_grid(。〜cyl)


#get gtable object
Plot1 < - ggplot_gtable( (图1,图1高度[[3]],2)
Plot1 < - gtable_add_grob(Plot1,
list(rectGrob(gp = gpar(col = NA,fill = gray(0.5))),
textGrob(Cyl,gp = gpar(col = gray(1)))),
3,4,3,10,name = paste(runif(2)))

#添加边距
Plot1 < - gtable_add_rows(Plot1,unit(1/8, line),2)

#绘制
grid.newpage()
print(grid.draw(Plot1))


解决方案

我认为你很接近。您在add grob命令中有错误的行号和列号。你的新长条横跨4到8列;也就是说,在add grob命令中将10更改为8。

 库(ggplot2)
库(gtable)
库(网格)
$ b $ pb facet_grid(。cyl)

z< -ggplotGrob(p)< ggplotGrobot(pt)

#$ b $上方的新条带< - gtable_add_rows(z,z $ height [6],pos = 5)#在第5行以下添加的新行

#检查布局
gtable_show_layout(z)#新条带进入第6行
#新条带跨第4至8列

z< - gtable_add_grob(z,
列表(rectGrob(gp = gpar(col = NA,fill =gray85,size = .5)),
textGrob(柱面数,gp = gpar(cex = .75,fontface =' (b),b = 6,r = 8,name = c(a,b))

#在条带之间添加小间隙 - 在第6行
z < - gtable_add_rows(z,unit(2/10,line),6)

#绘制它
grid.newpage()
grid.draw(z)



或者,在右边添加一个新条:
$ b $ (gtable)
library(grid)

p < - ggplot(mtcars,aes(b) mpg,wt))+ geom_point()
p <-p + facet_grid(cyl〜。)

z < - ggplotGrob(p)

#右边
z< - gtable_add_cols(z,z $ widths [5],pos = 5)#在列5的右边添加新列

#检查布局
gtable_show_layout(z)#新条带进入第6列
#新条带跨行6至10
z< - gtable_add_grob(z,
list(rectGrob(gp = gpar(col = NA ,fill =gray85,size = .5)),
textGrob(圆柱体数量,rot = -90,
gp = gpar(cex = .75,fontface ='bold',














$ b $ =在条带之间添加小间隙 - 在第5列右边
z < - gtable_add_cols(z,unit(2/10 ,line),5)

#绘制
grid.newpage()
grid.draw(z)


I want to add a facet title as strip over a facetted plot in ggplot2. My MWE throws an error. Any help will be highly appreciated. Thanks

library(ggplot2)
library(gtable)
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
p <- p + facet_grid(. ~ cyl)


# get gtable object
Plot1 <- ggplot_gtable(ggplot_build(p))
# add label for top strip
Plot1 <- gtable_add_rows(Plot1, Plot1$heights[[3]], 2)
Plot1 <- gtable_add_grob(Plot1,
  list(rectGrob(gp = gpar(col = NA, fill = gray(0.5))),
  textGrob("Cyl", gp = gpar(col = gray(1)))),
  3, 4, 3, 10, name = paste(runif(2)))

# add margins
Plot1 <- gtable_add_rows(Plot1, unit(1/8, "line"), 2)

# draw it
grid.newpage()
print(grid.draw(Plot1))

解决方案

I think you were close. You had the row and column numbers wrong in the add grob command. Your new strip spans columns 4 to 8; that is, change 10 to 8 in the add grob command.

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

p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
p <- p + facet_grid(. ~ cyl)

z <- ggplotGrob(p)

#  New strip at the top
z <- gtable_add_rows(z, z$height[6], pos = 5)  # New row added below row 5

# Check the layout
gtable_show_layout(z)   # New strip goes into row 6 
                        # New strip spans columns 4 to 8

z <- gtable_add_grob(z, 
  list(rectGrob(gp = gpar(col = NA, fill = "gray85", size = .5)),
       textGrob("Number of Cylinders", gp = gpar(cex = .75, fontface = 'bold', col = "black"))), 
  t=6, l=4, b=6, r=8, name = c("a", "b"))

# Add small gap between strips - below row 6
z <- gtable_add_rows(z, unit(2/10, "line"), 6)

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

OR, to add a new strip to the right:

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

p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
p <- p + facet_grid(cyl ~ .)

z <- ggplotGrob(p)

#  New strip to the right
z <- gtable_add_cols(z, z$widths[5], pos = 5)  # New column added to the right of column 5

# Check the layout
gtable_show_layout(z)   # New strip goes into column 6 
                        # New strip spans rows 6 to 10
z <- gtable_add_grob(z, 
  list(rectGrob(gp = gpar(col = NA, fill = "gray85", size = .5)),
       textGrob("Number of Cylinders", rot = -90,  
          gp = gpar(cex = .75, fontface = 'bold', col = "black"))), 
  t=6, l=6, b=10, r=6, name = c("a", "b"))

# Add small gap between strips - to the right of column 5
z <- gtable_add_cols(z, unit(2/10, "line"), 5)

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

这篇关于在facet上标注小平面标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆