使用自由标度 facet_wrap 绘制 geom_text()

plotting geom_text() with free scale facet_wrap

我想在 facet_wrap 中绘制 geom_text(),比例为自由。 我尝试使用 geom_blank() 或在每个图形上设置每个高度,但没有成功。 你能告诉我如何在每个图中的右下角绘制 geom_text() 吗?

z_cor <- fit01_varsize2 %>%
  filter(!variable1 == "intercept") %>%
  group_by(variable1) %>% 
#  mutate(height = max(value_with) + .3 * sd(value_with)) %>% 
  ggplot(aes(x = value_without, y = value_with))+
  geom_point(aes(color = value), shape = 1)+
#  geom_blank(aes(x = 1, y = 1)) +
  geom_text(
    data    = data.frame(variable1   = c("Agricultural_land", "Artificial_land", "Precipitation", "Protected_area",
                                         "RiverLake", "Seashore", "Temperature", "Volcanic_area", "Wasteland"),
                         label = c("TRUE:FALSE = 694:316", "TRUE:FALSE = 698:312", "TRUE:FALSE = 733:277", "TRUE:FALSE = 864:146",
                                   "TRUE:FALSE = 721:289", "TRUE:FALSE = 739:271", "TRUE:FALSE = 657:353", "TRUE:FALSE = 748:262", "TRUE:FALSE = 707:303")),
    aes(x = 0.1, y = 0.1, label = label))+
  geom_abline(intercept = 0, slope = 1, linetype = "dashed") +
  scale_color_manual(values = c("TRUE" = "salmon", "FALSE" = "steelblue"))+
  #  geom_smooth(method = "lm",colour= "deepskyblue3")+
  #  ggpubr::stat_cor(method="pearson", label.y.npc="top", label.x.npc = "center")+
  facet_wrap(.~variable1, scales = "free")+
  theme(strip.text.x = element_text(size = 20),
        axis.title=element_text(size=16),
      axis.line = element_line(colour="grey40"),
      axis.title.y = element_blank(),
      axis.title.x = element_blank(),
      legend.position = "bottom",
      panel.background = element_rect(fill = "transparent",
                                      colour = "transparent",
                                      size = 0.5, linetype = "solid"),
      plot.background = element_rect(fill = "transparent",
                                     colour = "transparent"),
      panel.grid.major = element_blank(), 
      panel.grid.minor = element_blank()
        )

[![在此处输入图片描述][1]][1]

通过在 geom_text 中将 aes(x, y) 参数设置为正或负 Inf,我们可以在每个面的右下角都有文本标签。额外的 hjustvjust 调整标签的位置,以便它们在面板中。

这里我以diamonds数据集为例,geom_text的数据叫做diamonds_label.

library(ggplot2)
diamonds_label <- data.frame(clarity = unique(diamonds$clarity), label = LETTERS[1:8])

ggplot(diamonds, aes(x, y)) + 
  geom_point() +
  facet_wrap(.~clarity, scale = "free") +
  geom_text(data = diamonds_label, aes(Inf, -Inf, label = label), 
            col = "red", 
            hjust = 1, 
            vjust = -1)

reprex package (v2.0.1)

于 2022-05-10 创建