如何在ggplot2中使用希腊符号?

发布于 2024-10-21 11:49:44 字数 212 浏览 6 评论 0 原文

我的类别需要用希腊字母命名。我正在使用 ggplot2,它与数据配合得很好。不幸的是,我无法弄清楚如何将这些希腊符号放在 x 轴上(在刻度线处)并使它们出现在图例中。有什么办法可以做到吗?

更新: 我查看了链接,但是,没有描述好的方法来完成我想要的事情做。

My categories need to be named with Greek letters. I am using ggplot2, and it works beautifully with the data. Unfortunately I cannot figure out how to put those greek symbols on the x axis (at the tick marks) and also make them appear in the legend. Is there any way to do it?

UPDATE:
I had a look at the link, however, there is no good method described to accomplish what I want to do.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

無處可尋 2024-10-28 11:49:44

这里有一个链接,指向一个优秀的 wiki,解释了如何在 ggplot2 中添加希腊符号。总之,以下是获取希腊符号文本标签的方法

  1. :在geom_textannotateparse = T >。
  2. 轴标签:使用表达式(alpha)获取希腊字母。
  3. 构面标签:构面内使用labeller = label_parsed
  4. 图例标签:在图例标签中使用bquote(alpha == .(value))

您可以在链接

EDIT 中查看这些选项的详细用法。沿刻度线使用希腊符号的目标可以如下实现。

require(ggplot2);
data(tips);
p0 = qplot(sex, data = tips, geom = 'bar');
p1 = p0 + scale_x_discrete(labels = c('Female' = expression(alpha),
                                      'Male'   = expression(beta)));
print(p1);

有关执行此操作时可用的各种符号以及如何使用它们的完整文档,请参阅 ?plotmath

Here is a link to an excellent wiki that explains how to put greek symbols in ggplot2. In summary, here is what you do to obtain greek symbols

  1. Text Labels: Use parse = T inside geom_text or annotate.
  2. Axis Labels: Use expression(alpha) to get greek alpha.
  3. Facet Labels: Use labeller = label_parsed inside facet.
  4. Legend Labels: Use bquote(alpha == .(value)) in legend label.

You can see detailed usage of these options in the link

EDIT. The objective of using greek symbols along the tick marks can be achieved as follows

require(ggplot2);
data(tips);
p0 = qplot(sex, data = tips, geom = 'bar');
p1 = p0 + scale_x_discrete(labels = c('Female' = expression(alpha),
                                      'Male'   = expression(beta)));
print(p1);

For complete documentation on the various symbols that are available when doing this and how to use them, see ?plotmath.

愛上了 2024-10-28 11:49:44

最简单的解决方案:使用 Unicode 字符

无需表达式或其他包。
不确定这是否是 ggplot 的新功能,但它有效。
它还可以轻松混合希腊语和常规文本(例如在刻度中添加“*”)

只需在文本字符串中使用 unicode 字符即可。
似乎对于我能想到的所有选项都很有效。
编辑:以前它在构面标签中不起作用。这显然已经在某个时候得到了修复。

library(ggplot2)
ggplot(mtcars, 
       aes(mpg, disp, color=factor(gear))) + 
  geom_point() + 
  labs(title="Title (\u03b1 \u03a9)", # works fine
       x= "\u03b1 \u03a9 x-axis title",    # works fine
       y= "\u03b1 \u03a9 y-axis title",    # works fine
       color="\u03b1 \u03a9 Groups:") +  # works fine
  scale_x_continuous(breaks = seq(10, 35, 5), 
                     labels = paste0(seq(10, 35, 5), "\u03a9*")) + # works fine; to label the ticks
  ggrepel::geom_text_repel(aes(label = paste(rownames(mtcars), "\u03a9*")), size =3) + # works fine 
  facet_grid(~paste0(gear, " Gears \u03a9"))

reprex 包 (v0.3.0)

Simplest solution: Use Unicode Characters

No expression or other packages needed.
Not sure if this is a newer feature for ggplot, but it works.
It also makes it easy to mix Greek and regular text (like adding '*' to the ticks)

