本文介绍了ggplot2返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 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()会显示出它的最后一行,它无形地返回表格,所以它可以通过赋值进一步捕获) $ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' b $ bx 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 The documentation for ggplot2's stat_bin function states that it returns a new data frame with additional columns. How does one actually get access to this data frame?Is it possible?simple <- data.frame(x = rep(1:10, each = 2))tmp <- stat_bin(data=simple, binwidth=0.1, aes(x))I have figured out that tmp is an environment, and ls(tmp) will show what objects are in the environment, but after exploring each of these objects, I'm not seeing anything like what is described as a return value. 解决方案 As Luciano Selzer mentions, the calculations that produce the table shown below aren't performed until print time. (A look at ggplot2:::print.ggplot() will show that, in its final line, it returns the table invisibly, so it can be captured by assignment for further examination.)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 这篇关于ggplot2返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!