R 我应该如何组织我的数据框架来轻松地比较特定的组并绘制它们的图表?

R 我应该如何组织我的数据框架来轻松地比较特定的组并绘制它们的图表?,r,ggplot2,R,Ggplot2,我是R新手。我没有足够的经验知道如何格式化数据以生成多个图形,将R中的某些组相互比较。 我有3个治疗组和2个对照组的两个时间点。 我希望能够创建多个图表,将特定组彼此进行比较。 T1和T2是时间点 test <- structure(list(group = c("control1 T1", "control2 T1", "treatment1 T1", "treatment2 T1", "trea

我是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))

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

注意:我对你的
列进行了抽样,因为在原始
测试
数据集中,各组没有差异。因此,方框图显示为一条线

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")


我想在“group2”列中对特定的组进行刻面,这是有限制的。当我想在同一时间点比较两种治疗方法时,问题就开始了。它已在列group2上标记。如果我想比较同一时间戳的不同处理。我得再写一篇专栏?我只想灵活地选择多个组合,而不是严格按照时间戳。此外,我在更改为字母时出错。错误:
mutate()
input
group2
有问题。x必须是字符向量,而不是双向量。我输入
group2
在(…)
时为
case\u。在
时为
case\u:您还必须将
NA
更改为字符
NA_real_u
表示数字,
NA_character_u
表示字符。这是
mutate
函数的一个简短版本,您需要填写<代码>测试%变异(第2组=情况(第%c组中的组%(“控制1 T1”,“控制1 T2”)~“c1”,第%c组中的组%(“治疗1 T1”,“治疗1 T2”)~“tr1”,…,真~NA_字符)
p <- ggplot(data = test, aes(x=group, y=value)) + 
  geom_boxplot(aes(fill=group))
p + facet_wrap( ~ group, scales="free")
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 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")