0

已搜索此问题的答案,但找不到任何相关内容。

我正在使用 ggplot2 和 facet_wrap 绘制一个数据框以获取多个绘图,这工作正常。这个想法是快速寻找 x、y 关系中的趋势。

但是,在某些级别上,数据点很少,因此无法识别趋势,并且单个图不发挥任何作用。

我的问题是:是否可以在 facet_wrap 绘图之前预先定义最小数量的观察?或者我是否必须重新格式化我的数据框以删除一个级别内的观察数量有限的子集。

希望这是有道理的,在此先感谢。

4

1 回答 1

1

这是您的问题的答案:

library(tidyverse)

set.seed(567)

# create dummy data frame with 4 types in column x, 
# one of which, "d", only has 2 observations

my.df <- tibble(x = c(rep(c("a", "b", "c"), 25), rep("d", 2)),
                y = rnorm(78))

# when faceted, "d" lacks sufficient data

my.df %>% 
  ggplot(aes(y)) +
  geom_histogram(binwidth = 0.5) +
  facet_wrap(~ x)

在此处输入图像描述

# use the add_count function to solve your problem when faceting

my.df %>% 
  add_count(x) %>% 
  filter(n > 10) %>% 
  ggplot(aes(y)) +
  geom_histogram(binwidth = 0.5) +
  facet_wrap(~ x)

在此处输入图像描述

于 2018-10-13T02:20:14.413 回答