如何使用ggplot2在单个标签中使用斜体和正常字体
我想制作一个ggplot2,在同一标签中可以使用斜体字体和普通字体,例如:wordx and wordy, word in Italic中的,而x and y在正常字体中: word x and word y
我尝试了类似:
data("mtcars")
mtcars$Type <- as.factor(c(rep("wordX", 16), rep("wordY", 16)))
head(mtcars, 3)
mpg cyl disp hp drat wt qsec vs am gear carb Type
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 wordX
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 wordX
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 wordX
lbs = brk = levels(mtcars$Type)
lbs[match("wordX", brk)] = expression(italic("word")~X)
lbs[match("wordY", brk)] = expression(italic("word")~Y)
ggplot(mtcars, aes(factor(am), mpg, fill=Type)) +
geom_col(position="dodge") +
theme(legend.text = element_text(size = 18),
legend.title = element_text(size = 18)) +
scale_fill_discrete(breaks=brk, labels=lbs)
//i.sstatic.net/tj92f.png“ alt =“在 分开;如何将其绘制在一起 word x和 word y
我该如何制作?
谢谢 !!!
I want to make a ggplot2 that in the same labels could use italic and normal font, example: wordX and wordY, word in italic, and X and Y in normal font: wordX and wordY
I tried something like:
data("mtcars")
mtcars$Type <- as.factor(c(rep("wordX", 16), rep("wordY", 16)))
head(mtcars, 3)
mpg cyl disp hp drat wt qsec vs am gear carb Type
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 wordX
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 wordX
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 wordX
lbs = brk = levels(mtcars$Type)
lbs[match("wordX", brk)] = expression(italic("word")~X)
lbs[match("wordY", brk)] = expression(italic("word")~Y)
ggplot(mtcars, aes(factor(am), mpg, fill=Type)) +
geom_col(position="dodge") +
theme(legend.text = element_text(size = 18),
legend.title = element_text(size = 18)) +
scale_fill_discrete(breaks=brk, labels=lbs)
in the example label word X and word Y
are separated; how can I plot it together wordX and wordY
How can I make it ?
Thanks !!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的表达式中使用星号(
*
),而不是tilde(〜
)。 Tilde符号指定一个空间,而星号将充当不空间分离器。Use asterisk (
*
) instead of tilde (~
) in your expressions. The tilde symbol specifies a space, whereas the asterisk will act as a no-space separator.