[R] Mix of plain and italic text in ggplot categorical x-axis

Tom Walker tom.n.walker at gmail.com
Tue May 6 23:51:33 CEST 2014


Hi,

I need to generate bar charts where the x-axis is a factor that
includes a mixture of species names (in italic) and control treatments
(in plain text).

I would like this to be represented in the contents of the axis
labels, meaning that I need the x-axis to include both italic and
plain text. An example of my failed attempt is below (the bins in the
label list containing expressions become blank).

d <- data.frame(Category = c("Sphagnum plant", "Calluna plant",
"Eriophorum plant", "Control"),
                Response = c(1, 3, 5, 6))

d

mylabels <- list(expression(paste(italic("Sphagnum"), " plant")),
                 expression(paste(italic("Calluna"), " plant")),
                 expression(paste(italic("Eriophorum"), " plant")),
                 "Control")

ggplot(d) +
  aes(x = Category, y = Response) +
  geom_bar() +
  scale_x_discrete(labels = mylabels)

Any help would be much appreciated!

Many thanks,

Tom



More information about the R-help mailing list