I am trying to label my bars with labels that include one italicized letter and line breaks.
When I use the expression function to successfully italicize one letter of my labels, I get this weird effect where the line break (\n) makes the bar labels move above the x-axis and the alignment of the labels becomes staggered rather than centered.
Any help on successfully labeling my bars with one italicized letter and line breaks would be greatly appreciated.
# transform mtcars to look more like my data
mtcars$trans <- factor(mtcars$am, labels = c("auto", "manual"))
mtcars$origin <- factor(mtcars$cyl, labels = c("JPN", "GER", "USA"))
# create a data frame of means, SEs, and bar label variable (e.g., predictor)
d <- Rmisc::summarySE(subset(mtcars, origin !='USA'), 'mpg', c('trans', 'origin'))
d$predictor <- with(d, paste(trans, origin, sep='\n+\n'))
# Create expression list of bar labels with one letter italicized
ital_labels <- expression(
paste("auto\n+\n", italic("J"), "PN"),
paste("auto\n+\n", italic("G"), "ER"),
paste("manual\n+\n", italic("J"), "PN"),
paste("manual\n+\n", italic("G"), "ER")
)
# Plot it
ggplot(data=d, aes(x=predictor, y=mpg)) +
geom_bar(aes(fill=trans),
position=position_dodge(),
stat="identity", colour="black", size=0) +
geom_errorbar(aes(ymin=mpg-se, ymax=mpg+se),
size=.4, width=.1, position=position_dodge(9)) +
scale_x_discrete("\n(Error bars represent standard errors)",
labels = ital_labels) +
ylab("\nMiles per Gallon\n")