ggplot2
的stat_bin
函数的文档指出,它返回一个带有附加列的新数据框。实际如何访问此数据帧?
是否可以?
simple <- data.frame(x = rep(1:10, each = 2))
tmp <- stat_bin(data=simple, binwidth=0.1, aes(x))
我已经知道
tmp
是一个环境,并且ls(tmp)
将显示环境中的对象,但是在探究了每个对象之后,我看不到任何类似于返回值的内容。
最佳答案
正如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
关于r - ggplot2返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12267970/