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

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Fri Feb 1 10:09:42 CET 2008


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 op 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 op r-project.org [mailto:r-help-bounces op r-project.org]
Namens Tribo Laboy
Verzonden: vrijdag 1 februari 2008 9:59
Aan: r-help op 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 op 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