【问题标题】:How should I organize my data frame to easily compare specific groups and graph them?我应该如何组织我的数据框以轻松比较特定组并绘制它们?
【发布时间】:2021-01-11 11:46:59
【问题描述】:

我是 R 的新手。我没有足够的经验来了解我应该如何格式化我的数据以生成多个图表来比较 R 中的某些组。 我有两个时间点进行 3 次治疗和 2 次对照。 我希望能够创建多个图表来比较特定组。 T1和T2是时间点。

test <- structure(list(group = c("control1 T1", "control2 T1", "treatment1 T1", 
"treatment2 T1", "treatment3 T1", "control1 T1", "control2 T1", 
"treatment1 T1", "treatment2 T1", "treatment3 T1", "control1 T1", 
"control2 T1", "treatment1 T1", "treatment2 T1", "treatment3 T1", 
"control1 T2", "control2 T2", "treatment1 T2", "treatment2 T2", 
"treatment3 T2", "control1 T1", "control2 T1", "treatment1 T1", 
"treatment2 T1", "treatment3 T1", "control1 T1", "control2 T1", 
"treatment1 T1", "treatment2 T1", "treatment3 T1", "control1 T1", 
"control2 T1", "treatment1 T1", "treatment2 T1", "treatment3 T1", 
"control1 T2", "control2 T2", "treatment1 T2", "treatment2 T2", 
"treatment3 T2"), value = c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 
5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 
1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 4L, 5L, 6L)), class = "data.frame", row.names = c(NA, 
-40L))

我试过这个:

my_comparisons <- list( c("control1 T1", "control1 T2"), c("control2 T1", "control2 T2"), c("treatment1 T1", "treatment1 T2") , c("treatment3 T1", "treatment3 T2"))#

ggboxplot(test, x = "group", y = "value", color = "group", 
          #add = "jitter",
          legend = "none", outlier.shape = NA) + 
  rotate_x_text(angle = 45) + geom_jitter(width = 0.15, alpha = .1, color = "black") +
  stat_compare_means(comparisons = my_comparisons, label.y = c(5, 5, 5, 5, 5, 5))+
  stat_compare_means(label.y = 5)

上述 ggboxplot 生成的图表很好,但我想将特定组相互比较。例如“治疗1 T1”、“治疗1 T2”。

我试过 facet_wrap。

p <- ggplot(data = test, aes(x=group, y=value)) + 
  geom_boxplot(aes(fill=group))
p + facet_wrap( ~ group, scales="free")

我喜欢这种格式,但我每个区域只有一个图表。理想情况下,我想在每个部分中比较两组。我不知道该怎么做。我可以手动拆分数据并一次制作每个图表,但应该可以一次完成所有操作并选择要针对每个方面比较哪些组?

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    如果您想按特定组对图进行分面,则需要一个新列来确定分组。在下面,我创建了group2 列,其中具有不同时间(T1、T2)的相同组得到了相同的数字。 (如果您愿意,可以将数字替换为字符)

    请注意,我对您的 value 列进行了抽样,因为在原始 test 数据集中,各组没有差异。因此箱线图显示为一条线。

    library(tidyverse)
    
    # put some variance in value                                       
    test <- test %>%
      mutate(value = sample(1:5, 40, replace = T))
    
    # create new column - group2
    test <- test %>% 
      mutate(group2 = case_when(group %in% c("control1 T1", "control1 T2") ~ 1,
                                group %in% c("treatment1 T1", "treatment1 T2") ~ 3,
                                group %in% c("control2 T1", "control2 T2") ~ 2,
                                group %in% c("treatment2 T1", "treatment2 T2") ~ 4,
                                group %in% c("treatment3 T1", "treatment3 T2") ~ 5, 
                                TRUE ~ NA_real_))
    
    
    # facet by group2
    p <- ggplot(data = test, aes(x=group, y=value)) + 
      geom_boxplot(aes(fill=group)) 
    p + facet_wrap( ~ as.factor(group2), scales="free")
    
    

    结果就是这样。

    编辑:更灵活的功能

    如果您熟悉正则表达式,您可以在 facet_wrap 本身中拆分您的方面。我在以下示例中使用了 stringr 包。

    # facet by T1 - T2
    p <- ggplot(data = test, aes(x=group, y=value)) + 
      geom_boxplot(aes(fill=group)) 
    p + facet_wrap( ~ str_extract(group, "T[123]{1}"), scales="free")
    
    # facet by control vs treatment
    p <- ggplot(data = test, aes(x=group, y=value)) + 
      geom_boxplot(aes(fill=group)) 
    p + facet_wrap( ~ str_extract(group, "treatment|control"), scales="free")
    
    # facet by group 
    p <- ggplot(data = test, aes(x=group, y=value)) + 
    geom_boxplot(aes(fill=group)) 
    p + facet_wrap( ~ str_extract(group, "treatment[123]|control[12]"), scales="free")
    

    【讨论】:

    • 我想在“group2”列中分面的特定组有一个限制。当我想在同一时间点比较两种治疗方法时,问题就开始了。它已在列 group2 上标记。如果我想比较具有相同时间戳的不同处理。我将不得不制作另一个专栏?我只想灵活地选择多个组合,而不是严格按时间戳。此外,更改为字母时出现错误。错误:mutate() 输入 group2 有问题。 x 必须是字符向量,而不是双精度向量。 i 输入group2case_when(...)
    • To case_when :您还必须将 NA 更改为字符。 NA_real_ 用于数字,NA_character_ 用于字符。这里是mutate函数的简化版,需要填写...test &lt;- test %&gt;% mutate(group2 = case_when(group %in% c("control1 T1", "control1 T2") ~ "c 1", group %in% c("treatment1 T1", "treatment1 T2") ~ "tr 1", ..., TRUE ~ NA_character_))
    猜你喜欢
    • 2012-11-25
    • 1970-01-01
    • 2016-06-04
    • 2015-03-17
    • 2015-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-21
    相关资源
    最近更新 更多