jonathan byrne
2011-Feb-04 11:04 UTC
[R] Multiple line-plots from one data file using factor
Hi, I'm processing a data file with the following structure: #index time distance 1 10 500 1 15 300 1 23 215 1 34 200 2 5 400 2 13 340 2 15 210 3 10 200 3 12 150 3 16 30 etc My intention was to generate line plots for the datapoints using their index to group the elements. I managed to get something to work using a for loop and some conditionals but I was wondering if there is a better way using factor or some other R method? Any help improving my R coding would be greatly appreciated! Thanks Jonathan [[alternative HTML version deleted]]
baptiste auguie
2011-Feb-04 13:17 UTC
[R] Multiple line-plots from one data file using factor
Hi,
It's a piece of cake with ggplot,
d <- read.table(textConnection("id x y
1 10 500
1 15 300
1 23 215
1 34 200
2 5 400
2 13 340
2 15 210
3 10 200
3 12 150
3 16 30"), head=TRUE)
str(d)
library(ggplot2)
p <-
ggplot(d) +
geom_path(aes(x,y, group=id))
p ## grouping withouth legend
## colour based on id
p + aes(colour=factor(id))
## linetype based on id
p + aes(linetype=factor(id))
HTH,
baptiste
On 4 February 2011 12:04, jonathan byrne <jonathanbyrn at gmail.com>
wrote:> Hi,
> I'm processing a data file with the following structure:
> #index ?time distance
> 1 ?10 ?500
> 1 ?15 ?300
> 1 ?23 ?215
> 1 ?34 ?200
> 2 ?5 ? ?400
> 2 ?13 ?340
> 2 ?15 ?210
> 3 ?10 ?200
> 3 ?12 ?150
> 3 ?16 ?30
> etc
>
> My intention was to generate line plots for the datapoints using their
index
> to group the elements. I managed to get something to work using a for loop
> and some conditionals but I was wondering if there is a better way using
> factor or some other R method? Any help improving my R coding would be
> greatly appreciated!
> Thanks
> Jonathan
>
> ? ? ? ?[[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.
>