Home > Back-end >  plotting geom_text() with free scale facet_wrap
plotting geom_text() with free scale facet_wrap

Time:05-11

I would like to plot geom_text() in a facet_wrap with scale = free. I tried to use geom_blank() or, set each height on each graph, but it was not successful. Would you possibly tell me how to plot geom_text() in the right bottom in each figure.

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()
        )

enter image description here

CodePudding user response:

By setting the aes(x, y) parameters to positive or negative Inf inside geom_text, we can have text labels on the lower right bottom of each facet. The extra hjust and vjust adjust the position of the label so that they would be in the panel.

Here I use the diamonds dataset as an example, and the data for geom_text is called 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)

Created on 2022-05-10 by the reprex package (v2.0.1)

  • Related