6

我想知道是否可以在 ggplot2 图中获取转换后的数据?特别是,我有兴趣获取点图中点的坐标和大小,以便在另一个绘图库 ( d3.js) 中使用它们。我怎样才能提取这些信息?

这是情节:

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

现在我可以使用得到原始数据,g$data但我想要转换后的数据(图中点的坐标)。

谢谢!

4

1 回答 1

9

使用ggplot_build然后提取数据。试试这个:

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,此功能仅在ggplot20.9.0 版中可用

于 2012-08-31T15:53:50.333 回答