我可以避免在 ggplot2 中使用数据框吗?
我正在运行蒙特卡罗模拟,输出的形式为:
> d = data.frame(iter=seq(1, 2), k1 = c(0.2, 0.6), k2=c(0.3, 0.4))
> d
iter k1 k2
1 0.2 0.3
2 0.6 0.4
我想要生成的图是:
plot(d$iter, d$k1)
plot(density(d$k1))
我知道如何使用 ggplot2 绘制等效图,转换为数据框,
new_d = data.frame(iter=rep(d$iter, 2),
k = c(d$k1, d$k2),
label = rep(c('k1', 'k2'), each=2))
然后绘图很容易。然而,迭代次数可能非常大,并且 k 的数量也可能很大。这意味着要处理一个非常大的数据框。
无论如何我可以避免创建这个新的数据框吗?
谢谢
I'm running a monte-carlo simulation and the output is in the form:
> d = data.frame(iter=seq(1, 2), k1 = c(0.2, 0.6), k2=c(0.3, 0.4))
> d
iter k1 k2
1 0.2 0.3
2 0.6 0.4
The plots I want to generate are:
plot(d$iter, d$k1)
plot(density(d$k1))
I know how to do equivalent plots using ggplot2, convert to data frame
new_d = data.frame(iter=rep(d$iter, 2),
k = c(d$k1, d$k2),
label = rep(c('k1', 'k2'), each=2))
then plotting is easy. However the number of iterations can be very large and the number of k's can also be large. This means messing about with a very large data frame.
Is there anyway I can avoid creating this new data frame?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
简短的回答是“不”,您无法避免创建数据框。 ggplot 要求数据位于数据框中。如果您使用 qplot,您可以为 x 和 y 提供单独的向量,但在内部,它仍然根据您传入的参数创建一个数据框。
我同意 juba 的建议 - 学习使用
reshape
函数,或者更好的是带有melt
/cast
函数的reshape
包。一旦您能够快速将数据以长格式存储,那么创建令人惊叹的 ggplot 图表就更近了一步!Short answer is "no," you can't avoid creating a data frame.
ggplot
requires the data to be in a data frame. If you useqplot
, you can give it separate vectors for x and y, but internally, it's still creating a data frame out of the parameters you pass in.I agree with juba's suggestion -- learn to use the
reshape
function, or better yet thereshape
package withmelt
/cast
functions. Once you get fast with putting your data in long format, creating amazingggplot
graphs becomes one step closer!是的,您可以避免创建数据框:只需为基础层 ggplot() 提供一个空参数列表即可。以下是基于您的代码的完整示例:
library(ggplot2)
请注意,尽管这允许您不创建数据框,但仍可能在内部创建数据框。例如,请参阅以下
?geom_point
摘录:Yes, it is possible for you to avoid creating a data frame: just give an empty argument list to the base layer,
ggplot()
. Here is a complete example based on your code:library(ggplot2)
Note that although this allows for you not to create a data frame, a data frame might still be created internally. See, e.g., the following extract from
?geom_point
:您可以使用
reshape
函数将数据框转换为“长”格式。也许它比你的代码快一点?You can use the
reshape
function to transform your data frame to "long" format. May be it is a bit faster than your code ?所以只是补充一下之前的答案。使用 qplot,您可以这样做
,然后从那里进一步构建它,例如
但我同意 - 熔化/铸造通常是前进的方向。
So just to add to the previous answers. With qplot you could do
and then from there building it further, e.g. with
But I agree - melt/cast is genereally the way forward.
只需传递 NULL 作为数据框,并使用数据向量定义必要的美感。简单示例:
Just pass NULL as the data frame, and define the necessary aesthetics using the data vectors. Quick example: