ggplot2返回值

rmf*_*ght 10 r ggplot2

为文档ggplot2stat_bin功能说,它返回与其他列新的数据帧.如何实际访问此数据框?

可能吗?

simple <- data.frame(x = rep(1:10, each = 2))
tmp <- stat_bin(data=simple, binwidth=0.1, aes(x))
Run Code Online (Sandbox Code Playgroud)

我已经发现这tmp是一个环境,ls(tmp)并将显示环境中的对象,但在探索了这些对象之后,我没有看到任何类似于被描述为返回值的内容.

Jos*_*ien 9

正如Luciano Selzer所提到的,产生下表所示的计算在打印时间之前不会执行.(看一下ggplot2:::print.ggplot()将会显示,在最后一行中,它会无形地返回表格,因此可以通过赋值进行进一步检查.)

tmp <- ggplot(data=simple) + stat_bin(aes(x), binwidth=0.1)
x <- print(tmp)
head(x[["data"]][[1]])
#   y count    x ndensity ncount density PANEL group ymin ymax xmin xmax
# 1 0     0 0.95        0      0       0     1     1    0    0  0.9  1.0
# 2 2     2 1.05        1      1       1     1     1    0    2  1.0  1.1
# 3 0     0 1.15        0      0       0     1     1    0    0  1.1  1.2
# 4 0     0 1.25        0      0       0     1     1    0    0  1.2  1.3
# 5 0     0 1.35        0      0       0     1     1    0    0  1.3  1.4
# 6 0     0 1.45        0      0       0     1     1    0    0  1.4  1.5
Run Code Online (Sandbox Code Playgroud)

  • 尝试记录现有的内部结构并没有多大意义,因为即使我不太了解它们.对于下一个版本,Winston和我将进行重写,应该更简单,更容易扩展(并且可以调用所有stat函数来获得绘图之外的结果) (2认同)

sho*_*eth 1

在 中ggplot2 (3.3.5),我无法访问返回的数据帧ggplot2:::print.ggplot(),但我获得了成功ggplot_build

simple <- data.frame(x = rep(1:10, each = 2))
tmp <- ggplot(data=simple) + stat_bin(aes(x), binwidth=0.1)
# with ggplot_build
x <- ggplot_build(tmp)
head(x[["data"]][[1]])
#   y count   x xmin xmax density ncount ndensity flipped_aes PANEL group ymin ymax colour   fill size linetype alpha
# 1 2     2 1.0 0.95 1.05       1      1        1       FALSE     1    -1    0    2     NA grey35  0.5        1    NA
# 2 0     0 1.1 1.05 1.15       0      0        0       FALSE     1    -1    0    0     NA grey35  0.5        1    NA
# 3 0     0 1.2 1.15 1.25       0      0        0       FALSE     1    -1    0    0     NA grey35  0.5        1    NA
# 4 0     0 1.3 1.25 1.35       0      0        0       FALSE     1    -1    0    0     NA grey35  0.5        1    NA
# 5 0     0 1.4 1.35 1.45       0      0        0       FALSE     1    -1    0    0     NA grey35  0.5        1    NA
# 6 0     0 1.5 1.45 1.55       0      0        0       FALSE     1    -1    0    0     NA grey35  0.5        1    NA
Run Code Online (Sandbox Code Playgroud)