我是否需要重塑这些广泛的数据才能有效地使用 ggplot2?
我有一个 data.frame,看起来像
Year Crustaceans Cod Tuna Herring Scorpion.fishes
1 1950 58578630 2716706 69690537 87161396 15250015
2 1951 59194582 3861166 34829755 51215349 15454659
3 1952 47562941 4396174 31061481 13962479 12541484
4 1953 68432658 3901176 23225423 13229061 9524564
5 1954 64395489 4412721 20798126 25285539 9890656
6 1955 76111004 4774045 13992697 18910756 8446391
还有几个物种(列),年份从 1950 年到 2006 年。我想用 ggplot2(我刚刚学习)来探索它。 我是否需要转换此数据,以便物种成为在该数据上有效使用 ggplot2 的一个因素?如果不需要,如何避免为每个物种单独创建一个图层?如果是,(或者实际上在任何一种情况下)使用 reshape
或 plyr
将列名称转换为因子的快速指南将不胜感激。
I have a data.frame that looks like
Year Crustaceans Cod Tuna Herring Scorpion.fishes
1 1950 58578630 2716706 69690537 87161396 15250015
2 1951 59194582 3861166 34829755 51215349 15454659
3 1952 47562941 4396174 31061481 13962479 12541484
4 1953 68432658 3901176 23225423 13229061 9524564
5 1954 64395489 4412721 20798126 25285539 9890656
6 1955 76111004 4774045 13992697 18910756 8446391
With several more species (columns), and years running from 1950 to 2006. I'd like to explore it with ggplot2 (which I'm just learning). Do I need to transform this data so that the species is a factor to effectively use ggplot2 on this data? If not, how do I avoid having to create a layer for each species individually? If yes, (or really in either case) a quick pointer on using reshape
or plyr
to turn column names into a factor would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
melt
(来自 reshape/2 包)进行简单的转换就足够了。我会做A simple transformation using
melt
(from the reshape/2 package) would suffice. I would do我发现以下链接对于学习重塑非常有帮助。一旦掌握了 Reshape 和 plyr 的工作方式(不一定是最快的(data.table 包是使用一些 C 语言编写的,所以速度更快)),它们就非常容易使用。本教程 pdf 是学习它的绝佳资源 中并一次运行一个脚本来查看结果。
另外,我建议将 example(cast) 中的行复制到脚本 pdf" rel="nofollow">http://had.co.nz/stat405/lectures/19-tables.pdf
I found the following link to be extremely helpful to learning reshape. Reshape and plyr are very easy to use functions once you have the format (not necessarily the fastest (data.table package is written using some C so it's much faster) of how they work down. This tutorial pdf is a great resource for learning it. Also I suggest copying the line from example(cast) into a script and running them one at a time to see the result.
http://had.co.nz/stat405/lectures/19-tables.pdf