accessing data of the plot within ggtitle

后端 未结 2 1367
失恋的感觉
失恋的感觉 2021-02-08 23:16

I am wondering if it is somehow possible to access the columns of the provided data within a ggplot2 graph for the title. So something like that:

gg         


        
2条回答
  •  孤城傲影
    2021-02-08 23:39

    Here's two ways I've done this using split. You can use split to split your dataframe into a named list of dataframes, based on a variable. So calling split(mpg, .$manufacturer) gives you a list of dataframes, where each dataframe is associated with a manufacturer, e.g. split_df$audi is the dataframe of all observations made by Audi.

    library(dplyr)
    library(purrr)
    library(ggplot2)
    
    split_df <- split(mpg, .$manufacturer)
    

    First way you could do this is to just call ggplot on a single item in the list. Since the list is named, names(split_df)[1] will give you the name, "audi".

    ggplot(split_df[[1]], aes(x = hwy, y = displ, label = model)) + 
        geom_point() + 
        geom_text(data = . %>% filter(hwy > 28)) +
        ggtitle(names(split_df)[1])
    

    That's kinda cumbersome, especially if you want plots for multiple manufacturers. When I've done this, I've used the map functions from purrr. What's really cool is imap, which maps over both the list and its names. Here I'm making a list of plots by mapping over the list of dataframes; each plot gets a title from the name of that list item.

    plots <- imap(split_df, function(df, manufacturer) {
        ggplot(df, aes(x = hwy, y = displ, label = model)) +
            geom_point() +
            geom_text(data = . %>% filter(hwy > 28)) +
            ggtitle(manufacturer)
    })
    
    plots$audi
    

    Then I can pull up a specific item from that list of plots. This is also handy for if you need to use walk to map over them and save every plot or a subset of plots, or if you need to use a grid function to arrange them into output, or really anything else cool that purrr is great for.

提交回复
热议问题