如何从ggplot2 facet_wrap删除构面? [英] How to Delete Facets from ggplot2 facet_wrap?

查看:52
本文介绍了如何从ggplot2 facet_wrap删除构面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过示例(v0.2.1.9000)创建于2018-10-05

I am trying to use the package cregg through the examples here. The tutorial works well. However, after generating the plot with facetting, there must be a rightest column with the label NA. I have tried several ways but still cannot remove it. Can anyone help? Thanks!

The codes are as follow:

library("cregg")
data("immigration")
stacked <- cj(immigration, ChosenImmigrant ~ Gender +
          Education + LanguageSkills + CountryOfOrigin + Job + JobExperience +
          JobPlans + ReasonForApplication + PriorEntry, id = ~ CaseID,
          estimate = "mm", by = ~ contest_no)
plot(stacked) + ggplot2::facet_wrap(~contest_no, nrow = 1L)

解决方案

You can manually remove the last facet using the gtable() package as follows

plt <- plot(stacked) + ggplot2::facet_wrap(~ contest_no, nrow = 1L)

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

# create gtable object
gt = ggplot_gtable(ggplot_build(plt))
print(gt)

#> TableGrob (15 x 29) "layout": 46 grobs
#>     z         cells        name
#> 1   0 ( 1-15, 1-29)  background
#> 2   1 ( 8- 8, 5- 5)   panel-1-1
#> 3   1 ( 8- 8, 9- 9)   panel-2-1
#> 4   1 ( 8- 8,13-13)   panel-3-1
#> 5   1 ( 8- 8,17-17)   panel-4-1
#> 6   1 ( 8- 8,21-21)   panel-5-1
#> 7   1 ( 8- 8,25-25)   panel-6-1
#> 8   3 ( 6- 6, 5- 5)  axis-t-1-1
#> 9   3 ( 6- 6, 9- 9)  axis-t-2-1
#> 10  3 ( 6- 6,13-13)  axis-t-3-1
#> 11  3 ( 6- 6,17-17)  axis-t-4-1
#> 12  3 ( 6- 6,21-21)  axis-t-5-1
#> 13  3 ( 6- 6,25-25)  axis-t-6-1
#> 14  3 ( 9- 9, 5- 5)  axis-b-1-1
#> 15  3 ( 9- 9, 9- 9)  axis-b-2-1
#> 16  3 ( 9- 9,13-13)  axis-b-3-1
#> 17  3 ( 9- 9,17-17)  axis-b-4-1
#> 18  3 ( 9- 9,21-21)  axis-b-5-1
#> 19  3 ( 9- 9,25-25)  axis-b-6-1
#> 20  3 ( 8- 8,24-24)  axis-l-1-6
#> 21  3 ( 8- 8,20-20)  axis-l-1-5
#> 22  3 ( 8- 8,16-16)  axis-l-1-4
#> 23  3 ( 8- 8,12-12)  axis-l-1-3
#> 24  3 ( 8- 8, 8- 8)  axis-l-1-2
#> 25  3 ( 8- 8, 4- 4)  axis-l-1-1
#> 26  3 ( 8- 8,26-26)  axis-r-1-6
#> 27  3 ( 8- 8,22-22)  axis-r-1-5
#> 28  3 ( 8- 8,18-18)  axis-r-1-4
#> 29  3 ( 8- 8,14-14)  axis-r-1-3
#> 30  3 ( 8- 8,10-10)  axis-r-1-2
#> 31  3 ( 8- 8, 6- 6)  axis-r-1-1
#> 32  2 ( 7- 7, 5- 5) strip-t-1-1
#> 33  2 ( 7- 7, 9- 9) strip-t-2-1
#> 34  2 ( 7- 7,13-13) strip-t-3-1
#> 35  2 ( 7- 7,17-17) strip-t-4-1
#> 36  2 ( 7- 7,21-21) strip-t-5-1
#> 37  2 ( 7- 7,25-25) strip-t-6-1
#> 38  4 ( 5- 5, 5-25)      xlab-t
#> 39  5 (10-10, 5-25)      xlab-b
#> 40  6 ( 8- 8, 3- 3)      ylab-l
#> 41  7 ( 8- 8,27-27)      ylab-r
#> 42  8 (12-12, 5-25)   guide-box
#> 43  9 ( 4- 4, 5-25)    subtitle
#> 44 10 ( 3- 3, 5-25)       title
#> 45 11 (13-13, 5-25)     caption
#> 46 12 ( 2- 2, 2- 2)         tag
#>                                             grob
#> 1                rect[plot.background..rect.379]
#> 2                        gTree[panel-1.gTree.42]
#> 3                        gTree[panel-2.gTree.53]
#> 4                        gTree[panel-3.gTree.64]
#> 5                        gTree[panel-4.gTree.75]
#> 6                        gTree[panel-5.gTree.86]
#> 7                        gTree[panel-6.gTree.97]
#> 8                                 zeroGrob[NULL]
#> 9                                 zeroGrob[NULL]
#> 10                                zeroGrob[NULL]
#> 11                                zeroGrob[NULL]
#> 12                                zeroGrob[NULL]
#> 13                                zeroGrob[NULL]
#> 14           absoluteGrob[GRID.absoluteGrob.104]
#> 15           absoluteGrob[GRID.absoluteGrob.104]
#> 16           absoluteGrob[GRID.absoluteGrob.104]
#> 17           absoluteGrob[GRID.absoluteGrob.104]
#> 18           absoluteGrob[GRID.absoluteGrob.104]
#> 19           absoluteGrob[GRID.absoluteGrob.104]
#> 20                                zeroGrob[NULL]
#> 21                                zeroGrob[NULL]
#> 22                                zeroGrob[NULL]
#> 23                                zeroGrob[NULL]
#> 24                                zeroGrob[NULL]
#> 25           absoluteGrob[GRID.absoluteGrob.146]
#> 26                                zeroGrob[NULL]
#> 27                                zeroGrob[NULL]
#> 28                                zeroGrob[NULL]
#> 29                                zeroGrob[NULL]
#> 30                                zeroGrob[NULL]
#> 31                                zeroGrob[NULL]
#> 32                                 gtable[strip]
#> 33                                 gtable[strip]
#> 34                                 gtable[strip]
#> 35                                 gtable[strip]
#> 36                                 gtable[strip]
#> 37                                 gtable[strip]
#> 38                                zeroGrob[NULL]
#> 39 titleGrob[axis.title.x.bottom..titleGrob.292]
#> 40   titleGrob[axis.title.y.left..titleGrob.295]
#> 41                                zeroGrob[NULL]
#> 42                             gtable[guide-box]
#> 43         zeroGrob[plot.subtitle..zeroGrob.375]
#> 44            zeroGrob[plot.title..zeroGrob.374]
#> 45          zeroGrob[plot.caption..zeroGrob.377]
#> 46              zeroGrob[plot.tag..zeroGrob.376]

