I'm using ggplot2 to graph boxplots of patient BMIs, and I'm having trouble with the text in the x axis. I'd like to include the units for BMI (kg/m2) and I'd like the "2" to be superscripted. When I make my graph like this:
require(plyr)
require(ggplot2)
Weights <- data.frame(SubjectID = 1:500,
Weight = rnorm(500, 28, 7))
Weights$Cat <- cut(Weights$Weight, breaks = c(0, 18.5, 25, 30, 40, Inf),
right = FALSE)
Weights$BMIcat <- revalue(Weights$Cat,
c("[0,18.5)" = "Underweight\n(<18.5 kg/m2)",
"[18.5,25)" = "Normal weight\n(18.5 to 24.9\nkg/m2)",
"[25,30)" = "Overweight\n(25 to 29.9\nkg/m2)",
"[30,40)" = "Obese\n(30 to 39.9\nkg/m2)",
"[40,Inf)" = "Severely obese\n(>40 kg/m2)"))
ggplot(Weights, aes(x = BMIcat, y = Weight)) +
geom_boxplot() +
xlab("BMI category") + ylab("Weight (kg)")
everything looks BEAUTIFUL except that the 2's aren't superscripted. I'm trying to use expression to make this work, and here's the best I've come up with:
ggplot(Weights, aes(x = Cat, y = Weight)) +
geom_boxplot() +
scale_x_discrete(breaks=c("[0,18.5)", "[18.5,25)", "[25,30)",
"[30,40)", "[40,Inf)"),
labels=c(
expression("Underweight\n(<18.5 kg/m"^2*")"),
expression("Normal weight\n(18.5 to 24.9\nkg/m"^2*")"),
expression("Overweight\n(25 to 29.9\nkg/m"^2*")"),
expression("Obese\n(30 to 39.9\nkg/m"^2*")"),
expression("Severely obese\n(>40 kg/m"^2*")"))) +
xlab("BMI category") + ylab("Weight (kg)")
Now, my 2s are superscripted but everything else looks TERRIBLE. It looks like it's suddenly left or maybe full justified instead of centered, the 2's are sometimes really far from the m's, and the axis text overlaps the graph! Any suggestions?