Just use unicode characters within the text string.
seems to work well for all options I can think of.
Edit: previously it did not work in facet labels. This has apparently been fixed at some point.

library(ggplot2)
ggplot(mtcars, 
       aes(mpg, disp, color=factor(gear))) + 
  geom_point() + 
  labs(title="Title (\u03b1 \u03a9)", # works fine
       x= "\u03b1 \u03a9 x-axis title",    # works fine
       y= "\u03b1 \u03a9 y-axis title",    # works fine
       color="\u03b1 \u03a9 Groups:") +  # works fine
  scale_x_continuous(breaks = seq(10, 35, 5), 
                     labels = paste0(seq(10, 35, 5), "\u03a9*")) + # works fine; to label the ticks
  ggrepel::geom_text_repel(aes(label = paste(rownames(mtcars), "\u03a9*")), size =3) + # works fine 
  facet_grid(~paste0(gear, " Gears \u03a9"))

Created on 2019-08-28 by the reprex package (v0.3.0)

自我难过 2024-10-28 11:49:44

使用表达式(delta),其中“delta”表示小写的δ,“Delta”表示大写的Δ

以下是希腊字符的完整列表:

ααα
β β β
γ 伽玛
Δ δ δ
Ε ε epsilon
Ζ zeta
η eta
θ θ θ
ι ι iota
κκκ
Λ λ 拉姆达
μμμ
Ν ν nu
Ξ Ξ xi
Ο ο omicron
π π pi
Ρ ρ rho
Σ σ 西格玛
τ τ τ
Υ υ upsilon
Φ Φ phi
Χ χ chi
ψ ψ psi
Ω Ω 欧米茄

编辑:从评论中复制,与其他单词结合使用时,请使用:表达式(Delta*"price")

Use expression(delta) where 'delta' for lowercase δ and 'Delta' to get capital Δ.

Here's full list of Greek characters:

Α α alpha
Β β beta
Γ γ gamma
Δ δ delta
Ε ε epsilon
Ζ ζ zeta
Η η eta
Θ θ theta
Ι ι iota
Κ κ kappa
Λ λ lambda
Μ μ mu
Ν ν nu
Ξ ξ xi
Ο ο omicron
Π π pi
Ρ ρ rho
Σ σ sigma
Τ τ tau
Υ υ upsilon
Φ φ phi
Χ χ chi
Ψ ψ psi
Ω ω omega

EDIT: Copied from comments, when using in conjunction with other words use like: expression(Delta*"price")

孤千羽 2024-10-28 11:49:44

您不需要 latex2exp 包来完成您想做的事情。下面的代码就可以解决这个问题。

ggplot(smr, aes(Fuel.Rate, Eng.Speed.Ave., color=Eng.Speed.Max.)) + 
  geom_point() + 
  labs(title=expression("Fuel Efficiency"~(alpha*Omega)), 
color=expression(alpha*Omega), x=expression(Delta~price))

输入图像描述这里

此外,一些评论(目前尚未得到答复)询问是否在希腊字母后添加星号 (*)。 表达式(alpha~"*") 有效,所以我建议尝试一下。

更多评论询问如何获取Δ Price,我发现实现这一目标的最直接方法是表达式(Delta~price))。如果需要在希腊字母前添加一些内容,也可以这样做:
表达式(Indicative~Delta~price) 得到:

在此处输入图像描述

You do not need the latex2exp package to do what you wanted to do. The following code would do the trick.

ggplot(smr, aes(Fuel.Rate, Eng.Speed.Ave., color=Eng.Speed.Max.)) + 
  geom_point() + 
  labs(title=expression("Fuel Efficiency"~(alpha*Omega)), 
color=expression(alpha*Omega), x=expression(Delta~price))

enter image description here

Also, some comments (unanswered as of this point) asked about putting an asterisk (*) after a Greek letter. expression(alpha~"*") works, so I suggest giving it a try.

More comments asked about getting Δ Price and I find the most straightforward way to achieve that is expression(Delta~price)). If you need to add something before the Greek letter, you can also do this:
expression(Indicative~Delta~price) which gets you:

enter image description here

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