[R] Reformatting data into data frame and plotting it in ggplot2

Tribo Laboy tribolaboy at gmail.com
Fri Feb 1 12:27:18 CET 2008


Hi, Thierry,

That was exactly what I was looking for. Thanks.

Now I have a data frame with the series data in my workspace, and a
plot on the graphics device with color lines and respective color
patches on the legend. My next question is about ggplot. Is it
possible to make the legend show not only the aesthetics (color in
this case), but also the geometry (lines). It would look much better
if it showed colored lines or colored dots (if it were) instead of
just the patch. But again the ggplot manual didn't have an example for
that. Also, what if I wanted to plot with lines+symbol, do I have to
use multiple layers? Is it (easily) achievable?

Too many questions... I guess it shows I am quite new to R, but I hope
to pick it up quickly with some help and shift all my plotting from
Matlab to R.


On Fri, Feb 1, 2008 at 6:09 PM, ONKELINX, Thierry
<Thierry.ONKELINX at inbo.be> wrote:
> Tribo,
>
>  Use data.frame() and rbind() to combine the vectors.
>
>
>
>  x_test1 <- c(1:10)
>   y_test1<-rnorm(10)
>   x_test2 <- c(1:15)
>   y_test2<-rnorm(15)
>   x_test3 <- c(1:20)
>   y_test3<-rnorm(20)
>
>  dataset <- rbind(data.frame(Test = "Test 1", x = x_test1, y = y_test1),
>  data.frame(Test = "Test 2", x = x_test2, y = y_test2), data.frame(Test =
>  "Test 3", x = x_test3, y = y_test3))
>  dataset$Test <- factor(dataset$Test)
>
>  library(ggplot2)
>  ggplot(data = dataset, aes(x = x, y = y, colour = Test)) + geom_line()
>
>  HTH,
>
>  Thierry
>
>  ------------------------------------------------------------------------
>  ----
>  ir. Thierry Onkelinx
>  Instituut voor natuur- en bosonderzoek / Research Institute for Nature
>  and Forest
>  Cel biometrie, methodologie en kwaliteitszorg / Section biometrics,
>  methodology and quality assurance
>  Gaverstraat 4
>  9500 Geraardsbergen
>  Belgium
>  tel. + 32 54/436 185
>  Thierry.Onkelinx at inbo.be
>  www.inbo.be
>
>  Do not put your faith in what statistics say until you have carefully
>  considered what they do not say.  ~William W. Watt
>  A statistical analysis, properly conducted, is a delicate dissection of
>  uncertainties, a surgery of suppositions. ~M.J.Moroney
>
>  -----Oorspronkelijk bericht-----
>  Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
>  Namens Tribo Laboy
>  Verzonden: vrijdag 1 februari 2008 9:59
>  Aan: r-help at r-project.org
>  Onderwerp: [R] Reformatting data into data frame and plotting it in
>  ggplot2
>
>
>
>  Hello,
>
>   I am sure this must have been asked before, but my nabble search did
>  not turn anything useful. Just pointer where to look will also be  nice.
>
>   So, I have the following data:
>
>   x_test1 <- c(1:10)
>   y_test1<-rnorm(10)
>   x_test2 <- c(1:15)
>   y_test2<-rnorm(15)
>   x_test3 <- c(1:20)
>   y_test3<-rnorm(20)
>
>   These represent time series or frequency spectra, possibly sampled
>  with different sampling frequencies, but obviously having different
>  lengths. The physical meaning of X and Y is the same for the three
>  series above - as I said, time or frequency in my case.
>   Now I want to plot them on the same graph with the legend "test1",
>  "test2" and "test3".
>
>   ggplot PDF manual says that the data frame is the preferable format
>  for the data to plot. I guess that turning the above data into a data
>  frame would be useful in many other situations as well. So how can I  do
>  that?
>
>  ______________________________________________
>  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.
>



More information about the R-help mailing list