Extract data from a ggplot

前端 未结 3 1185
猫巷女王i
猫巷女王i 2020-11-27 03:49

I have made a plot using ggplot2 geom_histogram from a data frame. See sample below and link to the ggplot histogram Need to label each geom_vline

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-27 04:13

    To get values actually plotted you can use function ggplot_build() where argument is your plot.

    p <- ggplot(mtcars,aes(mpg))+geom_histogram()+
          facet_wrap(~cyl)+geom_vline(data=data.frame(x=c(20,30)),aes(xintercept=x))
    
    pg <- ggplot_build(p)
    

    This will make list and one of sublists is named data. This sublist contains dataframe with values used in plot, for example, for histrogramm it contains y values (the same as count). If you use facets then column PANEL shows in which facet values are used. If there are more than one geom_ in your plot then data will contains dataframes for each - in my example there is one dataframe for histogramm and another for vlines.

    head(pg$data[[1]])
      y count         x ndensity ncount density PANEL group ymin ymax
    1 0     0  9.791667        0      0       0     1     1    0    0
    2 0     0 10.575000        0      0       0     1     1    0    0
    3 0     0 11.358333        0      0       0     1     1    0    0
    4 0     0 12.141667        0      0       0     1     1    0    0
    5 0     0 12.925000        0      0       0     1     1    0    0
    6 0     0 13.708333        0      0       0     1     1    0    0
          xmin     xmax
    1  9.40000 10.18333
    2 10.18333 10.96667
    3 10.96667 11.75000
    4 11.75000 12.53333
    5 12.53333 13.31667
    6 13.31667 14.10000
    
    head(pg$data[[2]])
      xintercept PANEL group xend  x
    1         20     1     1   20 20
    2         30     1     1   30 30
    3         20     2     2   20 20
    4         30     2     2   30 30
    5         20     3     3   20 20
    6         30     3     3   30 30
    

提交回复
热议问题