【问题标题】:geom_bar two datasets together in Rgeom_bar 两个数据集一起在 R
【发布时间】:2021-10-10 22:41:09
【问题描述】:

我有两个dataframes,每个都有two 列,我想在R 中使用ggplottogether 绘制为barplot,如下所示:

如何在 R 中使用 dplyr 来做到这一点?

样本数据:

DF1

Code Count_2020
A    1
B    2
C    3
D    4
E    5
F    6

DF2

Code Count_2021
A    4
B    8
C    6
D    8
E    10
F    12

所以,我首先想到的是使用dplyr::inner_join 将两个dataframes 合并为一个,然后得到一个新的dataframe,如下所示:

Code Count_2021 Count_2020
 A          4          1
 B          8          2
 C          6          3
 D          8          4
 E         10          5
 F         12          6 

接下来我想使用dplyr::gather 将这两年的计数数据一起绘制为TypeValue,但是当输出变为:

Type Value
Code A 
Code B
Code C
Code D
Code E
Code F

我尝试过的代码

library(tidyverse)

# Merge DF1 and DF2 
DF = inner_join(DF1, DF2)

# Gather data for plotting
Gathered_DF= DF%>% dplyr::select(Code, Count_2020, Count_2021) %>% 
    gather(key = Type, value = Value) # Output not as expected, stuck!!

【问题讨论】:

    标签: r ggplot2 dplyr


    【解决方案1】:

    我还找到了另一种方法:

    library(tidyverse)
    DF1 = data.frame(Code = c("A", "B", "C", "D", "E", "F"),
                      Count_2020 = c(1,2,3,4,5,6))
    DF2 = data.frame(Code = c("A", "B", "C", "D", "E", "F"),
                      Count_2021 = c(4, 8, 6, 8, 10, 12))
    
    DF_Merged = 
      inner_join(DF1, DF2)
    
    DFF_Merged = DF_Merged %>% dplyr::select(Code, Count_2020, Count_2021) %>% 
      gather(key = Type, value = Value, -Code) %>% 
      mutate(Type = ifelse(Type == "Count_2020", "2020", "2021"))
    
    DFF_Merged %>% 
      ggplot(aes(x = reorder(Code,Value), y = Value, fill = Type, 
                 text = paste("Count:", Value,
                              "<br>", "Offense Code:", Code,
                              "<br>", "Year:", Type))) +
      geom_col(position = "dodge", show.legend = FALSE) +
      xlab("Offense Code") +
      ylab("Count") +
      ggtitle("Arrest Counts for Group 1 in Year 2020 and  2021") +
      theme(axis.text=element_text(size=8)) 
    

    结果

    【讨论】:

      【解决方案2】:

      使用pivot_longer() 重塑您的数据,然后使用ggplot 绘图。 奖励:要在条上添加文本,请使用 ggfittext 包中的 geom_bar_text

      library(tidyverse)
      
      DF1 <- read.table(text = "Code Count_2020
      A    1
      B    2
      C    3
      D    4
      E    5
      F    6", header = TRUE)
      
      DF2 <- read.table(text = "Code Count_2021
      A    4
      B    8
      C    6
      D    8
      E    10
      F    12", header = TRUE)
      
      DF <- left_join(DF1, DF2, by = "Code")
      DF_long <- DF %>% 
        pivot_longer(-Code,
                     names_to = c("tmp", "Year"),
                     names_sep = "\\_",
                     values_to = "Count") %>% 
        select(-tmp)
      DF_long
      #> # A tibble: 12 x 3
      #>    Code  Year  Count
      #>    <chr> <chr> <int>
      #>  1 A     2020      1
      #>  2 A     2021      4
      #>  3 B     2020      2
      #>  4 B     2021      8
      #>  5 C     2020      3
      #>  6 C     2021      6
      #>  7 D     2020      4
      #>  8 D     2021      8
      #>  9 E     2020      5
      #> 10 E     2021     10
      #> 11 F     2020      6
      #> 12 F     2021     12
      
      plt <- ggplot(DF_long, aes(x = Code,
                                 y = Count,
                                 fill = Year)) +
        geom_col(position = position_dodge(width = 0.9)) +
        theme_minimal()
      plt
      

      library(ggfittext)
      plt +
        geom_bar_text(position = "dodge", reflow = TRUE)
      

      reprex package (v2.0.1) 于 2021-08-05 创建

      【讨论】:

        【解决方案3】:

        您可以使用pivot_longer 代替gather,因为它已被tidyr 1.1.3 取代

        library(tidyverse)
        
        df1 <- data.frame(Code = c("A", "B", "C", "D", "E", "F"),
                          Count_2020 = c(1,2,3,4,5,6))
        df2 <- data.frame(Code = c("A", "B", "C", "D", "E", "F"),
                          Count_2021 = c(4, 8, 6, 8, 10, 12))
        df_joined <- df1 %>% 
          inner_join(df2, by = "Code") %>% 
          pivot_longer(cols = !Code, names_to = "Year", names_prefix = "Count_", values_to = "Count")
        df_joined
        #> # A tibble: 12 x 3
        #>    Code  Year  Count
        #>    <fct> <chr> <dbl>
        #>  1 A     2020      1
        #>  2 A     2021      4
        #>  3 B     2020      2
        #>  4 B     2021      8
        #>  5 C     2020      3
        #>  6 C     2021      6
        #>  7 D     2020      4
        #>  8 D     2021      8
        #>  9 E     2020      5
        #> 10 E     2021     10
        #> 11 F     2020      6
        #> 12 F     2021     12
        
        ggplot(df_joined, aes(x = Code, y = Count, fill = Year)) +
          geom_bar(stat = "identity", position = "dodge")
        

        在上面的代码中,pivot_longer 里面的参数是:

        • cols = !Code 表示要旋转的列是除Code 之外的所有列
        • names_to = "Year" 表示要为分组创建的列的名称
        • names_prefix = "Count_" 用于从创建的列"Year" 中删除字符串"Count_"
        • values_to = "Count" 表示为每个组的存储值创建的列的名称。

        您可以通过拨打?pivot_longer了解更多有关此功能的信息

        【讨论】:

        • 哦,我没有意识到有人在我之前回答了,我输入的答案太长了。我应该删除我的答案吗?既然基本一样
        • 这是一个很好的答案。你应该保留它
        【解决方案4】:

        我们可以在加入后使用pivot_longer 重塑为'long'格式,然后在ggplot2 中使用geom_colposition 指定为'dodge',fill 指定为'Year`

        library(dplyr)
        library(tidyr)
        library(ggplot2)
        inner_join(DF1, DF2) %>% 
           pivot_longer(cols = -Code, names_to = 'Year', names_prefix = 'Count_') %>% 
           ggplot(aes(x = Code, y = value, fill = Year)) + 
             geom_col(position = 'dodge') + 
             theme_bw()
        

        -输出

        数据

        DF1 <- structure(list(Code = c("A", "B", "C", "D", "E", "F"), 
         Count_2020 = 1:6), class = "data.frame", row.names = c(NA, 
        -6L))
        
        DF2 <- structure(list(Code = c("A", "B", "C", "D", "E", "F"), Count_2021 = c(4L, 
        8L, 6L, 8L, 10L, 12L)), class = "data.frame", row.names = c(NA, 
        -6L))
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-09-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多