[R] adjusting x-axis labels
Dennis Murphy
djmuser at gmail.com
Wed Jul 27 03:39:09 CEST 2011
Hi:
Here are a few options. The most important thing I'd recommend is to
rotate the bar plot so that the species can be read easily. In the
process, you also get the right justification you want in a readable
text size. Here's an example with the barplot() function in base R:
set.seed(103)
x <- round(rnorm(9, mean = 800, sd = 200))
speciesnames<-c("Dialium\nguianensis", "Inga\nalba", "Tachigali\nversicolor",
"Brosimum\nutile",
"Caryocar\ncostaricense", "Castilla\ntunu",
"Otoba\nnovagranatensis",
"Pourouma\nbicolor", "Socratea\nexorrhiza")
# Increase margin for y axis to accommodate names
par(mar = c(5, 7, 4, 2) + 0.1)
mybp <- barplot(x, horiz = TRUE,
xlab = expression(plain("Soil acid phosphatase
activity")~(nmol~h^{-1}*g^{-1})),
yaxt = 'n')
axis(2, at = mybp[, 1], labels = speciesnames, srt = 90, las = 2)
box()
par(mar = c(5, 4, 4, 2) + 0.1) # return to default values
#-------
# Here is another possibility using the ggplot2 package.
# Start by putting species names on one line
speciesnames<-c("Dialium guianensis", "Inga alba", "Tachigali
versicolor", "Brosimum utile",
"Caryocar costaricense", "Castilla tunu", "Otoba
novagranatensis", "Pourouma bicolor",
"Socratea exorrhiza")
# Combine x and speciesnames into a data frame for ggplot2
dd <- data.frame(speciesnames, x)
# Basic bar plot; coord_flip() switches from vertical to horizontal
# stat = 'identity' means use the variable x to represent the counts (weights)
library(ggplot2)
ggplot(dd, aes(x = speciesnames, y = x)) +
geom_bar(stat = 'identity') + coord_flip() +
xlab("") +
ylab(expression(plain("Soil acid phosphatase
activity")~(nmol~h^{-1}*g^{-1})))
# Redo with some bells and whistles
# (1) Reorder species by increasing count; (2) use white background;
# (3) Use blue for fill color and orange for border color
ggplot(dd, aes(x = reorder(speciesnames, x, mean), y = x)) +
theme_bw() +
geom_bar(stat = 'identity', fill = 'blue', colour = 'orange') +
coord_flip() +
xlab("") +
ylab(expression(plain("Soil acid phosphatase
activity")~(nmol~h^{-1}*g^{-1})))
You should be able to reorder the factor levels per above in the
barplot() code as well; there are options for color as well as several
other characteristics. See ?barplot.
HTH,
Dennis
On Tue, Jul 26, 2011 at 10:33 AM, Adrienne Keller
<adrienne.keller at umontana.edu> wrote:
> I am trying to tweak how my categorical x-axis labels are formatted in my
> bar graph. Specifically, I would like to a) decrease the spacing between
> lines (e.g. spacing between Dialium and guianensis) b) right justify the
> text and c) have each species name align with the center of the
> corresponding bar graph.
>
> A simplified version of my code thus far is as follows. I am using RStudio
> v. .94.84
>
> install.packages('gplots')
> library(gplots)
> x<-rnorm(9, mean=600, sd=300)
> speciesnames<-c("Dialium\nguianensis", "Inga\nalba",
> "Tachigali\nversicolor", "Brosimum\nutile", "Caryocar\ncostaricense",
> "Castilla\ntunu", "Otoba\nnovagranatensis", "Pourouma\nbicolor",
> "Socratea\nexorrhiza")
> xvals<-barplot2(x, ylab=expression(plain("Soil acid phosphatase
> activity")~(nmol~h^{-1}*g^{-1})), ylim=c(0,1200), cex.axis=0.6)
> text(xvals,par("usr")[3]-115, srt=45,adj=c(0.5,0), labels=speciesnames,
> cex=.6, xpd=T)
>
> Any suggestions to help solve these problems would be greatly appreciated. I
> am new to working with graphics in R.
>
> Thanks,
>
> Adrienne Keller
> Graduate Student, College of Forestry
> University of Montana
> adrienne.keller at umontana.edu
> (Phone) 651-485-5822
>
> ______________________________________________
> 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