【问题标题】:Overlaying ggplot bar plots [duplicate]覆盖ggplot条形图[重复]
【发布时间】:2018-08-16 14:37:11
【问题描述】:

我使用 ggplot,我想覆盖两个条形图。这是我的头部数据集:(data = csv_total)

                              habitates surf_ha obs_flore obs_faune
1              Régénération de feuillus     0.4       0.0       2.4
2 Villes, villages et sites industriels     0.7       0.0      15.6
3                     Forêt de feuillus   384.8       1.1       0.0
4                   Forêt de Pin d'alep  2940.8       2.1       1.0
5                                Maquis    45.9       2.3       0.3
6                 Plantation de ligneux   306.4       2.5       1.0

这是我的 2 个条形图:

hist1 <- ggplot(csv_total, aes(x = habitates, y = obs_flore)) + geom_bar(stat = "identity") +
  theme(axis.text.x= element_text(angle=50, hjust = 1))

hist2 <- ggplot(csv_total, aes(x = habitates, y = obs_faune)) + geom_bar(stat = "identity") + 
  theme(axis.text.x= element_text(angle=50, hjust = 1))

X 轴代表栖息地,Y 轴代表观察次数(hist1 为植物群,hist2 为动物群)。

所以我想通过叠加两者来创建一个条形图。为了获得在 X 轴:栖息地和在 Y 轴:两种不同颜色的植物区系观察和动物系观察。 您是否有想法将这些条形图叠加在一起?

对不起我的英语不好。 谢谢!

【问题讨论】:

    标签: r ggplot2 plot


    【解决方案1】:

    我必须为您选择,但我更喜欢第二个(尽管它可能会更长一点)。

    library(ggplot2)
    
    ggplot(csv_total) +
      geom_col(aes(x = habitates, y = obs_flore, fill = "obs_flore"), alpha = 0.5) +
      geom_col(aes(x = habitates, y = obs_faune, fill = "obs_faune"), alpha = 0.5) +
      theme(axis.text.x = element_text(angle = 50, hjust = 1))
    

    这看起来不错,但是有了以下内容,我们不必像上面那样人为地创建填充图例,我们可以使用“闪避”选项将列并排放置。 首先,我们必须将数据转换为长格式(使用gather from tidyr):

    library(tidyr)
    
    csv_total_long <- gather(csv_total, flore_faune, obs, obs_flore, obs_faune)
    
    csv_total_long
    # A tibble: 12 x 4
    #    habitates surf_ha flore_faune   obs
    #    <chr>       <dbl> <chr>       <dbl>
    # 1  A             0.4 obs_flore     0  
    # 2  B             0.7 obs_flore     0  
    # 3  C           385.  obs_flore     1.1
    # 4  D          2941.  obs_flore     2.1
    # 5  E            45.9 obs_flore     2.3
    # 6  F           306.  obs_flore     2.5
    # 7  A             0.4 obs_faune     2.4
    # 8  B             0.7 obs_faune    15.6
    # 9  C           385.  obs_faune     0  
    # 10 D          2941.  obs_faune     1  
    # 11 E            45.9 obs_faune     0.3
    # 12 F           306.  obs_faune     1  
    

    现在我们为每个动物和植物观察增加了一行。然后我们可以绘制相邻的列。如果没有position = "dodge",你会得到与上面相同的情节。

    ggplot(csv_total_long, aes(x = habitates, y = obs, fill = flore_faune)) +
      geom_col(alpha = 0.5, position = "dodge") +
      scale_fill_brewer(palette = "Dark2") +
      theme(axis.text.x = element_text(angle = 50, hjust = 1))
    

    我在这里使用geom_col,因为它与geom_barstat = "identity" 相同。

    数据
    我用字母来表示栖息地,因为我的系统无法正确识别原来的字母,这不是重点。

    csv_total <- structure(list(habitates = c("A", "B", "C", "D", "E", "F"), 
                                surf_ha = c(0.4, 0.7, 384.8, 2940.8, 45.9, 306.4), 
                                obs_flore = c(0.0, 0.0, 1.1, 2.1, 2.3, 2.5), 
                                obs_faune = c(2.4, 15.6, 0.0, 1.0, 0.3, 1.0)), 
                           row.names = c(NA, -6L), 
                           class = c("tbl_df", "tbl", "data.frame"))
    

    【讨论】:

    • 非常感谢凯丝!效果很好,我也确实选择了第二种方法!祝你有美好的一天
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-10
    • 2020-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多