Skip to content

Adjusting format of boxplot

10 messages · David Arnold, David L Carlson, Peter Ehlers +3 more

#
Hi,

I managed to use the attached data set and figure out the following:

flies <- read.table("example12_1.dat",header=TRUE,sep="\t")
boxplot(long ~ group,
        data = flies,
        horizontal = TRUE,
        col = "red")

I'm very new to R and would like some help with the following:

1. Change the order on the y-axis from 1, 2, 3, 4, 5 to 5, 4, 3, 2, 1. In other words, I want the bottom boxplot on top.

2. I want the y-axis labels 1, 2, 3, 4, 5 to read Group 1, Group 2, Group 3, Group 4, and Group 5, and I would like them to be horizontal; i.e. parallel to the boxplots.

3. I'd like the tick marks and labels on the horizontal axis to go from 10 to 110 by 20's.

4. Finally, I want to turn off the frame surrounding the image, but not the horizontal axis and its tick marks and labels.

I appreciate the help.

Thanks.

David
#
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.
#
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:
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.
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.
data = flies,
        pars=list(las=1, ylim=c(10, 110), xaxt="n", bty="n"), 
        horizontal = TRUE,
        col = "red")

Finally draw the x-axis.
-------------------------------------
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
#
Added your code:


flies <- read.table("example12_1.dat",header=TRUE,sep="\t")

flies$group <- factor(flies$group,5:1)

levels(flies$group) <- paste0("Group ",5:1)

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

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

Almost worked perfectly, except the frame around the plot remains, which is
strange as you have bty="n".

http://r.789695.n4.nabble.com/file/n4636381/Rplot11.png 

David

--
View this message in context: http://r.789695.n4.nabble.com/Adjusting-format-of-boxplot-tp4636373p4636381.html
Sent from the R help mailing list archive at Nabble.com.
#
Sorry about that. I got rid of the box another way and then switched to using pars=
without making sure it worked. This works: 

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

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

axis(1, at=seq(10, 110, 20))
par(oldpar)

------------------------------------- 
David 

-------------------------------------
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 7:53:33 PM 
Subject: Re: [R] Adjusting format of boxplot 

Added your code: 


flies <- read.table("example12_1.dat",header=TRUE,sep="\t") 

flies$group <- factor(flies$group,5:1) 

levels(flies$group) <- paste0("Group ",5:1) 

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

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

Almost worked perfectly, except the frame around the plot remains, which is 
strange as you have bty="n". 

http://r.789695.n4.nabble.com/file/n4636381/Rplot11.png 

David
#
On 2012-07-12 18:39, David L Carlson wrote:
Or you could add 'frame = FALSE' to the boxplot() call.

Peter Ehlers
#
And I guess I would also either leave out the vertical axis line
or add box(bty="l") after the axis(1, ....) call.

Peter Ehlers
On 2012-07-12 19:48, Peter Ehlers wrote:
#
Hi Peter,

I had never heard of this 'frame' argument and it's a breakthrough for 
me to be finally able to get rid of this frame!

But where is this argument explained? I couldn't find it in plot(), 
boxplot(), bxp() or par().

Thank you for your answer :)
Ivan

--
Ivan CALANDRA
Universit? de Bourgogne
UMR CNRS/uB 6282 Biog?osciences
6 Boulevard Gabriel
21000 Dijon, FRANCE
+33(0)3.80.39.63.06
ivan.calandra at u-bourgogne.fr
http://biogeosciences.u-bourgogne.fr/calandra

Le 13/07/12 04:48, Peter Ehlers a ?crit :
#
Hello, Ivan,

you'll find argument frame (actually frame.plot) explained in

?plot.default

  Regards  --  Gerrit