本文介绍了ggplot2:从图中删除未使用的因子水平组合的方面(facet_grid)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想选择性地删除一个刻面的ggplot2图中不必要的方面。我看了一下这个问题,但不知道该怎么做(也许现在的建议已经过时了): 显然 drop = TRUE 不起作用这里是因为没有未使用的因素级别,但只有未使用的组合。解决方案在ggplot2 2.2.0中,grobs在情节中已经改变。 library(ggplot2)库(网格)d< - data.frame('factor_1' =因子(c('a','a','b')),'factor_2'=因子(c('1','2','1')),x = 1:3,y = 1:3) p = ggplot(data = d,mapping = aes(x = x,y = y))+ geom_point()+ facet_grid(facets = factor_1〜factor_2,drop = TRUE) #获取ggplot grob g = ggplotGrob(p) #获取布局数据框。 #请注意名称。 #你想删除panel-2-2g $ layout #gtable :: gtable_show_layout(g)#也许有用 #删除grobs #grob需要删除,#并且需要删除布局数据框中的相关行 pos g $ grobs< - g $ grobs [!pos] g $ layout< - g $ layout [!pos,] #或者,将grober替换为nullGrob g = ggplotGrob(p) pos< - grep(pattern =panel-2-2,g $ layout $ name) g $ grobs [[pos]]< - nullGrob() #如果需要,移动轴#g $ layout [g $ layout $ name == (8,8) #绘制绘图 grid.newpage()b-2,c(t,b)] = c grid.draw(g) 您的链接中的答案将需要修改如下所示: n df size = n,replace = TRUE),stringsAsFactors = TRUE) df $ label.new p facet_wrap(〜label.new,ncol = 3,drop = FALSE) $ bg = ggplotGrob(p) g $ layout#注意名称及其位置(t,b,l,r)#gtable :: gtable_show_layout(g)#也许有用 pos g $ grobs< - g $ grobs [!pos] g $ layout< - g $ layout [!pos,] #或者将grober替换为nullGrob g = ggplotGrob (p) pos g $ grobs [pos]< - list(nullGrob()) #移动坐标轴g $布局[g $ layout $ name ==axis-l-1-1,c(l,r)] = c(10,10) grid.newpage() grid.draw(g) I would like to selectively delete unnecessary facets from a facetted ggplot2 figure. I had a look at this question but could not figure out how to do it (maybe the advise there is outdated now):adding empty graphs to facet_wrap in ggplot2Here is a minimal example. I would like to remove the empty facet at the right bottom (b, 2).library('ggplot2')d <- data.frame('factor_1' = factor(c('a', 'a', 'b')), 'factor_2' = factor(c('1', '2', '1')), x = 1:3, y = 1:3)ggplot(data = d, mapping = aes(x = x, y = y)) + geom_point() + facet_grid(facets = factor_1 ~ factor_2, drop = TRUE)Obviously drop = TRUE has no effect here because there are no unused factor levels but only unused combinations thereof. 解决方案 In ggplot2 2.2.0, the names of the grobs in a plot have changed. library(ggplot2)library(grid)d <- data.frame('factor_1' = factor(c('a', 'a', 'b')), 'factor_2' = factor(c('1', '2', '1')), x = 1:3, y = 1:3)p = ggplot(data = d, mapping = aes(x = x, y = y)) + geom_point() + facet_grid(facets = factor_1 ~ factor_2, drop = TRUE)# Get ggplot grobg = ggplotGrob(p)# Get the layout dataframe. # Note the names.# You want to remove "panel-2-2"g$layout# gtable::gtable_show_layout(g) # Might also be useful# Remove the grobs# The grob needs to be remove,# and the relevant row in the layout data frame needs to be removedpos <- grepl(pattern = "panel-2-2", g$layout$name)g$grobs <- g$grobs[!pos]g$layout <- g$layout[!pos, ]# Alternatively, replace the grobs with the nullGrobg = ggplotGrob(p)pos <- grep(pattern = "panel-2-2", g$layout$name)g$grobs[[pos]] <- nullGrob()# If you want, move the axis# g$layout[g$layout$name == "axis-b-2", c("t", "b")] = c(8, 8)# Draw the plotgrid.newpage()grid.draw(g)The answer in your link would need to be modified something like this:n <- 1000df <- data.frame(x = runif(n), y=rnorm(n), label = sample(letters[1:7], size = n, replace = TRUE), stringsAsFactors=TRUE)df$label.new <- factor(df$label, levels=sort(c(""," ",levels(df$label))))p <- ggplot(df, aes(x=x, y=y)) + geom_point() + facet_wrap(~ label.new, ncol=3,drop=FALSE)g = ggplotGrob(p)g$layout # Note the names and their positions (t, b, l, r)# gtable::gtable_show_layout(g) # Might also be usefulpos <- g$layout$name %in% c("panel-1-1", "panel-1-2", "strip-t-1-1", "strip-t-2-1")g$grobs <- g$grobs[!pos]g$layout <- g$layout[!pos, ]# Or replace the grobs with the nullGrobg = ggplotGrob(p)pos <- g$layout$name %in% c("panel-1-1", "panel-1-2", "strip-t-1-1", "strip-t-2-1")g$grobs[pos] <- list(nullGrob())# Move the axisg$layout[g$layout$name == "axis-l-1-1", c("l", "r")] = c(10,10)grid.newpage()grid.draw(g) 这篇关于ggplot2:从图中删除未使用的因子水平组合的方面(facet_grid)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-29 09:00