英文:
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't know how to automatically pick scale for object of type <expression>. 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.
>
Here is my reproducible example code:
library(ggrepel)
library(ggplot2)
data <- data.frame(x = 1:3, # Create example data
y = 1:3,
label = LETTERS[1:3])
data
hh<-c(expression("ABC"^"+"),
expression("EFG"^"+"),
expression("HJK"^"+"))
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
-
hh
不需要是表达式,它只需要包含表达式的文本。我们添加geom_text_repel(..., parse=TRUE)
来正确地将这些character
字符串视为表达式。 -
我认为更好的方式是将该表达式定义为
'ABC^"+'
或"'ABC^'+'"
,以便+
不被解释为数学运算符。 -
Technique,我更喜欢将标签作为框架的一部分,所以我会将它添加到
data
,覆盖你的label
。(但这当然不是必需的,只是在我看来更“清晰”)。
data <- data.frame(x = 1:3, # 创建示例数据
y = 1:3,
label = c("ABC^'+'", "EFG^'+'", "HJK^'+'"))
set.seed(42) # 由于ggrepel是随机的,生产环境中不需要
ggplot(data = data,aes(x = x, y = y)) +
geom_point() +
geom_text_repel(aes(label = label), parse = TRUE)
(我仅添加了 set.seed(42)
,这样你可以获得完全相同的图像,因为排斥动作是随机的。我建议除非你找到一个特定的种子,以便以你喜欢的方式布置标签,否则不要使用 set.seed
。不设置种子时,每次调用它时,你会得到略有不同的图。)
英文:
hh
doesn't need to be expressions, it just needs to contain the text of the expression. We addgeom_text_repel(..., parse=TRUE)
so that it treats thecharacter
strings as expressions correctly.- I think the better way to define that expression is as
'ABC^"+"'
or"ABC^'+'"
so that the+
is not interpreted as the math operator. - Technique, I prefer to have the labels as part of the frame, so I'll add it to
data
, overriding yourlabel
. (But this is certainly not required, it's just "cleaner", in my opinion.)
data <- data.frame(x = 1:3, # Create example data
y = 1:3,
label = c("ABC^'+'", "EFG^'+'", "HJK^'+'"))
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)
(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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论