在ggplot2中进行构面时仅保留som组

时间:2019-02-21 12:29:12

标签: r ggplot2

我有以下示例。如何限制构面的数量,例如只保留奥迪和闪避?

library(tidyverse)

ggplot(mpg) +
  geom_histogram(aes(displ)) +
  facet_wrap(~ manufacturer)

enter image description here

2 个答案:

答案 0 :(得分:3)

一种快速的解决方法是在ggplot调用内设置数据子集(检查https://docs.microsoft.com/en-us/linkedin/marketing/integrations/community-management/organizations/organization-access-control中接受的答案)。

对于您而言,我认为您应该在第一个ggplot参数内添加一个subset(mpg,manufacturer %in% c("audi","dodge"))调用。

代码

> ggplot(subset(mpg,manufacturer %in% c("audi","dodge"))) +
+     geom_histogram(aes(displ)) +
+     facet_wrap(~ manufacturer)

这将产生所需的输出:

this link

编辑:两个答案都以相同的解决方案同时出现

答案 1 :(得分:2)

关于数据集的事情呢?

library(tidyverse)

mpg %>%
# select only desired manifacturers
filter(manufacturer %in% c('audi','dodge')) %>%  
 ggplot() +
 geom_histogram(aes(displ)) +
 facet_wrap(~ manufacturer)

enter image description here

相关问题