从ggplot中删除空面并单​​独标记

时间:2018-01-31 18:40:01

标签: r ggplot2

我有一些相当简单的数据,例如

library(dplyr)
dat <-
  data.frame(species = sample(c("1", "2"), 200, TRUE), 
           psd = sample(c("s", "q", "p", "m", "t"), 200, TRUE), 
           val = sample(40:120, 200, TRUE)) %>%
  mutate(pop = ifelse(species == "1", 
                      sample(c("a", "b", "f"), length(species), TRUE), 
                      sample(c("c", "d", "e", "g"), length(species), TRUE)))
物种间的{p> pop是相互排斥的。

我想将这些数据绘制成一个图,例如

dat %>%
  ggplot(aes(x = psd, y = val)) +
  geom_boxplot(outlier.colour = NA) +
  facet_grid(species~pop, drop = T)

enter image description here

但有空面(例如species == "2"&amp; pop = "c")。这些空面可以以某种方式掉落,每个刻面都单独标记?

2 个答案:

答案 0 :(得分:1)

c("1a", "1b", "2c", "2d", "2e", "1f", "2g")

使用粘贴命令来进一步自定义标签,但此示例会将 require_once __DIR__.'/vendor/autoload.php'; $Parser = new PhpMimeMailParser\Parser(); 作为您的名字

答案 1 :(得分:1)

p1 = dat[dat$species == 1,] %>%
    ggplot(aes(x = psd, y = val)) +
    geom_boxplot(outlier.colour = NA) +
    facet_grid(species~pop)

p2 = dat[dat$species == 2,] %>%
    ggplot(aes(x = psd, y = val)) +
    geom_boxplot(outlier.colour = NA) +
    facet_grid(species~pop)

library(grid)
library(egg)

grid.newpage()
grid.draw(ggarrange(p1, p2, ncol = 1))

enter image description here