0

I used the following code to produce a grouped 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"))

It works well as you see in this graph: enter image description here

As you see, the boxes are in order: 0, 100, 200 and 50. But, I would like to make them in order: 0, 50, 100 and 200. Any help?

user3801226
  • 65
  • 1
  • 6

1 Answers1

0

You can provide the order by making the factor yourself (as you did with D_level). Just factor(var) will order the factor levels alphabetically.

Therefor 5 comes after 0 1 and 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")
Wietze314
  • 5,942
  • 2
  • 21
  • 40