如何使用ggplot2在单个标签中使用斜体和正常字体

发布于 2025-02-08 12:57:26 字数 1199 浏览 2 评论 0原文

我想制作一个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)

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坏尐絯 2025-02-15 12:57:26

在您的表达式中使用星号(*),而不是tilde()。 Tilde符号指定一个空间,而星号将充当不空间分离器。

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)

Use asterisk (*) instead of tilde (~) in your expressions. The tilde symbol specifies a space, whereas the asterisk will act as a no-space separator.

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)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文