Is it possible to get the data of the converted graph? (for example, coordinates of points in a graph of points, density curve)

I was wondering if it is possible to get the converted data into ggplot2 graphs? In particular, I am interested in getting the coordinates and size of points in points, so that I can use them in another graphics library ( d3.js). How can I extract this information?

Here is the plot:

g=ggplot(data.frame(x=rnorm(100)), aes(x = x)) + geom_dotplot()

Now I can get the source data using g$data, but I want the converted data (the coordinates of the points on the graph).

Thank!

+5
source share
1 answer

Use ggplot_buildand then retrieve data. Try the following:

gg <- ggplot_build(g)

, , :

str(gg, max.level=1)
List of 3
 $ data :List of 1
 $ panel:List of 5
  ..- attr(*, "class")= chr "panel"
 $ plot :List of 8
  ..- attr(*, "class")= chr "ggplot"

:

head(gg$data[[1]])
  y         x  binwidth count ncount     width PANEL group countidx stackpos      xmin      xmax ymin ymax
1 0 -2.070496 0.1356025     1  0.125 0.1356025     1     1        1      0.5 -2.138297 -2.002695    0    1
2 0 -1.781799 0.1356025     2  0.250 0.1356025     1     1        1      0.5 -1.849600 -1.713998    0    1
3 0 -1.781799 0.1356025     2  0.250 0.1356025     1     1        2      1.5 -1.849600 -1.713998    0    1
4 0 -1.619960 0.1356025     1  0.125 0.1356025     1     1        1      0.5 -1.687761 -1.552159    0    1
5 0 -1.403223 0.1356025     6  0.750 0.1356025     1     1        1      0.5 -1.471024 -1.335422    0    1
6 0 -1.403223 0.1356025     6  0.750 0.1356025     1     1        2      1.5 -1.471024 -1.335422    0    1

PS. AFAIK, ggplot2 0.9.0

+9

All Articles