如何使用Facet_wrap在数字上添加不同的注释

发布于 2025-01-23 01:10:21 字数 1320 浏览 5 评论 0原文

我想对facet_wrap产生的数字添加不同的注释。

我尝试了以下代码,但是它显示出错误,例如“ fun中的错误(x [i],...):对象'值'未找到”。

dat_text <- data.frame(
  label = c("TRUE:FALSE = 686:324", "TRUE:FALSE = 976:34", "TRUE:FALSE = 516:494", "TRUE:FALSE = 360:650",
            "TRUE:FALSE = 351:659", "TRUE:FALSE = 440:570", "TRUE:FALSE = 645:365", "TRUE:FALSE = 151:859", "TRUE:FALSE = 542:468"),
  cyl   = c(Agricultural_land, Artificial_land, Precipitation, Protected_area,
            RiverLake, Seashore, Temperature, Volcanic_area, Wasteland)
)


z_cor <- fit01_zsize2 %>%
  ggplot(aes(x = value_without, y = value_with, color = value))+
  geom_point(shape = 1)+
   geom_text(
     data    = dat_text,
     mapping = aes(x = -Inf, y = -Inf, label = label),
     hjust   = -0.1,
     vjust   = -1
   )+
  geom_abline(intercept = 0, slope = 1, linetype = "dashed") +
  scale_color_manual(values = c("TRUE" = "salmon", "FALSE" = "steelblue"))+
  facet_wrap(.~variable1)+
  theme(strip.text.x = element_text(size = 20),
            axis.title=element_text(size=16))

plot(z_cor)

当我在没有GEOM_TEXT()的情况下尝试相同的代码时,它起作用。


当我避免错误时,新问题就会出现。 我粘贴了我创建的数字。许多注释都在数字上。

I would like to add different annotations on figures produced by facet_wrap.

I tried the below code, but the it showed error such as "Error in FUN(X[[i]], ...) : object 'value' not found".

dat_text <- data.frame(
  label = c("TRUE:FALSE = 686:324", "TRUE:FALSE = 976:34", "TRUE:FALSE = 516:494", "TRUE:FALSE = 360:650",
            "TRUE:FALSE = 351:659", "TRUE:FALSE = 440:570", "TRUE:FALSE = 645:365", "TRUE:FALSE = 151:859", "TRUE:FALSE = 542:468"),
  cyl   = c(Agricultural_land, Artificial_land, Precipitation, Protected_area,
            RiverLake, Seashore, Temperature, Volcanic_area, Wasteland)
)


z_cor <- fit01_zsize2 %>%
  ggplot(aes(x = value_without, y = value_with, color = value))+
  geom_point(shape = 1)+
   geom_text(
     data    = dat_text,
     mapping = aes(x = -Inf, y = -Inf, label = label),
     hjust   = -0.1,
     vjust   = -1
   )+
  geom_abline(intercept = 0, slope = 1, linetype = "dashed") +
  scale_color_manual(values = c("TRUE" = "salmon", "FALSE" = "steelblue"))+
  facet_wrap(.~variable1)+
  theme(strip.text.x = element_text(size = 20),
            axis.title=element_text(size=16))

plot(z_cor)

When I tried the same code without geom_text(), it worked.


When I could avoid the error, the new problem has come.
I pasted the figure I created. Many annotations has come in a figure.
enter image description here

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

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

发布评论

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

评论(1

荒路情人 2025-01-30 01:10:21

正如@stefan所指出的那样,您的文本层的数据应引用faceTing变量:

ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~am) +
  geom_text(data = data.frame(wt = 2, mpg = c(30, 12), am = c(0,1),
                              label = c("one note", "and another")),
            aes(label = label))

”在此处输入图像说明”

As @stefan noted, your text layer's data should reference the faceting variable:

ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~am) +
  geom_text(data = data.frame(wt = 2, mpg = c(30, 12), am = c(0,1),
                              label = c("one note", "and another")),
            aes(label = label))

enter image description here

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