Managing superscripts in axis labels with ggplot

8,630 views
Skip to first unread message

Deirdre Griffin

unread,
Nov 2, 2015, 2:21:31 PM11/2/15
to Davis R Users' Group
Hi all,

I'm trying to create an axis label in ggplot2 that is the following:   PMN (μg NH4+-N g-1 dry soil)

I've been successful, except for the -N part following NH4+. It keeps printing as NH4+-N.  

Anyone know how to fix this issue? 

Below is my code and my graph. Let me know if you need more info.

Thanks in advance for the help! 
Deirdre

ggplot(PMN_all, aes(x = Fertilizer, y = PMN_Diff)) +
  geom_boxplot(aes(fill = Biochar)) + 
  labs(x = "Fertilizer type", 
       y = bquote('PMN ('*mu*'g '*NH[4]^+ ~-N~g^-1 ~ dry ~ soil*')'),
       fill = "Biochar") +
  scale_x_discrete(labels = c("Compost", "Mineral")) + 
  scale_fill_manual(values = c("grey90", "grey40"), labels = c("No", "Yes")) + 
  facet_wrap(~ Date) + 
  theme_bw() + 
  theme(axis.title.x = element_text(size = 20), 
           axis.title.y = element_text(face = "bold", size = 20),
           axis.text.x = element_text(size = 12),
           axis.text.y = element_text(size = 10),
        panel.grid.minor = element_blank(), panel.grid.major = element_blank(),
        strip.text.x = element_text(size = 15))


plot_zoom_png.png

Jaime Ashander

unread,
Nov 2, 2015, 2:27:44 PM11/2/15
to davi...@googlegroups.com
Putting backticks around the + will work, I think:

 labs(y=bquote('PMN ('*mu*'g '*NH[4]^`+` ~ -N~g^-1 ~ dry ~ soil*')'))

--
Check out our R resources at http://www.noamross.net/davis-r-users-group.html
---
You received this message because you are subscribed to the Google Groups "Davis R Users' Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to davis-rug+...@googlegroups.com.
Visit this group at http://groups.google.com/group/davis-rug.
For more options, visit https://groups.google.com/d/optout.

Katherine Ransom

unread,
Nov 2, 2015, 2:51:53 PM11/2/15
to davis-rug
Hi Deirdre,
I frequently need to write NO3-N in my axis labels, and I use expression(): xlab=expression("NO"[3]*"-N mg/L")
--
--
Katherine Ransom
PhD Candidate
Hydrologic Sciences Graduate Group
UC Davis

Hannah Waterhouse

unread,
Nov 2, 2015, 2:52:27 PM11/2/15
to davi...@googlegroups.com
this is what I did - not sure it will help:

labs(y=expression("Seasonal Flux (kg " *N[2]*"O-N/ha)"))

On Mon, Nov 2, 2015 at 11:21 AM, Deirdre Griffin <degr...@ucdavis.edu> wrote:

Tim Bowles

unread,
Nov 2, 2015, 2:53:46 PM11/2/15
to davi...@googlegroups.com
And to chime in with one more example:

labs(y=expression(paste(mu,'g ',NH[4]^{'+'},' - N ',g^{-1},' soil')))
--
Timothy Bowles, Ph.D.
Department of Land, Air, and Water Resources
University of California Davis

Deirdre Griffin

unread,
Nov 2, 2015, 3:03:36 PM11/2/15
to Davis R Users' Group
Wow, thanks all! You're awesome. Problem solved!
Reply all
Reply to author
Forward
0 new messages