ggplot 中不同列值的多个箱线图并排放置
Multiple boxplots placed side by side for different column values in ggplot
我读过 this and this 等不同的帖子,但我的问题有细微差别。我有一个这样的 df
ID <- c("DJ45","DJ46","DJ47","DJ48","DJ49","DJ53","DJ54","DJ55","DJ56","DJ57")
Tool <- c("Tool_A", "Tool_A", "Tool_A", "Tool_A", "Tool_A", "Tool_B", "Tool_B", "Tool_B", "Tool_B", "Tool_B")
Name <- c("CMP", "CMP", "CMP", "CMP", "CMP", "CMP", "CMP", "CMP", "CMP", "CMP")
MS1 <- c(51,55,50,59,50,47,48,42,43,46)
MS2 <- c(13,11,14,11,10,17,18,17,20,21)
MS3 <- c(2,3,2,5,6,4,9,6,4,4)
MS4 <- c(16,13,14,11,16,16,18,16,19,15)
MS5 <- c(3,6,3,6,3,4,4,8,5,4)
MS6 <- c(7,7,5,5,8,9,8,6,6,9)
df1 <- data.frame(ID,Tool,Name,MS1,MS2,MS3,MS4,MS5,MS6)
我试图从统计学上找出工具(Tool_A 和 Tool_B)在不同测量步骤中的差异,因此我做了 t-test。
t.test(MS1 ~ Tool, df1)
我使用 ggplot 绘制箱线图以进行可视化,但在这里我为步骤 1 进行绘制。
p <- ggplot(df1, aes(factor(Tool), MS6))
p + geom_boxplot(aes(fill = Tool)) + labs(title = "CMP")
我想将所有 6 个测量步骤的箱线图并排放置在一个共同的标题 (CMP) 下。 facet_wrap 能做到吗?我只是无法做到正确。请提供建议。
你的问题是你需要一个长格式来做facet_wraps。
#first, reshape to long
library(reshape2)
df1_long <- melt(df1, id.vars=c("ID","Tool","Name"))
#then plot
p2 <- ggplot(df1_long, aes(x=factor(Tool),y=value,fill=factor(Tool)))+
geom_boxplot() + labs(title="CMP") +facet_wrap(~variable)
p2
你也可以在没有 facet_wrap 的情况下这样做:
library(reshape2)
df2<-melt(df1,id.var=c("ID","Tool","Name"))
p <- ggplot(df2, aes(variable, value,fill=Tool))
p + geom_boxplot() + labs(title = "CMP")
也可以使用Tidyr包中的gather函数对数据进行整形:
library(tidyr)
df1 %>%
gather(MS, value, MS1, MS2, MS3, MS4, MS5, MS6) %>%
ggplot(aes(x = factor(Tool), y = value, fill = factor(Tool)))+
geom_boxplot()+
facet_wrap(~MS)
我读过 this and this 等不同的帖子,但我的问题有细微差别。我有一个这样的 df
ID <- c("DJ45","DJ46","DJ47","DJ48","DJ49","DJ53","DJ54","DJ55","DJ56","DJ57")
Tool <- c("Tool_A", "Tool_A", "Tool_A", "Tool_A", "Tool_A", "Tool_B", "Tool_B", "Tool_B", "Tool_B", "Tool_B")
Name <- c("CMP", "CMP", "CMP", "CMP", "CMP", "CMP", "CMP", "CMP", "CMP", "CMP")
MS1 <- c(51,55,50,59,50,47,48,42,43,46)
MS2 <- c(13,11,14,11,10,17,18,17,20,21)
MS3 <- c(2,3,2,5,6,4,9,6,4,4)
MS4 <- c(16,13,14,11,16,16,18,16,19,15)
MS5 <- c(3,6,3,6,3,4,4,8,5,4)
MS6 <- c(7,7,5,5,8,9,8,6,6,9)
df1 <- data.frame(ID,Tool,Name,MS1,MS2,MS3,MS4,MS5,MS6)
我试图从统计学上找出工具(Tool_A 和 Tool_B)在不同测量步骤中的差异,因此我做了 t-test。
t.test(MS1 ~ Tool, df1)
我使用 ggplot 绘制箱线图以进行可视化,但在这里我为步骤 1 进行绘制。
p <- ggplot(df1, aes(factor(Tool), MS6))
p + geom_boxplot(aes(fill = Tool)) + labs(title = "CMP")
我想将所有 6 个测量步骤的箱线图并排放置在一个共同的标题 (CMP) 下。 facet_wrap 能做到吗?我只是无法做到正确。请提供建议。
你的问题是你需要一个长格式来做facet_wraps。
#first, reshape to long
library(reshape2)
df1_long <- melt(df1, id.vars=c("ID","Tool","Name"))
#then plot
p2 <- ggplot(df1_long, aes(x=factor(Tool),y=value,fill=factor(Tool)))+
geom_boxplot() + labs(title="CMP") +facet_wrap(~variable)
p2
你也可以在没有 facet_wrap 的情况下这样做:
library(reshape2)
df2<-melt(df1,id.var=c("ID","Tool","Name"))
p <- ggplot(df2, aes(variable, value,fill=Tool))
p + geom_boxplot() + labs(title = "CMP")
也可以使用Tidyr包中的gather函数对数据进行整形:
library(tidyr)
df1 %>%
gather(MS, value, MS1, MS2, MS3, MS4, MS5, MS6) %>%
ggplot(aes(x = factor(Tool), y = value, fill = factor(Tool)))+
geom_boxplot()+
facet_wrap(~MS)