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

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Fri Feb 1 13:56:57 CET 2008


Have a look at scale_manual on http://had.co.nz/ggplot2/

This code will give you a key with lines instead of tiles.

ggplot(data = dataset, aes(x = x, y = y, colour = Test)) + geom_line() +
scale_colour_manual(values = c("red", "blue", "green"),  guide = "line")

If you want both lines and dots.

ggplot(data = dataset, aes(x = x, y = y, colour = Test)) + geom_line() +
geom_point()

ggplot(data = dataset, aes(x = x, y = y, colour = Test)) + geom_line() +
geom_point() + scale_colour_manual(values = c("red", "blue", "green"),
guide = "line")

I haven't found a way to get both the lines and the points in the key.
But on the other hand do you realy need to have lines in the key?
Suppose you want to combine to datasets in one graph: eg. points
representing the raw data, dotted lines representing a smoother on the
raw data and a continuous lines representing a model based on the raw
data. Then the colours would group the points, smoother and model for
each level. So what you you put in the key. Points? Dotted lines? Lines?
Or all possible combinations of colour (level) and symbol? In such cases
I prefer a key with the different colours for each level and a tile is
fine for that. The difference between point, dotted line and line can go
to the caption of the graph.

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: Tribo Laboy [mailto:tribolaboy op gmail.com] 
Verzonden: vrijdag 1 februari 2008 12:27
Aan: ONKELINX, Thierry
CC: r-help op r-project.org
Onderwerp: Re: [R] Reformatting data into data frame and plotting it in
ggplot2

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 op 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 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