boxplot "names" encroaches on axis

556 views
Skip to first unread message

Earl Brown

unread,
Aug 7, 2014, 5:58:34 PM8/7/14
to corplin...@googlegroups.com

Rists,

I want to add a line break to each name in the "names" argument of boxplot(), but that pushes the first word up into the x-axis. I increased margin sizes with par(mar) and par(oma), but that only increases the space between the second (botton) word and the bottom of the plot, rather than creating more space between the first (top) word and the axis. Also, I tried turning off the axis and adding it myself:

boxplot(... axis = FALSE)
axis(... line = 2)

but that moves down the axis line itself too, when I only want to move the words down.

I'll attach a screenshot of what I'm seeing. (Hopefully, it will attach.)

Thanks in advance for any help. Earl Brown


David Reitter

unread,
Aug 7, 2014, 6:56:33 PM8/7/14
to corplin...@googlegroups.com
On Aug 7, 2014, at 5:58 PM, Earl Brown <ekbr...@gmail.com> wrote:

> I want to add a line break to each name in the "names" argument of boxplot(), but that pushes the first word up into the x-axis. I increased margin sizes with par(mar) and par(oma), but that only increases the space between the second (botton) word and the bottom of the plot, rather than creating more space between the first (top) word and the axis.

I can’t tell you how to fix the specific issue elegantly. If you must, perhaps try using empty labels and the `text’ function to place labels manually.

However, I would, in this case, just leave out the redundant word “speakers”, because it does not discriminate between the three classes.

Second, as a general idea, consider using ggplot2, which creates prettier plots with more control in much nicer syntax. (After getting over the learning curve, that is.)

Best
David

Alex Perrone

unread,
Aug 7, 2014, 8:04:22 PM8/7/14
to corplin...@googlegroups.com
Yeah, I couldn’t get it to work with `boxplot` either. Even when I added a “\n” before the x-axis labels, it kept creeping up towards x-axis. 


--
You received this message because you are subscribed to the Google Groups "CorpLing with R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to corpling-with...@googlegroups.com.
To post to this group, send email to corplin...@googlegroups.com.
Visit this group at http://groups.google.com/group/corpling-with-r.
For more options, visit https://groups.google.com/d/optout.

Martin Schweinberger

unread,
Aug 8, 2014, 7:48:32 AM8/8/14
to corplin...@googlegroups.com
Hi R-onauts,

there are more elegant ways to handle this, but this fix works fine on my machine. you can set up traditional box plots (without the ggplot2 package) by combining the argument axes = F with the axis function and the mtext function as suggested by the pervious commentators (I used the mtcars data for illustration purposes):

boxplot(mpg~cyl,data=mtcars, main="Customized Box Plot", ylab = "Duration (ms)", axes = F)
axis(1, at= 1:3, labels= c("", "", ""), lty= 1, col= "blue", las= .8)
mtext(c("English\nspeakers", "Heritage\nspeakers", "Spanish\nspeakers"), side = 1, line= 3, at = 1:3)
box()


​all the best,
martin

=====================================
Martin Schweinberger
Gählerstraße 11
22767 Hamburg

Fon.: ++49 (0)176 387 48 283
Home: http://www.martinschweinberger.de/blog/

Martin Schweinberger

unread,
Aug 8, 2014, 7:59:16 AM8/8/14
to corplin...@googlegroups.com
sorry - forgot the tick marks and labels at the 2nd axis. here is the updated code:

boxplot(mpg~cyl,data=mtcars, main="Customized Box Plot", ylab = "Duration (ms)", ylim = c(10, 40), axes = F)

axis(1, at= 1:3, labels= c("", "", ""), lty= 1, col= "blue", las= .8)
axis(2, at= c(10, 20, 30, 40), labels= c("10", "20", "30", "40"), lty= 1, col= "blue", las= .8)

mtext(c("English\nspeakers", "Heritage\nspeakers", "Spanish\nspeakers"), side = 1, line= 3, at = 1:3)
box()




best

Earl Brown

unread,
Aug 11, 2014, 2:28:47 PM8/11/14
to corplin...@googlegroups.com

Thanks for all the replies. I've used base R boxplot() with mtext(), as I don't yet have the control in ggplot2 that I have in base R. I'll attach the final graphic I was trying to produce, if you'd like to see it. Thanks again.

Reply all
Reply to author
Forward
0 new messages