Annotating text on individual facet in ggplot2

前端 未结 6 824
野的像风
野的像风 2020-11-22 09:39

I want to annotate some text on last facet of the plot with the following code:

library(ggplot2)
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
p <-          


        
6条回答
  •  伪装坚强ぢ
    2020-11-22 10:37

    Here's the plot without text annotations:

    library(ggplot2)
    
    p <- ggplot(mtcars, aes(mpg, wt)) +
      geom_point() +
      facet_grid(. ~ cyl) +
      theme(panel.spacing = unit(1, "lines"))
    p
    

    Let's create an additional data frame to hold the text annotations:

    dat_text <- data.frame(
      label = c("4 cylinders", "6 cylinders", "8 cylinders"),
      cyl   = c(4, 6, 8)
    )
    p + geom_text(
      data    = dat_text,
      mapping = aes(x = -Inf, y = -Inf, label = label),
      hjust   = -0.1,
      vjust   = -1
    )
    

    Alternatively, we can manually specify the position of each label:

    dat_text <- data.frame(
      label = c("4 cylinders", "6 cylinders", "8 cylinders"),
      cyl   = c(4, 6, 8),
      x     = c(20, 27.5, 25),
      y     = c(4, 4, 4.5)
    )
    
    p + geom_text(
      data    = dat_text,
      mapping = aes(x = x, y = y, label = label)
    )
    

    We can also label plots across two facets:

    dat_text <- data.frame(
      cyl   = c(4, 6, 8, 4, 6, 8),
      am    = c(0, 0, 0, 1, 1, 1)
    )
    dat_text$label <- sprintf(
      "%s, %s cylinders",
      ifelse(dat_text$am == 0, "automatic", "manual"),
      dat_text$cyl
    )
    p +
      facet_grid(am ~ cyl) +
      geom_text(
        size    = 5,
        data    = dat_text,
        mapping = aes(x = Inf, y = Inf, label = label),
        hjust   = 1.05,
        vjust   = 1.5
      )
    

    Notes:

    • You can use -Inf and Inf to position text at the edges of a panel.
    • You can use hjust and vjust to adjust the text justification.
    • The text label data frame dat_text should have a column that works with your facet_grid() or facet_wrap().

提交回复
热议问题