replace the x axis with specific words

47 views
Skip to first unread message

ccw9...@gmail.com

unread,
Oct 22, 2015, 7:46:04 AM10/22/15
to ggplot2
Hi everyone,

I have a plot, while I wanna change the 3 groups in the x axis (1,2,3) in to some specific words, number 1 for Ba, 2 for Aa, 3 for Ca. while the a should be the subscript. I dont know how to do that ,thanks so much.


Dennis Murphy

unread,
Oct 22, 2015, 8:05:11 AM10/22/15
to ccw9...@gmail.com, ggplot2
Here's an example to generate mathematical labels - ignore the warning.

DF <- data.frame(x = factor(LETTERS[1:3]), y = c(20, 30, 10))

library(ggplot2)

# See ?plotmath for explanation
lbls <- c(expression(A[a]), expression(B[a]), expression(C[a]))

ggplot(DF, aes(x = x, y = y)) +
   geom_bar(stat = "identity", fill = "darkorange") +
   scale_x_discrete(labels = lbls)

The key point is that the labels should be put in a vector of expressions.

Dynamite plots? Sigh....

Dennis

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Hadley Wickham

unread,
Oct 22, 2015, 9:25:52 AM10/22/15
to Dennis Murphy, ccw9...@gmail.com, ggplot2
One tiny point: an expression() is a vectorised data structure, so you can just do:

lbls <- expression(A[a], B[a], C[a])

Hadley

Dennis Murphy

unread,
Oct 22, 2015, 3:52:28 PM10/22/15
to Hadley Wickham, ccw9...@gmail.com, ggplot2
I wondered about that but didn't follow through. I forgot that expression() was vectorized. Thanks, Hadley.

Dennis
Reply all
Reply to author
Forward
0 new messages