extract input data from ggplot object

后端 未结 2 838
孤城傲影
孤城傲影 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:22

    p itself is a list, you can get df with p$data.

    A samll example:

    library(ggplot2)
    p <- ggplot(mtcars, aes(x = mpg, y = cyl)) + geom_point()
    identical(p$data, mtcars)
    # [1] TRUE
    

提交回复
热议问题