extract input data from ggplot object

后端 未结 2 811
孤城傲影
孤城傲影 2021-01-14 13:00

I know I can extract the data I plot in a ggplot2 plot using

p <- ggplot(df, aes(x, y)) + geom_point()
ggplot_build(p)$data

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-14 13:27

    The same approach but wrapped in a package/function is to 'capture the spirit of your ggplot calls' using library(ggghost)

    library(ggghost)
    library(ggplot2)
    
    df <- data.frame(x = 1:20, y = 1:20, z= letters[1:20])
    
    p %g<% ggplot(data = df, aes(x, y))
    p <- p + geom_point()
    
    rm(df)
    ggghost::recover_data(p)
    ## this returns the data back to your environment
    

提交回复
热议问题