为什么 geom_text() 多次绘制文本?
Why is geom_text() plotting the text several times?
请考虑以下最小示例:
library(ggplot2)
library(ggrepel)
ggplot(mtcars) +
aes(x = mpg, y = qsec) +
geom_line() +
geom_text(x = 20, y = 20, label = "(20,20)")
我猜你可以很容易地看出文本“(20,20)”被严重过度绘制(实际上,我不知道这是不是正确的词。我的意思是文本被一次绘制了好几次位置)。
如果我使用 annotate()
,这不会发生:
ggplot(mtcars) +
aes(x = mpg, y = qsec) +
geom_line() +
annotate("text", x = 20, y = 20, label = "(20,20)")
"So, why don't you use annotate()
then?" 你可能会问。其实我不想用文字做注解,而是标签。而且我还想使用 {ggrepel} 包来避免过度绘制。但是看看会发生什么,当我尝试这个时:
ggplot(mtcars) +
aes(x = mpg, y = qsec) +
geom_line() +
geom_label_repel(x = 20, y = 20, label = "(20,20)")
同样,绘制了许多标签,{ggrepel} 在防止它们重叠方面做得很好。但我只想要 one 标签指向特定位置。我真的不明白为什么会这样。我只为 x
、y
和 label
各提供了一个值。我还尝试了 data = NULL
和 inherit.aes = F
并将值放入 geom_label_repel()
内的 aes()
无效。我怀疑标签的数量与 mtcars
中的行一样多。对于我的实际应用程序来说,这真的很糟糕,因为我在相应的数据集中有很多行。
你能帮我解决这个问题吗?也许可以简短地解释一下为什么会发生这种情况以及为什么你的解决方案有效?非常感谢!
geom_text
或 geom_label_repel
每行添加一个标签。因此,您可以为注释几何提交一个单独的数据集。例如:
library(ggplot2)
library(ggrepel)
ggplot(mtcars, aes(mpg, qsec)) +
geom_line() +
geom_label_repel(aes(20, 20, label = "(20,20)"), data.frame())
将"check_overlap = TRUE"添加到geom_text以防止重叠。
library(ggplot2)
ggplot(mtcars) +
aes(x = mpg, y = qsec) +
geom_line() +
geom_text(x = 20, y = 20, label = "(20,20)", check_overlap = TRUE)
请考虑以下最小示例:
library(ggplot2)
library(ggrepel)
ggplot(mtcars) +
aes(x = mpg, y = qsec) +
geom_line() +
geom_text(x = 20, y = 20, label = "(20,20)")
我猜你可以很容易地看出文本“(20,20)”被严重过度绘制(实际上,我不知道这是不是正确的词。我的意思是文本被一次绘制了好几次位置)。
如果我使用 annotate()
,这不会发生:
ggplot(mtcars) +
aes(x = mpg, y = qsec) +
geom_line() +
annotate("text", x = 20, y = 20, label = "(20,20)")
"So, why don't you use annotate()
then?" 你可能会问。其实我不想用文字做注解,而是标签。而且我还想使用 {ggrepel} 包来避免过度绘制。但是看看会发生什么,当我尝试这个时:
ggplot(mtcars) +
aes(x = mpg, y = qsec) +
geom_line() +
geom_label_repel(x = 20, y = 20, label = "(20,20)")
同样,绘制了许多标签,{ggrepel} 在防止它们重叠方面做得很好。但我只想要 one 标签指向特定位置。我真的不明白为什么会这样。我只为 x
、y
和 label
各提供了一个值。我还尝试了 data = NULL
和 inherit.aes = F
并将值放入 geom_label_repel()
内的 aes()
无效。我怀疑标签的数量与 mtcars
中的行一样多。对于我的实际应用程序来说,这真的很糟糕,因为我在相应的数据集中有很多行。
你能帮我解决这个问题吗?也许可以简短地解释一下为什么会发生这种情况以及为什么你的解决方案有效?非常感谢!
geom_text
或 geom_label_repel
每行添加一个标签。因此,您可以为注释几何提交一个单独的数据集。例如:
library(ggplot2)
library(ggrepel)
ggplot(mtcars, aes(mpg, qsec)) +
geom_line() +
geom_label_repel(aes(20, 20, label = "(20,20)"), data.frame())
将"check_overlap = TRUE"添加到geom_text以防止重叠。
library(ggplot2)
ggplot(mtcars) +
aes(x = mpg, y = qsec) +
geom_line() +
geom_text(x = 20, y = 20, label = "(20,20)", check_overlap = TRUE)