我有这样的数据
df<- structure(list(Samples = structure(c(1L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 2L, 3L, 1L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 2L,
3L, 1L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 2L, 3L, 1L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 2L, 3L, 1L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 11L, 2L, 3L, 1L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 2L, 3L,
1L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 2L, 3L, 1L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 2L, 3L, 1L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 2L, 3L, 1L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 2L, 3L, 1L,
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 2L, 3L, 1L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 2L, 3L), .Label = c("Sam1", "Sam10", "Sam11",
"Sam2", "Sam3", "Sam4", "Sam5", "Sam6", "Sam7", "Sam8", "Sam9"
), class = "factor"), variable = structure(c(1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L), .Label = c("Group1", "Group1",
"Group1", "Group2", "Group2", "Group2", "Group3", "Group3", "Group3",
"Group4", "Group4", "Group4"), class = "factor"), value = c(0.000708,
0.000525, 0.0026, 5.5e-05, 0.000291, 0.00177, 0.000281, 0.00117,
0.000199, 0.0133, 0.00161, 0.000708, 0.000525, 0.0026, 5.5e-05,
0.000291, 0.00177, 0.000281, 0.00117, 0.000199, 0.0133, 0.00161,
0.000708, 0.000525, 0.0026, 5.5e-05, 0.000291, 0.00177, 0.000281,
0.00117, 0.000199, 0.0133, 0.00161, 0.000777, 0.00177, 0.00175,
4.47e-05, 0.000131, 0.000834, 3e-04, 0.0019, 0.000164, 0.0115,
0.00109, 0.000777, 0.00177, 0.00175, 4.47e-05, 0.000131, 0.000834,
3e-04, 0.0019, 0.000164, 0.0115, 0.00109, 0.000777, 0.00177,
0.00175, 4.47e-05, 0.000131, 0.000834, 3e-04, 0.0019, 0.000164,
0.0115, 0.00109, 0.000398, 0.000738, 0.00224, 5.28e-05, 0.000243,
0.000783, 0.000284, 0.00153, 0.000191, 0.0141, 0.00124, 0.000398,
0.000738, 0.00224, 5.28e-05, 0.000243, 0.000783, 0.000284, 0.00153,
0.000191, 0.0141, 0.00124, 0.000398, 0.000738, 0.00224, 5.28e-05,
0.000243, 0.000783, 0.000284, 0.00153, 0.000191, 0.0141, 0.00124,
0.00237, 0.000661, 0.00364, 4.44e-05, 0.000238, 0.000925, 0.000395,
0.00588, 1e-04, 0.0141, 0.00147, 0.00237, 0.000661, 0.00364,
4.44e-05, 0.000238, 0.000925, 0.000395, 0.00588, 1e-04, 0.0141,
0.00147, 0.00237, 0.000661, 0.00364, 4.44e-05, 0.000238, 0.000925,
0.000395, 0.00588, 1e-04, 0.0141, 0.00147)), row.names = c(NA,
-132L), class = "data.frame")
我正在尝试这样绘制它
cols <- rainbow_hcl(4, c = 50) # choose chromaticity of 50 to dull colours
ggplot(data = df, aes(x=variable, y=value),col=cols) +
stat_boxplot(geom="errorbar", width=.5)+
geom_boxplot(aes(fill=variable))+
theme(legend.position="none")+
theme_bw()+
theme(axis.title.x=element_blank(), axis.title.y=element_blank())+
stat_summary(fun.y=median, colour="red", geom="line", aes(group = 1))+
geom_jitter(position = position_jitter(0.2))
答案 0 :(得分:0)
您需要在aes
函数内部分配color参数。此外,您需要包括一个scale_color_manual
图层才能使用您拥有的特定颜色矢量(例如cols
)。
这看起来如何?
ggplot(data = df, aes(x=variable, y=value, color = variable)) +
stat_boxplot(geom="errorbar", width=.5)+
geom_boxplot()+
scale_color_manual(values = cols) +
theme(legend.position="none")+
theme_bw()+
theme(axis.title.x=element_blank(), axis.title.y=element_blank())+
stat_summary(fun.y=median, colour="red", geom="line", aes(group = 1))+
geom_jitter(position = position_jitter(0.2))
答案 1 :(得分:0)
您的数据框有些奇怪,它具有重复的级别:
levels(df$variable)
[1] "Group1" "Group1" "Group1" "Group2" "Group2" "Group2" "Group3" "Group3"
[9] "Group3" "Group4" "Group4" "Group4"
我们先解决这个问题:
df$variable=factor(as.character(df$variable))
然后我们绘制:
cols <- rainbow_hcl(4, c = 50) # choose chromaticity of 50 to dull colours
g <- ggplot(data = df, aes(x=variable, y=value,col=variable)) +
stat_boxplot(geom="errorbar", width=.5)+
geom_boxplot()+
theme(legend.position="none")+
theme_bw()+
theme(axis.title.x=element_blank(), axis.title.y=element_blank())+
stat_summary(fun.y=median, colour="red", geom="line", aes(group = 1))+
geom_jitter(position = position_jitter(0.2))
g + scale_fill_manual(values=cols)