【问题标题】:How to create a two barplots for two subjects, with multiple columns on the x-axis?如何为两个主题创建两个条形图,在 x 轴上有多个列?
【发布时间】:2021-04-09 05:58:07
【问题描述】:

我需要为 x 轴上不同列的两种动物创建条形图。

为了更清楚

我的数据是这样的:

ID, Type, Baseline_Respiratory_Rate (BLRR), Day3_RR, Day7_RR, Day14_RR
1 Stroke   233
2 Stroke  200
3 Sham   250
4 Sham   450
5 Sham   350
6 Stroke  234
7 Stroke   180
8 Sham    300
9 Sham    250
10 Stroke  166

所以我需要在 x 轴(基线、第 3 天、第 7 天、第 4 天)上绘制不同天的平均呼吸频率,并且每天有两个条形图,一个用于中风,一个用于假手术,都在同一条上情节。

作为初学者,我想不出办法,我正在尝试系统地学习 R,但我现在需要学习这个。

非常感谢

【问题讨论】:

  • 我猜你的数据不完整。 Day3_Rr、Day7_RR 没有值......

标签: r ggplot2 data-visualization bar-chart


【解决方案1】:

在您的数据中,缺少第 3 天以后的值。因此,我将使用以下较小的数据集进行说明。

ID;Type;baseline;day1
1;Stroke;233;133
2;Stroke;200;100
3;Sham;250;200
4;Sham;450;300
5;Sham;350;200
6;Stroke;234;230
7;Stroke;180;190
8;Sham;300;290
9;Sham;250;150
10;Stroke;166;200

将其放入名为“动物”的数据框中。现在

tmp<-aggregate(animals[,3:4],list(type=animals$Type),mean)

给出每天和类型的平均呼吸量。要获得条形图,请使用

barplot(as.matrix(tmp[2:3]),beside=TRUE)

您可以通过添加图例、轴标签等来使条形图更漂亮...我把它留给您。

【讨论】:

  • 这真的很有帮助,也是最直接的,但是你能帮我用 ggplot 来做吗?因为我想在我的绘图中添加误差线或更多东西,而且 ggplot 对我来说比 base R 更容易
【解决方案2】:

这是使用“Tidyverse”的典型工作流程选项 - 还有其他方法可以实现这一点..

library(dplyr)
library(tidyr)
library(ggplot2)

df %>% 
  pivot_longer(-c(id, type)) %>% 
  mutate(name = factor(name, levels = c("blrr", "day3_rr", "day7_rr", "day14_rr"))) %>% 
  group_by(name, type) %>%
  summarise(avg = mean(value)) %>% 
  ggplot(aes(name, avg, fill = type))+
  geom_col(position = position_dodge())

**data**

#to make the outcome repeatable
set.seed(123)

#representative dataset
df <- data.frame(id = 1:10,
                 type = sample(c("Stroke", "Sham"), 10, replace = TRUE),
                 blrr = runif(10, 150, 450),
                 day3_rr = runif(10, 150, 450),
                 day7_rr = runif(10, 150, 450),
                 day14_rr = runif(10, 150, 450))

reprex package (v1.0.0) 于 2021-04-09 创建

【讨论】:

    【解决方案3】:

    这是使用假数据的完整解决方案:

    library(tidyverse)
    
    # add fake data for Day 3, 7, 14 
    set.seed(123)
    Day3_RR <- sample(166:450,10)
    Day7_RR <- sample(166:450,10)
    Day14_RR <- sample(166:450,10)
    BLRR <- c(233, 200, 250, 450, 350, 234, 180, 300, 250, 166)
    Type <- c("Stroke", "Stroke", "Sham", "Sham", "Sham", "Stroke", "Stroke", "Sham", "Sham", "Stroke") 
    ID <- 1:10
    df <- cbind(ID, Type, BLRR, Day3_RR, Day7_RR, Day14_RR)
    
    # tweak dataframe, to longformat with `pivot_longer` from `tidyr` package
    df <- df %>% 
      as_tibble() %>% 
      type.convert(as.is = T) %>% 
      pivot_longer(
        cols= c(starts_with("Day"), BLRR), 
        names_to = "time",
        values_to = "RR"
      ) %>% 
      mutate(time = factor(time, levels = c("BLRR", "Day3_RR", "Day7_RR", "Day14_RR"))) %>% 
      group_by(Type, time) %>% 
      dplyr::summarise(avg_RR = mean(RR, na.rm=TRUE)) %>% 
      ungroup()
    
    
    ggplot(df, aes(x=time, y=avg_RR, fill = Type)) + 
      geom_bar(stat="identity", position = "dodge") + 
      geom_text(aes(label=round(avg_RR)), position=position_dodge(width=0.9), vjust=-0.25) +
      scale_fill_brewer(palette = "Dark2") +
      theme_bw()
    
    

    【讨论】:

    • 这工作得很好,只有一个问题。我的一些动物在第 3 天和第 7 天死亡,因此该列的数据为 NA,当我这样做时,它只给了我 BL_RR 的列,而其他的没有列。我尝试对这些列使用 na.omit() 但没有成功,因为我不确定它在代码中的位置。非常感谢,非常感谢。
    • pivot_longer()之后添加mutate(RR = if_else(is.na(RR), 0, RR))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-25
    • 2014-08-02
    • 2018-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-08
    相关资源
    最近更新 更多