Hello, this is probably a recurrent question, but I couldn't find any answers that didn't involve the expression "data frame"... so perhaps I'm looking for something new here. I wanted to find a code equivalent to> x=sqrt(1:10) > y=log(1:10) > plot(1:10, x, type="lines", col="darkgreen") > lines(1:10, y, col="red")to use with ggplot2. I've tried> x=sqrt(1:10) > y=log(1:10) > qplot(1:10, x, geom="line", colour=I("darkgreen")) > geom_line(1:10, y, colour="red")Error: ggplot2 doesn't know how to deal with data of class numeric but it seems that the "data frame restriction" is really very restrictive here. Any solutions that don't imply using as.data.frame to my data? Thanks in advance, and best regards! Eduardo Horta [[alternative HTML version deleted]]
Hi Eduardo, To shamelessly borrow from the Princess Bride: ?Why do you use a data frame? Are you touched in the head? "Oh no. It's just they're terribly practical. I think everyone will be using them in the future.? Using data frames is what Hadley intended, and once you get used to it, it is not nearly as restrictive as you might think. This does what you want, I believe. Rather than creating extraneous variables, I simply perform various transformations on 'x' within the plotting code. require(ggplot2) dfm <- data.frame(x = 1:10) qplot(x = x, y = sqrt(x), data = dfm, geom = "line", colour = I("darkgreen")) + geom_line(aes(x = x, y = log(x)), colour = "red") Cheers, Josh On Tue, Jan 4, 2011 at 6:56 PM, Eduardo de Oliveira Horta <eduardo.oliveirahorta at gmail.com> wrote:> Hello, > > this is probably a recurrent question, but I couldn't find any answers that > didn't involve the expression "data frame"... so perhaps I'm looking for > something new here. > > I wanted to find a code equivalent to > >> x=sqrt(1:10) >> y=log(1:10) >> plot(1:10, x, type="lines", col="darkgreen") >> lines(1:10, y, col="red") > > to use with ggplot2. I've tried > >> x=sqrt(1:10) >> y=log(1:10) >> qplot(1:10, x, geom="line", colour=I("darkgreen"))##### note you would also need a + after the qplot() code to add geom_line() #####>> geom_line(1:10, y, colour="red") > Error: ggplot2 doesn't know how to deal with data of class numeric > > but it seems that the "data frame restriction" is really very restrictive > here. Any solutions that don't imply using as.data.frame to my data? > > Thanks in advance, and best regards! > > Eduardo Horta > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
Hi: On Tue, Jan 4, 2011 at 6:56 PM, Eduardo de Oliveira Horta < eduardo.oliveirahorta@gmail.com> wrote:> Hello, > > this is probably a recurrent question, but I couldn't find any answers that > didn't involve the expression "data frame"... so perhaps I'm looking for > something new here. > > I wanted to find a code equivalent to > > > x=sqrt(1:10) > > y=log(1:10) > > plot(1:10, x, type="lines", col="darkgreen") > > lines(1:10, y, col="red") > > to use with ggplot2. I've tried > > > x=sqrt(1:10) > > y=log(1:10) > > qplot(1:10, x, geom="line", colour=I("darkgreen")) > > geom_line(1:10, y, colour="red") > Error: ggplot2 doesn't know how to deal with data of class numeric >It would work in lattice, though: xyplot(x ~ 1:10, type = 'l', col.line = 'darkgreen')> > but it seems that the "data frame restriction" is really very restrictive > here. Any solutions that don't imply using as.data.frame to my data? >Please explain to me how df <- data.frame(x, y, index = 1:10) qplot(index, x, geom = 'line', ...) is 'very restrictive'. Lattice and ggplot2 are *structured* graphics systems - to get the gains that they provide, there are some costs. I don't perceive organization of data into a data frame as being restrictive - in fact, if you learn how to construct data for input into ggplot2 to simplify the code for labeling variables and legends, the data frame requirement is actually a benefit rather than a restriction. Moreover, one can use the plyr and reshape(2) packages to reshape or condense data frames to provide even more flexibility and freedom to produce ggplot2 and lattice graphics. In addition, the documentation for ggplot2 is quite explicit about requiring data frames for input, so it is behaving as documented. The complexity (and interaction) of the graphics code probably has something to do with that. Since Josh left you a quote, I'll supply another, from Prof. Steve Vardeman in a class I took with him a long time ago: "There is no free lunch in statistics: in order to get something, you've got to give something up." In this case, if you want the nice infrastructure provided by ggplot2, you have to create a data frame for input. Dennis> > Thanks in advance, and best regards! > > Eduardo Horta > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Dear Eduardo, This a solution that you seem to want n <- 1:10 x <- sqrt(n) y <- log(n) qplot(n, x, geom="line", colour="darkgreen") + geom_line(data data.frame(n , x = y), colour="red") But please compare it with the solution (code + result) below. Formatting the data.frame might be a bit more work, but formatting your graph is much easier. n <- 1:10 dataset <- rbind( data.frame(Number = n, Function = "sqrt", Result sqrt(n)), data.frame(Number = n, Function = "log", Result log(n)) ) #Using the default colours ggplot(dataset, aes(x = Number, y = Result, colour = Function)) + geom_line() #Using user-specified colours ggplot(dataset, aes(x = Number, y = Result, colour = Function)) + geom_line() + scale_colour_manual(values = c(sqrt = "darkgreen", log "red")) Think about the gain when you want to display much more than 2 lines... dataset <- expand.grid(Number = n, Power = seq(0, 2, length = 21)) dataset$Result <- dataset$Number ^ dataset$Power ggplot(dataset, aes(x = Number, y = Result, colour = factor(Power))) + geom_line() HTH, Thierry ------------------------------------------------------------------------ ---- ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek team Biometrie & Kwaliteitszorg Gaverstraat 4 9500 Geraardsbergen Belgium Research Institute for Nature and Forest team Biometrics & Quality Assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 Thierry.Onkelinx at inbo.be www.inbo.be To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey> -----Oorspronkelijk bericht----- > Van: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] Namens Eduardo de Oliveira Horta > Verzonden: woensdag 5 januari 2011 3:56 > Aan: r-help > Onderwerp: [R] Adding lines in ggplot2 > > Hello, > > this is probably a recurrent question, but I couldn't find > any answers that didn't involve the expression "data > frame"... so perhaps I'm looking for something new here. > > I wanted to find a code equivalent to > > > x=sqrt(1:10) > > y=log(1:10) > > plot(1:10, x, type="lines", col="darkgreen") lines(1:10, y, > col="red") > > to use with ggplot2. I've tried > > > x=sqrt(1:10) > > y=log(1:10) > > qplot(1:10, x, geom="line", colour=I("darkgreen")) > geom_line(1:10, y, > > colour="red") > Error: ggplot2 doesn't know how to deal with data of class numeric > > but it seems that the "data frame restriction" is really very > restrictive here. Any solutions that don't imply using > as.data.frame to my data? > > Thanks in advance, and best regards! > > Eduardo Horta > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >