strip.text在labeller中无法正常工作 – facet_wrap ggplot2

huangapple go评论70阅读模式
英文:

strip.text does not work with labeller - facet_wrap ggplot2

问题

我正在使用facet_wrap,并需要在标题中使用表达式,所以我正在使用labeller。但是strip.text不起作用(我希望标题加粗)。请看以下代码:

这段代码会产生加粗的标题,如预期所示:

strip.text在labeller中无法正常工作 – facet_wrap ggplot2

但是,如果我使用labeller添加表达式:

vars2plot <- c(expression(paste("Hypol. ",O[2])),expression(paste("Epil. ",PO[4]^"3-")),
               expression(paste("Epil. ",NH[4]^"+")), expression(paste("Hypol. ",NO[3]^"-")))
names(vars2plot) <- vars
df1$variable <- factor(df1$variable, levels = vars, labels = vars2plot)

ggplot(df1, aes(y= scenario, x=pct)) +
  geom_col(aes( fill = scenario), position = position_dodge()) + 
  facet_wrap(vars(variable), labeller = labeller(variable=label_parsed)) +
  theme (strip.text = element_text(face = "bold"))

加粗效果不会出现:

strip.text在labeller中无法正常工作 – facet_wrap ggplot2

有关如何使标题加粗的任何想法吗?

英文:

I am using facet_wrap and need expression in header so I am using labeller. But then strip.text does not work (I want the header to be in bold). see code:

sc &lt;- c(&quot;Ref&quot;,&quot;DJ&quot;,&quot;DT&quot;,&quot;DTHW&quot;)
vars &lt;- c(&quot;O2&quot;,&quot;PO4&quot;,&quot;NH4&quot;,&quot;NO3&quot;)
df1 &lt;- data.frame(scenario=rep(sc,4),variable=rep(vars,each=4),pct=runif(16,-10,10))

ggplot(df1, aes(y= scenario, x=pct)) +
  geom_col(aes( fill = scenario), position = position_dodge()) + 
  facet_wrap(vars(variable)) +
  theme (strip.text = element_text(face = &quot;bold&quot;))

This gives bold header as expected:

strip.text在labeller中无法正常工作 – facet_wrap ggplot2

But if I add the expression with labeller:

vars2plot &lt;- c(expression(paste(&quot;Hypol. &quot;,O[2])),expression(paste(&quot;Epil. &quot;,PO[4]^&quot;3-&quot;)),
               expression(paste(&quot;Epil. &quot;,NH[4]^&quot;+&quot;)), expression(paste(&quot;Hypol. &quot;,NO[3]^&quot;-&quot;)))
names(vars2plot) &lt;- vars
df1$variable &lt;- factor(df1$variable, levels = vars, labels = vars2plot)

ggplot(df1, aes(y= scenario, x=pct)) +
  geom_col(aes( fill = scenario), position = position_dodge()) + 
  facet_wrap(vars(variable), labeller = labeller(variable=label_parsed)) +
  theme (strip.text = element_text(face = &quot;bold&quot;))

The bold does not appear:

strip.text在labeller中无法正常工作 – facet_wrap ggplot2

Any ideas how to make the header bold?

答案1

得分: 3

如果您使用表达式,那么需要使用 plotmath 语法来使文本加粗。您不能使用字体属性来改变外观。您可以使用以下方式将您的表达式包装在 bold() 中:

vars2plot <- c(expression(paste("Hypol. ",O[2])), expression(paste("Epil. ",PO[4]^"3-")),
               expression(paste("Epil. ",NH[4]^"+")), expression(paste("Hypol. ",NO[3]^"-")))

# 或者简化为 ...
# vars2plot <- expression(
#   "Hypol. " ~ O[2],
#   "Epil. " ~ PO[4]^"3-",
#   "Epil. " ~ NH[4]^"+",
#   "Hypol. " ~ NO[3]^"-")

# 现在将它们全部加粗
vars2plot <- as.expression(lapply(vars2plot, function(x) bquote(bold(.(x)))))

这将输出如下图所示的加粗表达式:
strip.text在labeller中无法正常工作 – facet_wrap ggplot2

英文:

If you use expressions, then you need to use the plotmath syntax to make things bold. You cannot use font properties to alter the appearance. You can wrap your expressions in bold() with

vars2plot &lt;- c(expression(paste(&quot;Hypol. &quot;,O[2])),expression(paste(&quot;Epil. &quot;,PO[4]^&quot;3-&quot;)),
               expression(paste(&quot;Epil. &quot;,NH[4]^&quot;+&quot;)), expression(paste(&quot;Hypol. &quot;,NO[3]^&quot;-&quot;)))

# or simplified ...
# vars2plot &lt;- expression(
#   &quot;Hypol. &quot; ~ O[2],
#   &quot;Epil. &quot; ~ PO[4]^&quot;3-&quot;,
#   &quot;Epil. &quot; ~ NH[4]^&quot;+&quot;,
#   &quot;Hypol. &quot; ~ NO[3]^&quot;-&quot;)

# now make them all bold
vars2plot &lt;- as.expression(lapply(vars2plot, function(x) bquote(bold(.(x)))))

which will output
strip.text在labeller中无法正常工作 – facet_wrap ggplot2

huangapple
  • 本文由 发表于 2023年7月10日 21:37:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76654308.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定