从dplyr管道生成直方图

时间:2019-03-01 18:17:22

标签: r dplyr

我有一个数据集,我想group_by()并为每个组生成一个直方图。我当前的代码如下:

df %>%
  group_by(x2) %>%
  with(hist(x3,breaks = 50))

但这会生成整个x3的单个直方图,而不是x3的几个块,这里是一些示例数据

df = data.frame(x1 = rep(c(1998,1999,2000),9),
            x2 = rep(c(1,1,1,2,2,2,3,3,3),3),
            x3 = rnorm(27,.5))

所需的输出:

enter image description here

实际输出:

enter image description here

4 个答案:

答案 0 :(得分:5)

我想我对do的评论已过时。 ?do指向我们当前的?group_walk

df %>%
 group_by(x2) %>%
 group_walk(~ hist(.x$x3))

dplyr <0.8.0版本中,没有group_walk,因此可以使用do

df %>% 
  group_by(x2) %>%
  do(h = hist(.$x3))

假设您只想要hist(打印的直方图)的副作用,而不想要返回的值,则可以在链的末尾添加%>% invisible(),以不打印结果小标题。 / p>

答案 1 :(得分:4)

例如,我认为是时候进行ggplot了:

library(tidyverse)

df %>% 
  ggplot(aes(x = x3)) + 
  geom_histogram(bins = 50) + 
  facet_wrap(~x2) # optional: use argument "ncols = 1"

答案 2 :(得分:2)

在数据帧列表中运行hist命令后,您可以使用split.data.frame命令根据类别拆分数据

list_df <- split.data.frame(df, f= df$x2)
par(mfrow = c(round(length(list_df), 0), 1))

for( lnam in names(list_df)){
  hist(list_df[[lnam]][, "x3"])

}

答案 3 :(得分:2)

我非常喜欢@Gregor对group_walk的回答,但在dplyr v0.8.0.1中仍被列为实验性的。如果要避免使用稍后可能会中断的功能,我将使用基数split,然后使用purrr::walk。我正在使用walkplot来避免hist给出的所有文本打印输出。

library(dplyr)
library(purrr)

df %>%
  split(.$x2) %>%
  walk(~hist(.$x3) %>% plot())
相关问题