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

David Winsemius dwinsemius at comcast.net
Wed May 7 00:08:57 CEST 2014


On May 6, 2014, at 2:51 PM, Tom Walker wrote:

> 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")
> 

Just changing that assignment to create an expression vector instead of a list works in R 3.1.0 Patched and ggplot2_0.9.3.1

mylabels <- c(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!

Thanks for providing complete example.

> 
> Many thanks,
> 
> Tom
> 
> _

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list