首页 文章

带有4个分类变量的堆积条形图

提问于
浏览
0

我的问题是能够在R中的条形图中显示4个分类变量 .

4个分类变量各有2个或更多级别 . 我的想法是使用 ggplot 为3个类别中的每一个使用 geom_bar 创建单独的条形图,每个级别的计数将被堆叠 . 然后我会使用 facet_wrap 然后将其拆分为第4类 .

数据如下所示:

Species     Crown_Class     Life_class      Stem_Category
E. obliqua  Suppressed      Standing live   Large stems
E. rubida   Intermediate    Standing live   Large stems
E. obliqua  Suppressed      Standing live   Small stems
E. obliqua  Suppressed      Standing live   Small stems
E. rubida   Suppressed      Standing live   Large stems
E. radiata  Suppressed      Standing live   Small stems
E. obliqua  Dominant        Standing live   Small stems
E. obliqua  Suppressed      Standing live   Small stems
E. radiata  Suppressed      Standing live   Large stems
E. rubida   NA              Standing dead   Large stems
E. rubida   Intermediate    Standing live   Large stems

我想到的图表显示了三个类别中每个类别的堆叠条形图,然后按第三个类别进行分组 . 对于给定的数据,将为每个物种显示Crown_Class,life_class和Stem_Category的单独栏 .

我已经尝试了几个小时,并且可以使用此代码执行单独的绘图(我将数据分成3个单独的数据框来执行此操作:

ggplot(data= cc, aes(x= Species, fill = Crown_Class))+
geom_bar(position='stack')

ggplot(data=lc, aes(x = Species, fill = Life_class))+
geom_bar(position ='stack')

ggplot(data=sc, aes(x = Species, fill = Stem_Category))+
geom_bar(position ='stack')

这个想法是做这样的事情:

ggplot()+
  geom_bar(data= cc, aes(x = Species, fill = Crown_Class), 
      position='stack') +
  geom_bar(data=lc, aes(x = Species, fill = Life_class), 
      position ='dodge')+
  facet_wrap(~Species)

但结果并不是我想到的 . 第二个图有效地覆盖了第一个 .

enter image description here

我将不胜感激任何帮助 .

2 回答

  • 3

    这是一个如何使用 facet_grid 在同一个图上包含所有4个变量的示例 .

    请注意,我生成了一些虚拟数据,因为我无法将数据集导入 R .

    生成数据

    library(ggplot2)
    theme_set(theme_bw())
    set.seed(123)
    df1 <- data.frame(s1 = sample(letters[1:3], 11, replace = T),
                      s2 = sample(letters[4:6], 11, replace = T),
                      s3 = sample(letters[7:9], 11, replace = T),
                      s4 = sample(letters[10:12], 11, replace = T),
                      stringsAsFactors = FALSE)
    

    编辑:

    也许这更接近你所追求的:

    ggplot(df1)+
        geom_bar(aes(x = s1), position = 'stack')+
        geom_bar(aes(x = s2), position = 'stack')+
        geom_bar(aes(x = s3), position = 'stack')+
        facet_wrap(~ s4)
    

    enter image description here

    如果以这种方式继续,您应该注意x轴上的值来自 three different variables .

    恕我直言:虽然我有点怀疑在同一轴上创建一个具有三个不同变量的可视化,并且 ggplot2 为您提供了大量选项以避免以这种方式继续进行 .

    使用facet_grid制作情节

    ggplot(df1, aes(x = s1, fill = s2))+
        geom_bar(position = 'stack')+
        facet_grid(s3~s4)
    

    enter image description here

    使用交互和facet_wrap制作情节

    现在,假设您不希望将两个分组因素作为方面,而只是偏好一个方面 . 然后,我们可以使用 interaction 函数 .

    ggplot(df1, aes(x = s1, fill = interaction(s2,s3)))+
        geom_bar(position = 'stack')+
        facet_wrap(~s4)
    

    enter image description here

    使用Rmisc :: multiplot

    最后,我们可以创建三个单独的图,然后使用 Rmisc::multiplot 在同一页面上绘图 .

    library(Rmisc)
    p1 <- ggplot(df1, aes(x = s1, fill = s2))+
        geom_bar(position = 'stack')
    p2 <- ggplot(df1, aes(x = s1, fill = s3))+
        geom_bar(position = 'stack')
    p3 <- ggplot(df1, aes(x = s1, fill = s4))+
        geom_bar(position = 'stack')
    
    multiplot(p1,p2,p3, cols = 3)
    

    enter image description here

  • 0

    由于您尝试使用 Crown_ClassLife_classStem_Category 来区分您的绘图,ggplot2会更喜欢这些值位于它们自己的列中(通常ggplot2就像长数据一样,其中只有一列包含绘制的值 . )我们可以使用tidyr重新组织数据 .

    library(tidyr)
    df <-
      gather(df, variable, value, -Species)
    
    head(df)
         Species    variable           value
    1 E. obliqua Crown_Class      Suppressed
    2  E. rubida Crown_Class    Intermediate
    3 E. obliqua Crown_Class      Suppressed
    4 E. obliqua Crown_Class      Suppressed
    5  E. rubida Crown_Class      Suppressed
    6 E. radiata Crown_Class      Suppressed
    

    现在我们可以在 variable 上进行整理

    ggplot(df) +
      geom_bar(aes(x = Species, fill = value)) +
      facet_wrap(~ variable)
    

    enter image description here

    如果您不想只为 Crown_ClassLife_class 和'Stem_Category'的所有颜色提供一个指南,您可以制作三个单独的图并使用 gridExtra 包合并它们 .

    library(dplyr)
    library(gridExtra)
    p <-
      df %>%
      filter(variable == 'Crown_Class') %>%
      ggplot() +
      geom_bar(aes(x = Species, fill = value)) +
      facet_wrap(~ variable)
    
    q <-
      df %>%
      filter(variable == 'Life_class') %>%
      ggplot() +
      geom_bar(aes(x = Species, fill = value)) +
      facet_wrap(~ variable)
    
    r <-
      df %>%
      filter(variable == 'Stem_Category') %>%
      ggplot() +
      geom_bar(aes(x = Species, fill = value)) +
      facet_wrap(~ variable)
    
    grid.arrange(p, q, r)
    

    enter image description here

相关问题