我正在尝试通过示例here使用软件包cregg
。本教程效果很好。但是,在生成带有小平面的图之后,必须有最右边的列,其标签为NA
。我已经尝试了几种方法,但是仍然无法删除它。有人可以帮忙吗?谢谢!
代码如下:
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)
答案 0 :(得分:2)
您可以使用gtable()
软件包手动删除最后一个构面,如下所示
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]
显示情节布局
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)
下一步,删除剩余的空间
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)
由reprex package(v0.2.1.9000)于2018-10-05创建