【问题标题】:How to plot a grouped bar plot in R with more than one dataframe?如何在 R 中使用多个数据框绘制分组条形图?
【发布时间】:2020-07-23 08:59:51
【问题描述】:

我在 R 中有两个数据框:

> dataframe2
   zone   meangpp
1     1 5.4153407
2     2 4.2429236
3     3 4.5719178
4     4 3.1215946
5     5 4.9222054
6     6 3.0384872
7     7 1.9293729
8     8 8.9709741
9     9 7.8904906
10   10 6.6410986
11   12 5.5011823

> dataframe1
   zone     meangpp
1     1 4.050161
2     2 7.729265
3     3 3.408220
4     4 4.884040
5     5 4.258422
6     6 2.906374
7     7 2.241984
8     8 4.703197
9     9 3.617657
10   10 2.712997
11   12 3.589406

两个数据框中的区域代表土地覆盖类别。

如何从条形图中的两个数据框中绘制meangpp

请告诉我不使用ggplot的解决方案。

【问题讨论】:

  • 已添加图例选项作为对答案的编辑..希望有用

标签: r dataframe bar-chart


【解决方案1】:

一种方法是:

dfnew=merge(df1,df2,by = "zone", all = TRUE)
rownames(dfnew)=dfnew$zone
dfnew$zone=NULL

barplot(t(as.matrix(dfnew)), beside=TRUE)

即合并,将连接的数据框作为矩阵传递给 barplot 函数

设置图例如下:

colnames(dfnew)=c('df1','df2')
barplot(t(as.matrix(dfnew)), beside=TRUE,legend=colnames(dfnew))

如何为此目的合并超过 2 个数据框?

dfnew=Reduce(function(x, y) merge(x, y, by = "zone", all = TRUE), list(df1, df2, df3)) 

注意:所有都是与库无关的本机函数。 ggplot 或其他库未用于上述任何方法。

【讨论】:

  • 对于图例和 colnames 等其他小的调整,请参阅 r-graph-gallery.com/211-basic-grouped-or-stacked-barplot.html
  • 是否可以通过此代码将 3 个数据帧合并在一起? dfnew=merge(df1,df2,by = "zone", all = TRUE)
  • 是的! dfnew=Reduce(function(x, y) 合并(x, y, by = "zone", all = TRUE), list(df1, df2, df3))
  • @HallieSheikh 编辑了答案.. 如此有效,只需创建一个数据框列表并传递给 Reduce 函数。您将能够通过这种方法合并任意数量的 dfs
【解决方案2】:

您可以使用以下代码

library(tidyverse)

dataframe2 = read.table(text="sl zone   meangpp
1     1 5.4153407
2     2 4.2429236
3     3 4.5719178
4     4 3.1215946
5     5 4.9222054
6     6 3.0384872
7     7 1.9293729
8     8 8.9709741
9     9 7.8904906
10   10 6.6410986
11   12 5.5011823", header=T)

dataframe1 = read.table(text="sl zone   meangpp
1     1 4.050161
2     2 7.729265
3     3 3.408220
4     4 4.884040
5     5 4.258422
6     6 2.906374
7     7 2.241984
8     8 4.703197
9     9 3.617657
10   10 2.712997
11   12 3.589406", header=T)

df <- bind_rows("dataframe1" = dataframe1, "dataframe2" = dataframe2, .id = "groups")

df %>% 
  ggplot(aes(x=factor(zone), y=meangpp, fill = groups)) + 
  geom_col(position = position_dodge())

你也可以使用lattice这样的包

library(lattice)
barchart(meangpp ~ factor(zone),data=df, groups=groups, auto.key = T,
     xlab = "Zone", ylab = "gpp") 

【讨论】:

    【解决方案3】:

    如果要使用ggplot,则必须绑定2个data.frames的行:

    # impporting your df ####
    library(tidyverse)
    
    df1 <- tibble::tribble(
      ~zone,  ~meangpp,
          1, 5.4153407,
          2, 4.2429236,
          3, 4.5719178,
          4, 3.1215946,
          5, 4.9222054,
          6, 3.0384872,
          7, 1.9293729,
          8, 8.9709741,
          9, 7.8904906,
         10, 6.6410986,
         12, 5.5011823
    )
    
    df2 <- tibble::tribble(
      ~zone,  ~meangpp,
          1,  4.050161,
          2,  7.729265,
          3,  3.408220,
          4,  4.884040,
          5,  4.258422,
          6,  2.906374,
          7,  2.241984,
          8,  4.703197,
          9,  3.617657,
         10,  2.712997,
         12,  3.589406
    )
    
    # joining them (adding a source id column named country) #### 
    big_df <- dplyr::bind_rows(
      df1 %>% mutate(country = 'A'),
      df2 %>% mutate(country = 'B')
    )
    
    # the ggplot ####
    # planing to fill the bars according to the country variable
    ggplot(data = big_df, mapping = aes(x = zone, y = meangpp, fill = country)) + 
      geom_bar(
        stat = 'identity', # so that y is the height of the bars
        position = 'dodge' # so that the bars are side by side (stack to stack them)
      )
    

    【讨论】:

      猜你喜欢
      • 2020-01-28
      • 1970-01-01
      • 2020-01-07
      • 1970-01-01
      • 2012-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-17
      相关资源
      最近更新 更多