Boxplot

30 views
Skip to first unread message

julienhi...@gmail.com

unread,
May 26, 2015, 2:38:24 AM5/26/15
to ggp...@googlegroups.com
Hi,

I want to build a boxplot ploting length as a function of time (weeks).

Here is my code :

###Data
data3 <- structure(list(Date = structure(c(43L, 43L, 3L, 3L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 9L, 9L, 9L, 9L, 9L, 9L, 
11L, 40L, 40L, 35L, 38L, 38L, 38L, 38L, 39L, 41L, 41L, 41L, 41L, 
8L, 8L, 8L, 23L, 18L, 18L, 18L, 30L, 36L, 4L, 7L, 7L, 1L, 10L, 
10L, 12L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 15L, 15L, 
17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 20L, 20L, 20L, 2L, 
5L, 16L, 16L, 19L, 22L, 22L, 22L, 22L, 22L, 22L, 25L, 25L, 27L, 
21L, 24L, 26L, 28L, 28L, 29L, 29L, 31L, 31L, 33L, 37L, 32L, 32L, 
34L, 42L), .Label = c("01/12/2013", "02/12/2013", "04/01/2014", 
"04/12/2010", "04/12/2013", "05/01/2014", "05/12/2010", "05+06/02/14", 
"06/01/2014", "06/12/2010", "07/01/2014", "07/12/2010", "08/12/2010", 
"09/12/2010", "10/12/2010", "11/12/2009", "11/12/2010", "12/02/2015", 
"12/12/2009", "12/12/2010", "14/12/2009", "14/12/2010", "15/02/2010", 
"15/12/2009", "15/12/2010", "16/12/2009", "17/12/2010", "18/12/2009", 
"19/12/2009", "20/02/2015", "20/12/2009", "21/12/2009", "21/12/2010", 
"22/12/2009", "23/01/2015", "23/02/2015", "24/12/2010", "27/01/2014", 
"28/01/2014", "29/01/2010", "29/01/2014", "30/12/2009", "30/12/2013"
), class = "factor"), Espece = structure(c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L), .Label = "A. megastoma", class = "factor"), Semaine = c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 8L, 9L, 48L, 48L, 48L, 
48L, 49L, 49L, 49L, 49L, 49L, 49L, 49L, 49L, 49L, 49L, 49L, 49L, 
49L, 49L, 49L, 49L, 49L, 49L, 49L, 49L, 49L, 49L, 49L, 49L, 49L, 
49L, 49L, 49L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 
50L, 50L, 51L, 51L, 51L, 51L, 51L, 51L, 51L, 51L, 51L, 51L, 51L, 
52L, 52L, 52L, 53L), LT = c(53, 54, 50, 54, 52, 52, 54, 53, 52, 
52, 53, 51, 52, 51, 52.5, 50.5, 51, 53, 53, 51, 52, 52, 52, 51, 
49, 52, 54, 52.5, 53, 52, 54, 52, 49, 55.5, 51, 54, 54, 53, 53, 
55, 50, 49, 47, 50, 50, 54, 52, 52, 49, 54, 52, 54, 54, 51, 48, 
48.5, 49, 51, 50, 50, 49, 53, 49.5, 50.5, 52, 48, 53.5, 52.5, 
55, 51, 55, 52, 52.5, 50.5, 53, 51, 51, 50, 50, 55, 51, 54.5, 
52, 54.5, 54, 52, 50.5, 50.5, 53, 55, 50, 54, 52, 53, 51, 49, 
48, 44, 51, 52, 53, 49, 52, 51)), .Names = c("Date", "Espece", 
"Semaine", "LT"), row.names = c(NA, -104L), class = "data.frame")

data3$LT <- as.numeric(levels(data3$LT))[data3$LT]

####Graph
p2 <- ggplot(data3, aes(as.factor(Semaine), LT))
p2 + geom_boxplot(colour="#3366FF") + ylim(40,60) + ylab("Total Length (mm)") + xlab("Weeks") + theme_bw() + theme(legend.title=element_blank(), plot.title = element_text(lineheight=.8, face="bold"), panel.grid.major=element_blank(), panel.grid.minor=element_blank(), legend.position="bottom")

My problem : because week 53 is the last week of December and week 1 the first of January, I want my X axe graduated from 47 to 9. How can I do that ?

Thank you

Julien

Wouter van der Bijl

unread,
May 26, 2015, 4:09:16 AM5/26/15
to ggp...@googlegroups.com
ggplot uses the ordering of the factor levels to place the boxplots along the x-axis. You should manually set the order of the levels before plotting.

Example code:
data3$Semaine <- factor(data3$Semaine, levels = c(48:53, 1:8))
ggplot(data3, aes(Semaine, LT)) + geom_boxplot() + theme_bw()

HTH,
W.

julienhi...@gmail.com

unread,
May 26, 2015, 4:29:54 AM5/26/15
to ggp...@googlegroups.com
That works perfectly !
Thank you so much

Julien
Reply all
Reply to author
Forward
0 new messages