如何在ggplot2中加粗和斜体化y轴标签的部分?

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

How to bold and italicize parts of the y label axis in ggplot2?

问题

我想让y轴上的文本显示为粗体索引(B),即文本的一部分是粗体,另一部分是斜体。我尝试了以下代码,但出现了错误:

set.seed(123)
df <- data.frame(x.axis = rep(c("A","B","C","D"), each = 50),
                 y.axis = rnorm(1000, 1.2, 2))

ggplot(aes(x = x.axis, y = y.axis), data = df) + 
  geom_point(aes(x = x.axis, y = y.axis), color = "gray")+
  stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.3, colour="black") + 
  stat_summary(fun.y = mean, color = "black", geom ="point", size = 5,show.legend = FALSE) + 
  theme_classic() +
  theme(aspect.ratio = 7/5)  + geom_hline(yintercept = 1) +
  ylab(expression(bold("Bold")~italic("text (B)")))

错误信息:unexpected symbol in:

  ylab(expression(bold("Bold")~italic("text (B)")))
英文:

I want the yaxis to read as Boldindex (B) i.e. part of the text that is bolded and the other italicized. I tried the following based on previous examples but get errors

set.seed(123)
df &lt;- data.frame(x.axis = rep(c(&quot;A&quot;,&quot;B&quot;,&quot;C&quot;,&quot;D&quot;), each = 50),
             y.axis = rnorm(1000, 1.2, 2))

 ggplot(aes(x = x.axis, y = y.axis), data = df) + 
  geom_point(aes(x = x.axis, y = y.axis), color = &quot;gray&quot;)+
  stat_summary(fun.data=mean_cl_boot, geom=&quot;errorbar&quot;, width=0.3, colour=&quot;black&quot;) + 
  stat_summary(fun.y = mean, color = &quot;black&quot;, geom =&quot;point&quot;, size = 5,show.legend = FALSE) + 
  theme_classic() +
  theme(aspect.ratio = 7/5)  + geom_hline(yintercept = 1) +
  ylab(expression(bold(paste(Bold)))text (B))


Error: unexpected symbol in:
&quot;  theme(aspect.ratio = 7/5)  + geom_hline(yintercept = 1) +
  ylab(expression(bold(paste(Bold))text (B)&quot;

答案1

得分: 1

这可以通过使用 plotmath 来实现。我不确定你对要加粗和斜体的内容有多清楚,但这应该为你提供一个实现你要求的框架。
一般原则:

  • 加粗(这段文字将会加粗)
  • 斜体(这段文字将会斜体)
  • ~ 符号用于表示空格
  • * 符号用于区分两个表达式,它们之间没有空格
  • 将括号 "(" 用引号括起来,因为这些是特殊字符

我专注于回答中的 y 轴表达式,以删除所有似乎与绘图无关的输入。

library(ggplot2)

ggplot() + 
  geom_point(aes(x = 1, y = 1)) +
  theme_classic() +
  ylab(expression(bold(Bold)*index~&quot;(&quot;*italic(B)*&quot;)&quot;))

如何在ggplot2中加粗和斜体化y轴标签的部分?

创建于2023年06月25日,使用 reprex v2.0.2

英文:

This can be achieved with plotmath. I'm not sure if I'm completely clear for what you want in bold and italic but this should give you a framework to do what you want.
The general principles:

  • bold(this text will be bold)
  • italic(this text will be italic)
  • ~ symbol for a space
  • * symbol to distinguish between one expression and another without a space.
  • "(" brackets in quotation marks as these are special characters

I've focused on the y axis expression in the answer to remove all the seemingly non relevant inputs to the plot.

library(ggplot2)

ggplot() + 
  geom_point(aes(x = 1, y = 1))+
  theme_classic() +
  ylab(expression(bold(Bold)*index~&quot;(&quot;*italic(B)*&quot;)&quot;))

如何在ggplot2中加粗和斜体化y轴标签的部分?<!-- -->

<sup>Created on 2023-06-25 with reprex v2.0.2</sup>

答案2

得分: 0

尝试使用 bquote

p + ylab(bquote(bold(Bold)~'指数 (B)'))

数据:

library(ggplot2)
p <- ggplot(aes(x = x.axis, y = y.axis), data = df) + 
  geom_point(aes(x = x.axis, y = y.axis), color = "gray") +
  stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.3, colour="black") + 
  stat_summary(fun.y = mean, color = "black", geom ="point", size = 5, show.legend = FALSE) + 
  theme_classic() +
  theme(aspect.ratio = 7/5) + geom_hline(yintercept = 1)

如何在ggplot2中加粗和斜体化y轴标签的部分?

英文:

Try bquote.

p + ylab(bquote(bold(Bold)~&#39;index (B)&#39;))

如何在ggplot2中加粗和斜体化y轴标签的部分?


Data:

library(ggplot2)
p &lt;- ggplot(aes(x = x.axis, y = y.axis), data = df) + 
  geom_point(aes(x = x.axis, y = y.axis), color = &quot;gray&quot;)+
  stat_summary(fun.data=mean_cl_boot, geom=&quot;errorbar&quot;, width=0.3, colour=&quot;black&quot;) + 
  stat_summary(fun.y = mean, color = &quot;black&quot;, geom =&quot;point&quot;, size = 5,show.legend = FALSE) + 
  theme_classic() +
  theme(aspect.ratio = 7/5)  + geom_hline(yintercept = 1) 

huangapple
  • 本文由 发表于 2023年6月25日 20:58:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76550520.html
匿名

发表评论

匿名网友

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

确定