[R] Adjusting format of boxplot

David L Carlson dcarlson at tamu.edu
Fri Jul 13 00:25:10 CEST 2012


If you read the posting guide you will see that only a few types of attachments 
will be allowed through. Also it is much preferred to paste data into your 
email using dput: 

> dput(flies)
structure(list(long = c(40L, 37L, 44L, 47L, 47L, 47L, 68L, 47L, 
54L, 61L, 71L, 75L, 89L, 58L, 59L, 62L, 79L, 96L, 58L, 62L, 70L, 
72L, 74L, 96L, 75L, 46L, 42L, 65L, 46L, 58L, 42L, 48L, 58L, 50L, 
80L, 63L, 65L, 70L, 70L, 72L, 97L, 46L, 56L, 70L, 70L, 72L, 76L, 
90L, 76L, 92L, 21L, 40L, 44L, 54L, 36L, 40L, 56L, 60L, 48L, 53L, 
60L, 60L, 65L, 68L, 60L, 81L, 81L, 48L, 48L, 56L, 68L, 75L, 81L, 
48L, 68L, 35L, 37L, 49L, 46L, 63L, 39L, 46L, 56L, 63L, 65L, 56L, 
65L, 70L, 63L, 65L, 70L, 77L, 81L, 86L, 70L, 70L, 77L, 77L, 81L, 
77L, 16L, 19L, 19L, 32L, 33L, 33L, 30L, 42L, 42L, 33L, 26L, 30L, 
40L, 54L, 34L, 34L, 47L, 47L, 42L, 47L, 54L, 54L, 56L, 60L, 44L
), group = 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, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L)), .Names = c("long", "group"), class = "data.frame", row.names = c(NA, 
-125L))

Everything you want is easy to handle in R but it is not as straightforward
as I initially thought.

First make groups into a factor and then modify the factor levels. The
default for the factor would be in numerical order 1, 2, ... but by listing
the levels in reverse order the plot comes out the way you want. Changing
the levels from 5, 4, ... to Group 5, Group 4, ... takes care of the labels
on the boxplot although you could also use the names= argument in boxplot
to do that.

> flies$group <- factor(flies$group, 5:1) # Request 1
> levels(flies$group) <- paste0("Group ", 5:1) # Request 2

Now insert the pars= argument to handle #3 and set up #4. Note
the counter-intuitive ylim= when we are actually talking about the
x axis - probably a result of plotting the boxplot horizontally.

> boxplot(long ~ group, 
        data = flies,
        pars=list(las=1, ylim=c(10, 110), xaxt="n", bty="n"), 
        horizontal = TRUE,
        col = "red")

Finally draw the x-axis.

> axis(1, at=seq(10, 110, 20))

-------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77840-4352


----- Original Message ----- 

From: "darnold" <dwarnold45 at suddenlink.net> 
To: r-help at r-project.org 
Sent: Thursday, July 12, 2012 5:01:19 PM 
Subject: Re: [R] Adjusting format of boxplot 

Looks like data was not attached. Here is is. 


long group 
40 1 
37 1 
44 1 
47 1 
47 1 
47 1 
68 1 
47 1 
54 1 
61 1 
71 1 
75 1 
89 1 
58 1 
59 1 
62 1 
79 1 
96 1 
58 1 
62 1 
70 1 
72 1 
74 1 
96 1 
75 1 
46 2 
42 2 
65 2 
46 2 
58 2 
42 2 
48 2 
58 2 
50 2 
80 2 
63 2 
65 2 
70 2 
70 2 
72 2 
97 2 
46 2 
56 2 
70 2 
70 2 
72 2 
76 2 
90 2 
76 2 
92 2 
21 3 
40 3 
44 3 
54 3 
36 3 
40 3 
56 3 
60 3 
48 3 
53 3 
60 3 
60 3 
65 3 
68 3 
60 3 
81 3 
81 3 
48 3 
48 3 
56 3 
68 3 
75 3 
81 3 
48 3 
68 3 
35 4 
37 4 
49 4 
46 4 
63 4 
39 4 
46 4 
56 4 
63 4 
65 4 
56 4 
65 4 
70 4 
63 4 
65 4 
70 4 
77 4 
81 4 
86 4 
70 4 
70 4 
77 4 
77 4 
81 4 
77 4 
16 5 
19 5 
19 5 
32 5 
33 5 
33 5 
30 5 
42 5 
42 5 
33 5 
26 5 
30 5 
40 5 
54 5 
34 5 
34 5 
47 5 
47 5 
42 5 
47 5 
54 5 
54 5 
56 5 
60 5 
44 5 


-- 
View this message in context: http://r.789695.n4.nabble.com/Adjusting-format-of-boxplot-tp4636373p4636379.html 
Sent from the R help mailing list archive at Nabble.com. 

______________________________________________ 
R-help at r-project.org mailing list 
https://stat.ethz.ch/mailman/listinfo/r-help 
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html 
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list