Show plot layout

library(lemon)
gtable_show_names(gt)

# we want to remove everything related to panel-6-
rm_grobs <- gt$layout$name %in% c("panel-6-1", "strip-t-6-1", 
                                  "axis-t-6-1", "axis-b-6-1",
                                  "axis-l-1-6", "axis-r-1-6", "ylab-r")
# remove grobs
gt$grobs[rm_grobs] <- NULL
gt$layout <- gt$layout[!rm_grobs, ]

# check result
gtable_show_names(gt)

Next remove the space left over

names(gt)
#>  [1] "grobs"         "layout"        "widths"        "heights"      
#>  [5] "respect"       "rownames"      "colnames"      "name"         
#>  [9] "gp"            "vp"            "children"      "childrenOrder"

gt$widths
#>  [1] 5.5pt              0cm                1grobwidth        
#>  [4] 3.93105593607306cm 1null              0cm               
#>  [7] 5.5pt              0cm                1null             
#> [10] 0cm                5.5pt              0cm               
#> [13] 1null              0cm                5.5pt             
#> [16] 0cm                1null              0cm               
#> [19] 5.5pt              0cm                1null             
#> [22] 0cm                5.5pt              0cm               
#> [25] 1null              0cm                0cm               
#> [28] 0pt                5.5pt

gt$widths[25] = unit(0, "cm")
gt$widths[29] = unit(0, "cm")

# check result again
gtable_show_names(gt)

# final plot
grid.newpage()
grid.draw(gt)

Created on 2018-10-05 by the reprex package (v0.2.1.9000)

这篇关于如何从ggplot2 facet_wrap删除构面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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