我的类别需要用希腊字母命名。我正在使用 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.
发布评论
评论(4)
这里有一个链接,指向一个优秀的 wiki,解释了如何在 ggplot2 中添加希腊符号。总之,以下是获取希腊符号文本标签的方法
geom_text
或annotateparse = T
>。表达式(alpha)
获取希腊字母。构面
内使用labeller = label_parsed
。bquote(alpha == .(value))
。您可以在链接
EDIT 中查看这些选项的详细用法。沿刻度线使用希腊符号的目标可以如下实现。
有关执行此操作时可用的各种符号以及如何使用它们的完整文档,请参阅
?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
parse = T
insidegeom_text
orannotate
.expression(alpha)
to get greek alpha.labeller = label_parsed
insidefacet
.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
For complete documentation on the various symbols that are available when doing this and how to use them, see
?plotmath
.最简单的解决方案:使用 Unicode 字符
无需
表达式
或其他包。不确定这是否是 ggplot 的新功能,但它有效。
它还可以轻松混合希腊语和常规文本(例如在刻度中添加“*”)
只需在文本字符串中使用 unicode 字符即可。
似乎对于我能想到的所有选项都很有效。
编辑:以前它在构面标签中不起作用。这显然已经在某个时候得到了修复。
由 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.
Created on 2019-08-28 by the reprex package (v0.3.0)
使用
表达式(delta)
,其中“delta”表示小写的δ
,“Delta”表示大写的Δ
。以下是希腊字符的完整列表:
编辑:从评论中复制,与其他单词结合使用时,请使用:
表达式(Delta*"price")
Use
expression(delta)
where 'delta' for lowercaseδ
and 'Delta' to get capitalΔ
.Here's full list of Greek characters:
EDIT: Copied from comments, when using in conjunction with other words use like:
expression(Delta*"price")
您不需要
latex2exp
包来完成您想做的事情。下面的代码就可以解决这个问题。此外,一些评论(目前尚未得到答复)询问是否在希腊字母后添加星号 (*)。
表达式(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.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 isexpression(Delta~price))
. If you need to add something before the Greek letter, you can also do this:expression(Indicative~Delta~price)
which gets you: