ggplot:用plotmath表达式标记点

huangapple go评论81阅读模式
英文:

ggplot: label points with plotmath expressions

问题

我尝试了几种方法来在R中的绘图中添加带有其他字符串的上标“+”作为点标签,但都失败了。

例如:

我知道如何在下方添加单个上标“+”:
plot(1:10, xlab = expression("ABC"^"+"))

当我只想通过xlab添加x轴上的上标“+”时,它确实有效。

但我想要添加带有上标标签的多个点,例如expression("EFG"^"+"),expression("HIJ"^"+")等等。

当我将它放入ggplot2时,出现了错误信息:

不知道如何自动选择对象类型的比例。 默认为连续。
geom_text_repel()中的错误:
!计算美学时出现问题。
i第2层中发生错误。
compute_aesthetics()中的错误引起:
!美学无效的数据列。
x以下美学无效:
x label = hh
i您是否输错了数据列的名称或忘记添加after_stat()
运行rlang::last_trace()以查看错误发生的位置。

这是我的可重现示例代码:

library(ggrepel)
library(ggplot2)
data <- data.frame(x = 1:3, # 创建示例数据
y = 1:3,
label = LETTERS[1:3])
data

hh<-c(expression("ABC"^"+"),
expression("EFG"^"+"),
expression("HJK"^"+"))
plot(data$x, # 绘制图表
data$y,
xlim = c(1, 4))
text(data$x, # 添加标签
data$y,
labels = hh,
pos = 4)

ggplot(data = data, aes(x = x, y = y)) +
geom_point() +
geom_text_repel(aes(label = hh))

我希望有人可以给我一些建议。提前致谢。

英文:

I tried several methods to add superscript "+" with other strings as point labels in plot in R but it failed.

for example:

I know how to add single superscript "+" below:
plot(1:10,xlab = expression("ABC"^"+"))

It really work when I just want to add superscript "+" on x axis through xlab.

But I want to add multiple points with superscript labels, such as expression("EFG"^"+"), expression("HIJ"^"+") and so on.

When I put it into ggplot2, error information appeared:

Don&#39;t know how to automatically pick scale for object of type &lt;expression&gt;. Defaulting to continuous.
Error in `geom_text_repel()`:
! Problem while computing aesthetics.
i Error occurred in the 2nd layer.
Caused by error in `compute_aesthetics()`:
! Aesthetics are not valid data columns.
x The following aesthetics are invalid:
x `label = hh`
i Did you mistype the name of a data column or forget to add `after_stat()`?
Run `rlang::last_trace()` to see where the error occurred.
&gt; 

Here is my reproducible example code:

library(ggrepel)
library(ggplot2)
data &lt;- data.frame(x = 1:3,                 # Create example data
                   y = 1:3,
                   label = LETTERS[1:3])
data  

hh&lt;-c(expression(&quot;ABC&quot;^&quot;+&quot;),
      expression(&quot;EFG&quot;^&quot;+&quot;),
      expression(&quot;HJK&quot;^&quot;+&quot;))
plot(data$x,                                # Draw plot
     data$y,
     xlim = c(1, 4))
text(data$x,                                # Add labels
     data$y,
     labels = hh,
     pos = 4)

ggplot(data = data,aes(x=x,y=y))+
  geom_point()+
  geom_text_repel(aes(label = hh))

I hope somebody can give me some advice. Thanks in advance.

答案1

得分: 2

  1. hh 不需要是表达式,它只需要包含表达式的文本。我们添加 geom_text_repel(..., parse=TRUE) 来正确地将这些 character 字符串视为表达式。

  2. 我认为更好的方式是将该表达式定义为 'ABC^"+'"'ABC^'+'",以便 + 不被解释为数学运算符。

  3. Technique,我更喜欢将标签作为框架的一部分,所以我会将它添加到 data,覆盖你的 label。(但这当然不是必需的,只是在我看来更“清晰”)。

data &lt;- data.frame(x = 1:3,                 # 创建示例数据
                   y = 1:3,
                   label = c(&quot;ABC^&#39;+&#39;&quot;, &quot;EFG^&#39;+&#39;&quot;, &quot;HJK^&#39;+&#39;&quot;))
set.seed(42) # 由于ggrepel是随机的,生产环境中不需要
ggplot(data = data,aes(x = x, y = y)) +
  geom_point() +
  geom_text_repel(aes(label = label), parse = TRUE)

ggplot:用plotmath表达式标记点

(我仅添加了 set.seed(42),这样你可以获得完全相同的图像,因为排斥动作是随机的。我建议除非你找到一个特定的种子,以便以你喜欢的方式布置标签,否则不要使用 set.seed。不设置种子时,每次调用它时,你会得到略有不同的图。)

英文:
  1. hh doesn't need to be expressions, it just needs to contain the text of the expression. We add geom_text_repel(..., parse=TRUE) so that it treats the character strings as expressions correctly.
  2. I think the better way to define that expression is as &#39;ABC^&quot;+&quot;&#39; or &quot;ABC^&#39;+&#39;&quot; so that the + is not interpreted as the math operator.
  3. Technique, I prefer to have the labels as part of the frame, so I'll add it to data, overriding your label. (But this is certainly not required, it's just "cleaner", in my opinion.)
data &lt;- data.frame(x = 1:3,                 # Create example data
                   y = 1:3,
                   label = c(&quot;ABC^&#39;+&#39;&quot;, &quot;EFG^&#39;+&#39;&quot;, &quot;HJK^&#39;+&#39;&quot;))
set.seed(42) # since ggrepel is stochastic, not required in production
ggplot(data = data,aes(x = x, y = y)) +
  geom_point() +
  geom_text_repel(aes(label = label), parse = TRUE)

ggplot:用plotmath表达式标记点

(I added set.seed(42) purely so that you can get exactly this image, since the repel action is random. I recommend not using set.seed unless you find one specific seed which lays out the labels in a way you prefer. Without setting the seed, you will get a slightly-different plot each time you call it.)

答案2

得分: 1

我更喜欢r2evans的答案,但这里有另一种使用annotate的方法:

set.seed(1)
ggplot(data = data, aes(x = x, y = y)) +
    geom_point() +
    annotate(geom = "text_repel", x = data$x, y = data$y, label = hh)

ggplot:用plotmath表达式标记点

英文:

I prefer r2evans' answer, but here's another way using annotate:

set.seed(1)
ggplot(data = data,aes(x=x,y=y))+
    geom_point()+
    annotate(geom = &quot;text_repel&quot;, x = data$x, y = data$y, label = hh)

ggplot:用plotmath表达式标记点

huangapple
  • 本文由 发表于 2023年7月3日 22:27:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76605697.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定