MUHC-Research
2009-Apr-30 19:03 UTC
[R] Overlaying graphs from different datasets with ggplot
Dear R-users, I recently began using the ggplot2 package and I am still in the process of getting used to it. My goal would be to plot on the same grid a number of curves derived from two distinct datasets. The first dataset (called molten.data) looks like this : Column names : Perc, Week, Weight P10 21 333.3554 P90 21 486.0480 P10 22 452.6347 P90 22 563.8263 P10 23 575.0960 P90 23 661.6841 P10 24 700.4449 P90 24 779.4067 P10 25 828.4966 P90 25 917.1222 The second dataset (called skj) looks like this: Column names : Week, Perc, Weight 21 1 317.5 22 1 392.5 23 1 467.5 24 1 542.5 25 1 617.5 26 1 697.5 21 2 535.0 22 2 632.5 23 2 737.5 24 2 855.0 25 2 980.0 26 2 1115.0 21 3 425.0 22 3 512.5 23 3 602.5 24 3 697.5 25 3 800.0 26 3 907.5 Now, I plot my graphs using (with the Perc column in skj being a factor) : p <- ggplot(molten.data, aes(x=Week, y=Weight, group=Perc)) ; p <- p + geom_line(aes(colour = Perc,size=1,linetype=Perc)) ; p + geom_line(data=skj,mapping=aes(x=Week,y=Weight,group=Perc,linetype=Perc)) ; This yields the following error message: ## Error in data.frame(c("#FF6C91FF", "#00C1A9FF"), c("solid", "22", "42", : arguments imply differing number of rows: 2, 5 ## If I remove the linetype=Perc argument, I get a graph, but also a warning: ## Warning message: In data$arrow <- NULL : Coercing LHS to a list ## So, what am I doing wrong in this situation? I thank you sincerely for your help, Luc -- View this message in context: http://www.nabble.com/Overlaying-graphs-from-different-datasets-with-ggplot-tp23322409p23322409.html Sent from the R help mailing list archive at Nabble.com.
hadley wickham
2009-May-03 23:23 UTC
[R] Overlaying graphs from different datasets with ggplot
On Thu, Apr 30, 2009 at 2:03 PM, MUHC-Research <villandl at dms.umontreal.ca> wrote:> > Dear R-users, > > I recently began using the ggplot2 package and I am still in the process of > getting used to it. > > My goal would be to plot on the same grid a number of curves derived from > two distinct datasets. The first dataset (called molten.data) looks like > this : > > Column names : Perc, Week, Weight > > P10 ? 21 ?333.3554 > P90 ? 21 ?486.0480 > P10 ? 22 ?452.6347 > P90 ? 22 ?563.8263 > P10 ? 23 ?575.0960 > P90 ? 23 ?661.6841 > P10 ? 24 ?700.4449 > P90 ? 24 ?779.4067 > P10 ? 25 ?828.4966 > P90 ? 25 ?917.1222 > > The second dataset (called skj) looks like this: > > Column names : Week, Perc, Weight > > 21 ? ?1 ?317.5 > 22 ? ?1 ?392.5 > 23 ? ?1 ?467.5 > 24 ? ?1 ?542.5 > 25 ? ?1 ?617.5 > 26 ? ?1 ?697.5 > 21 ? ?2 ?535.0 > 22 ? ?2 ?632.5 > 23 ? ?2 ?737.5 > 24 ? ?2 ?855.0 > 25 ? ?2 ?980.0 > 26 ? ?2 1115.0 > 21 ? ?3 ?425.0 > 22 ? ?3 ?512.5 > 23 ? ?3 ?602.5 > 24 ? ?3 ?697.5 > 25 ? ?3 ?800.0 > 26 ? ?3 ?907.5 >...> So, what am I doing wrong in this situation?The perc columns are different in the two data frames. How do you expect them to match up? Hadley -- http://had.co.nz/