以r的方式添加文本注释

发布于 2025-02-07 04:17:41 字数 906 浏览 2 评论 0 原文

我试图在迭代地注释许多图。我希望注释在所有图上完全相同的位置出现,但是它们都具有不同的y轴尺度。如何在不使用Y值的情况下注释图表?必须有一种独立的方式,但我认为我缺少一个关键的词汇,因此我的搜索毫无结果。

通常,我会添加,例如, notate(“ text”,x = 5,y = 10) 到ggplot对象,但这是不起作用的,因为y的范围在组之间差异很大。

考虑到大约有30个地块可以跟踪,这是一个使用mtcars和ggpubr的玩具示例。

library(ggpubr)
ggboxplot(mtcars, x = "gear", y = "wt") +
  annotate("text", x = 2, y = 5, label = "I should be in the same place in both plots")
ggboxplot(mtcars, x = "gear", y = "disp") + 
  annotate("text", x = 2, y = 5, label = "I should be in the same place in both plots")

“

I am trying to annotate many plots iteratively. I want an annotation to appear in the exact same place on all graphs, but they all have different y-axis scales. How do I annotate a graph without using the y-values? There must be a data-independent way, but I think I am missing a key piece of vocabulary so my searching has been fruitless.

Normally, I would add, e.g., annotate("text", x = 5, y = 10) to the ggplot object, but this doesn't work because the range of y varies widely between groups.

Bearing in mind there are around 30 plots to keep track of, here is a toy example using mtcars and ggpubr.

library(ggpubr)
ggboxplot(mtcars, x = "gear", y = "wt") +
  annotate("text", x = 2, y = 5, label = "I should be in the same place in both plots")
ggboxplot(mtcars, x = "gear", y = "disp") + 
  annotate("text", x = 2, y = 5, label = "I should be in the same place in both plots")

wt

disp

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

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

发布评论

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

评论(3

朮生 2025-02-14 04:17:42

您可以从 patchwork 中使用 inset_element 来覆盖仅由彼此绘图的标签组成的绘图。

library(ggpubr)
library(patchwork)

label <- ggplot() + 
  annotate("text", x = 2, y = 5, label = "I should be in the same place in both plots") +
  coord_cartesian(clip = "off") +   # allows text to overflow if needed
  theme_void()
ggboxplot(mtcars, x = "gear", y = "wt") +
  inset_element(label, left = 0.4, bottom = 0.7, right = 0.8, top = 0.8)
ggboxplot(mtcars, x = "gear", y = "disp") + 
  inset_element(label, left = 0.4, bottom = 0.7, right = 0.8, top = 0.8)

label_layer <- inset_element(label, left = 0.4, bottom = 0.7, 
                             right = 0.8, top = 0.8) 

ggboxplot(mtcars, x = "gear", y = "wt") + label_layer

You could use inset_element from patchwork to overlay a plot consisting of just the label on top of each other plot.

library(ggpubr)
library(patchwork)

label <- ggplot() + 
  annotate("text", x = 2, y = 5, label = "I should be in the same place in both plots") +
  coord_cartesian(clip = "off") +   # allows text to overflow if needed
  theme_void()
ggboxplot(mtcars, x = "gear", y = "wt") +
  inset_element(label, left = 0.4, bottom = 0.7, right = 0.8, top = 0.8)
ggboxplot(mtcars, x = "gear", y = "disp") + 
  inset_element(label, left = 0.4, bottom = 0.7, right = 0.8, top = 0.8)

enter image description here

enter image description here

We could go one step further and define:

label_layer <- inset_element(label, left = 0.4, bottom = 0.7, 
                             right = 0.8, top = 0.8) 

Then each plot could be:

ggboxplot(mtcars, x = "gear", y = "wt") + label_layer
听风吹 2025-02-14 04:17:42

开发第一个 y by 100:

library(ggpubr)
ggboxplot(mtcars, x = "gear", y = "wt") +
  annotate("text", x = 2, y = 5/100, label = "I should be in the same place in both plots")
ggboxplot(mtcars, x = "gear", y = "disp") + 
  annotate("text", x = 2, y = 5, label = "I should be in the same place in both plots")

Devide the first y by 100:

library(ggpubr)
ggboxplot(mtcars, x = "gear", y = "wt") +
  annotate("text", x = 2, y = 5/100, label = "I should be in the same place in both plots")
ggboxplot(mtcars, x = "gear", y = "disp") + 
  annotate("text", x = 2, y = 5, label = "I should be in the same place in both plots")

enter image description here
enter image description here

戏蝶舞 2025-02-14 04:17:42

您可以将轴最大值的一部分传递。
它并不总是使它们始终处于同一位置,但是它很近。

library(ggpubr)
ggboxplot(mtcars, x = "gear", y = "wt") +
  annotate("text", y = max(mtcars$wt)*.9, x = 2, label = "I should be in the same place in both plots")
ggboxplot(mtcars, x = "gear", y = "disp") + 
  annotate("text", y = max(mtcars$disp)*.9, x = 2, label = "I should be in the same place in both plots")

”在此处输入图像说明”

You can pass a percentage of the maximum value of the axis.
It doesn't get them always in the same spot but it gets pretty close.

library(ggpubr)
ggboxplot(mtcars, x = "gear", y = "wt") +
  annotate("text", y = max(mtcars$wt)*.9, x = 2, label = "I should be in the same place in both plots")
ggboxplot(mtcars, x = "gear", y = "disp") + 
  annotate("text", y = max(mtcars$disp)*.9, x = 2, label = "I should be in the same place in both plots")

enter image description here

enter image description here

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