【问题标题】:ggplot: how to remove empty groups from a facet_wrap? [duplicate]ggplot:如何从 facet_wrap 中删除空组? [复制]
【发布时间】:2021-01-13 07:43:56
【问题描述】:

我有

如您所见,nystudie 代表的所有研究都印在x-axis 上。这会创建很多空组,我需要帮助将其删除。

> head(p)
  study response treatment
1    13        1       SSA
2    12        4       SSA
3    10        4       SSA
4     4        4      SSTR
5     4        3      SSTR
6     9        4       SSA

每个p$study 都属于SSTR SSA。我想按p$study 计算p$response,然后按bind_rows 计算所有response 每个p$treatment

我有

 p %>%
      mutate(nystudie=as.character(study),
             best.resp =as.factor(response)) %>%    
      bind_rows(., mutate(., nystudie="All")) %>%   
      group_by(nystudie,best.resp) %>%   
      summarise(N=n(),Val=unique(treatment))

这给了

# A tibble: 6 x 4
# Groups:   nystudie, best.resp [6]
  nystudie best.resp     N Val  
  <chr>    <fct>     <int> <fct>
1 1        3             1 SSTR 
2 1        4             2 SSTR 
3 10       4             1 SSA  
4 11       4             2 SSA  
5 12       3             9 SSA  
6 12       4             4 SSA 

所以,为了对p$treatmet 进行分层,我写道:

 %>% 
    ggplot(aes(nystudie, N, color = best.resp, fill= best.resp)) +
      geom_col(position = position_dodge2(preserve = "single", padding = 0.1)) +
      facet_wrap(~Val,ncol = 2)

但是,这会创建“空组”。例如。 study 11, 12, 13, 14, 15SSTRstudy 2, 22, 3, 4, 5, 6, 7SSA

如何在每个facet_wrap 中省略这些“空”组,所以它只包含实际上应用了p$treatmentstudies

p <- structure(list(study = structure(c(12L, 2L, 12L, 12L, 9L, 8L, 
13L, 2L, 12L, 15L, 1L, 13L, 2L, 12L, 9L, 16L, 8L, 3L, 5L, 13L, 
11L, 5L, 4L, 6L, 1L, 9L, 4L, 12L, 1L, 8L, 12L, 11L, 4L, 2L, 6L, 
3L, 12L, 4L, 5L, 8L, 12L, 12L, 5L, 12L, 4L, 13L, 12L, 10L, 4L, 
12L), .Label = c("1", "2", "3", "4", "5", "6", "7", "8", "9", 
"10", "11", "12", "13", "14", "15", "22"), class = "factor"), 
    response = c("3", "3", "3", "4", "3", "1", "4", "3", "3", 
    "4", "4", "4", "3", "3", "2", "4", "1", "3", "3", "4", "4", 
    "2", "3", "3", "3", "2", "4", "3", "4", "1", "4", "4", "3", 
    "3", "4", "3", "3", "3", "2", "1", "4", "4", "3", "3", "4", 
    "4", "3", "4", "4", "3"), treatment = structure(c(2L, 1L, 
    2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 
    1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 
    1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 
    2L, 1L, 2L), .Label = c("SSTR", "SSA"), class = "factor")), row.names = c(NA, 
-50L), class = "data.frame")

【问题讨论】:

    标签: r ggplot2 plot dplyr facet-wrap


    【解决方案1】:

    也许是这样的:

    #Code
    p %>%
      mutate(nystudie=as.character(study),
             best.resp =as.factor(response)) %>%    
      bind_rows(., mutate(., nystudie="All")) %>%   
      group_by(nystudie,best.resp) %>%   
      summarise(N=n(),Val=unique(treatment)) %>% 
      ggplot(aes(nystudie, N, color = best.resp, fill= best.resp)) +
      geom_col(position = position_dodge2(preserve = "single", padding = 0.1)) +
      facet_wrap(~Val,ncol = 2,scales='free')
    

    输出:

    【讨论】:

    • @cmirian 是的,如果有兴趣查看facet_grid() 的矩阵样式。它还有其他选项,例如条带的空间和位置:)
    • 很抱歉再次打扰您。当使用facet_wrap(~Val) 分层时,似乎“全部”打印了facets 中所有/两种处理的总和(您可以在您的ggplot 中看到All 是相同的)。在每个facet 中打印的All 是否可以只打印每个Val 中的所有计数?
    • @cmirian 是的,可以尝试使用汇总功能。我会检查并告诉你发生了什么!
    • @cmirian 先尝试:data &lt;- p %&gt;% mutate(nystudie=as.character(study), best.resp =as.factor(response)) %&gt;% group_by(nystudie,best.resp) %&gt;% summarise(N=n(),Val=unique(treatment)) %&gt;% bind_rows( p %&gt;% mutate(nystudie=as.character(study), best.resp =as.factor(response)) %&gt;% group_by(best.resp,treatment) %&gt;% summarise(N=n()) %&gt;% mutate(nystudie="All") %&gt;% rename(Val=treatment) )
    • @cmirian 然后ggplot(data,aes(nystudie, N, color = best.resp, fill= best.resp)) + geom_col(position = position_dodge2(preserve = "single", padding = 0.1)) + facet_wrap(~Val,ncol = 2,scales='free')
    猜你喜欢
    • 2014-01-07
    • 2016-04-11
    • 1970-01-01
    • 2019-06-22
    • 2020-01-14
    • 1970-01-01
    • 2016-07-02
    相关资源
    最近更新 更多