我是否需要重新整理这些宽泛的数据才能有效地使用ggplot2? [英] Do I need to reshape this wide data to effectively use ggplot2?
问题描述
年份甲壳类鳕鱼吞拿鱼鲱鱼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
的快速指针将列名变成因子会受到大大的赞赏。
melt
从重塑/ 2包)就足够了。我会这样做$ p $ lt; code> library(reshape2)
qplot(Year,value,color = variable,data = melt(df ,'Year'),geom ='line')
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.
A simple transformation using melt
(from the reshape/2 package) would suffice. I would do
library(reshape2)
qplot(Year, value, colour = variable, data = melt(df, 'Year'), geom = 'line')
这篇关于我是否需要重新整理这些宽泛的数据才能有效地使用ggplot2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!