R语言中复杂轴标签上标位置修正及大小调整求助
Hey there! Let's sort out that frustrating tiny superscript problem you're dealing with. I totally get it—getting subscripts and superscripts aligned correctly and sized properly can feel like a puzzle when you're new to R's grid graphics and expression syntax.
The Root of the Problem
Your earlier fix for the superscript position ("sponge"^"-1") worked for alignment, but putting the text in quotes told R to treat it as a literal string. This makes the superscript inherit a smaller default size instead of matching your main label font.
The Solution: Clean Expression Syntax
Instead of nesting the superscript inside the subscript quotes, use R's native expression syntax to define the structure clearly. This ensures all parts of the label (main text, subscripts, superscripts) inherit the same font size you set.
Here's how to adjust your code:
Build the correct axis label expression
Use*to connect symbols (likemu) to text, and~to add spaces between terms. This lets R properly parse the subscript and superscript hierarchy:# Define the label with proper expression structure y_axis_label <- expression(mu*mol~C~or~N[tracer]~mmol[sponge]^-1~incubation^-1)This tells R:
mu*mol: Attach the mu symbol to "mol" (no space)~C~or~N[tracer]: Add spaces around "C or N", with "tracer" as a subscript~mmol[sponge]^-1: Add a space before "mmol", with "sponge" as a subscript and-1as a superscript after the subscript~incubation^-1: Add a space before "incubation" with-1as a superscript
Plug it into textGrob with matching font size
Use the expression in yourtextGrob, and set thefontsizeto match the axis text size you defined in your ggplot theme (you used 14 for axis.text and axis.title—keep it consistent!):# Combine your plot with the fixed label b <- grid.arrange(BN, left = textGrob(y_axis_label, x = 0.5, rot = 90, hjust = 0.38, gp = gpar(fontsize = 14)))
Why This Works
By using R's native expression syntax instead of quoted strings, you're letting R recognize the label as a mathematical expression. This means all elements (subscripts, superscripts, main text) will inherit the font size you specify in gpar(), so your -1 superscripts will be the same size as the rest of the label, while staying in the correct position.
Quick Tip for Consistency
Since you set axis.title.y = element_blank() in your ggplot theme, matching the textGrob font size to your axis.text size (14) will keep your plot looking polished and cohesive.
内容的提问来源于stack exchange,提问作者Meggie

