Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
297 views
in Technique[技术] by (71.8m points)

r - Do I need to reshape this wide data to effectively use ggplot2?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...