我使用以下代码生成分组的boxplot
S<-read.table("N.csv", sep=";",header = TRUE)
S
D_Level Mel R FW
1: 100% 0 mM 1 60
2: 100% 0 mM 2 70
3: 100% 0 mM 3 64
4: 100% 50 mM 1 90
5: 100% 50 mM 2 85
6: 100% 50 mM 3 88
7: 100% 100 mM 1 80
8: 100% 100 mM 2 85
9: 100% 100 mM 3 75
10: 100% 200 mM 1 74
11: 100% 200 mM 2 68
12: 100% 200 mM 3 75
13: 70% 0 mM 1 62
14: 70% 0 mM 2 50
15: 70% 0 mM 3 58
16: 70% 50 mM 1 85
17: 70% 50 mM 2 80
18: 70% 50 mM 3 75
19: 70% 100 mM 1 67
20: 70% 100 mM 2 65
21: 70% 100 mM 3 60
22: 70% 200 mM 1 60
23: 70% 200 mM 2 68
24: 70% 200 mM 3 70
25: 50% 0 mM 1 56
26: 50% 0 mM 2 40
27: 50% 0 mM 3 46
28: 50% 50 mM 1 45
29: 50% 50 mM 2 53
30: 50% 50 mM 3 55
31: 50% 100 mM 1 50
32: 50% 100 mM 2 55
33: 50% 100 mM 3 50
34: 50% 200 mM 1 51
35: 50% 200 mM 2 50
36: 50% 200 mM 3 48
S$D_Level <- factor(S$D_Level,
levels = c('100%','70%','50%'),ordered = TRUE)
ggplot(data=S) +
geom_boxplot( aes(x=factor(D_Level), y=FW, fill=factor(Mel),order =
as.numeric(Mel)), position=position_dodge(.8)) +
theme(panel.background = element_rect(fill = 'white', colour = 'black'))+
labs(x = "Drought level",y = "FW",fill = "Mel")+
scale_fill_discrete(breaks=c("0 mM","50 mM","100 mM","200 mM"))
如您所见,这些方框按顺序排列:0,100,200和50.但是,我想按顺序制作:0,50,100和200。 有什么帮助吗?
答案 0 :(得分:0)
您可以通过自己制作因子来提供订单(就像您使用D_level一样)。只需factor(var)
即可按字母顺序排列因子级别。
因此在0 1和2之后出现。
txt <- "
D_Level,Mel,R,FW
100%,0 mM,1,60
100%,0 mM,2,70
100%,0 mM,3,64
100%,50 mM,1,90
100%,50 mM,2,85
100%,50 mM,3,88
100%,100 mM,1,80
100%,100 mM,2,85
100%,100 mM,3,75
100%,200 mM,1,74
100%,200 mM,2,68
100%,200 mM,3,75
70% ,0 mM,1,62
70% ,0 mM,2,50
70% ,0 mM,3,58
70% ,50 mM,1,85
70% ,50 mM,2,80
70% ,50 mM,3,75
70% ,100 mM,1,67
70% ,100 mM,2,65
70% ,100 mM,3,60
70% ,200 mM,1,60
70% ,200 mM,2,68
70% ,200 mM,3,70
50% ,0 mM,1,56
50% ,0 mM,2,40
50% ,0 mM,3,46
50% ,50 mM,1,45
50% ,50 mM,2,53
50% ,50 mM,3,55
50% ,100 mM,1,50
50% ,100 mM,2,55
50% ,100 mM,3,50
50% ,200 mM,1,51
50% ,200 mM,2,50
50% ,200 mM,3,48
"
S <- read.table(textConnection(txt), sep = ",", header = T)
S$D_Level <- factor(S$D_Level,
levels = c('100%','70%','50%'),ordered = TRUE)
S$Mel <- factor(S$Mel,
levels = paste(c(0,50,100,200),'mM'))
require(ggplot2)
ggplot(data=S, aes(x=factor(D_Level), y=FW, fill=Mel)) +
geom_boxplot(position=position_dodge(.8)) +
theme(panel.background = element_rect(fill = 'white', colour = 'black')) +
labs(x = "Drought level",y = "FW",fill = "Mel")