英文:
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 <- 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(paste(Bold)))text (B))
Error: unexpected symbol in:
" theme(aspect.ratio = 7/5) + geom_hline(yintercept = 1) +
ylab(expression(bold(paste(Bold))text (B)"
答案1
得分: 1
这可以通过使用 plotmath
来实现。我不确定你对要加粗和斜体的内容有多清楚,但这应该为你提供一个实现你要求的框架。
一般原则:
- 加粗(这段文字将会加粗)
- 斜体(这段文字将会斜体)
~
符号用于表示空格*
符号用于区分两个表达式,它们之间没有空格- 将括号 "(" 用引号括起来,因为这些是特殊字符
我专注于回答中的 y 轴表达式,以删除所有似乎与绘图无关的输入。
library(ggplot2)
ggplot() +
geom_point(aes(x = 1, y = 1)) +
theme_classic() +
ylab(expression(bold(Bold)*index~"("*italic(B)*")"))
创建于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~"("*italic(B)*")"))
<!-- -->
<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)
英文:
Try bquote
.
p + ylab(bquote(bold(Bold)~'index (B)'))
Data:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论