Tom Walker
2014-May-06 21:51 UTC
[R] Mix of plain and italic text in ggplot categorical x-axis
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
David Winsemius
2014-May-06 22:08 UTC
[R] Mix of plain and italic text in ggplot categorical x-axis
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