[R] Help with coloring segments on a plot

Andrew Robinson A.Robinson at ms.unimelb.edu.au
Tue May 3 01:18:45 CEST 2011


Hi Paul,

not to seem naive, but have you actually tried the code below?  It
doesn't seem that you have, from your text.  I think that if you try
it and hack then ask concrete questions (e.g. can anyone explain why
the following simple, reproducible, commented code does not work) then
you'll have more luck.

Best wishes

Andrew

On Mon, May 02, 2011 at 02:26:16PM -0400, Paul Davison wrote:
> Hi. I need a very short piece of help regarding colouring segments plotted
> on a graph.
> 
> When I am plotting segments for the graph, I am using "red" and "darkgreen
> for the values "1" and "2" respectively. Heres the relevant line of code in
> R:
> 
> + col = c("red", "darkgreen")[line.colour.value])
> 
> I just need to extend this to refer to a larger range of numbers from 1 to
> 10, to plot the segments in ten different colours. The values are just the
> first ten integers: 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10
> Each of the ten values will refer to a different colour just as "1" would
> plot a segment in red and "2" would plot a segment in darkgreen.
> 
> The only other condition I need is that the colours be in hex format. Would
> this be along the right lines? :
> 
> + col = c("#FFFFFF", "#FFFFFF", "#FFFFFF", "#FFFFFF", "#FFFFFF", "#FFFFFF",
> "#FFFFFF", "#FFFFFF", "#FFFFFF", "#FFFFFF",)[line.colour.value])
> 
> Or would I need to adjust the code in other places too?
> 
> I have copied the code I am using below. I have also copied below a small
> excerpt of the simple data I am plotting - with the headers at the top.
> 
> Thank you so much for your help.
> 
> Paul Davison
> University of Cambridge, UK
> 
> 
> 
> 
> > data = read.csv("r.test.data.csv", header = TRUE)
> > with(data, {
> + par(bg="#0B5FA5")
> + par(lwd=0.01)
> + plot(NA, NA,
> + xlim = range(start.x.co.ordinate, end.x.co.ordinate, 50000),
> + ylim = range(start.y.co.ordinate, end.y.co.ordinate, 50000),
> + type = "n", ann = FALSE, axes = FALSE)
> + segments(start.x.co.ordinate, start.y.co.ordinate,
> + end.x.co.ordinate, end.y.co.ordinate,
> + col = c("red", "darkgreen")[line.colour.value])
> + title(main = "10th April 1991",
> + xlab = "Pandora",
> + ylab = "Luna")
> + })
> >> quartz.save("sample4.png","png")
> 
> 
> The values in the following data table for the column "line.colour.value"
> are just 1s and 2s. Ideally I would have numbers of 1 through to 10 and each
> one would plot a different coloured (using a hex value) segment.
> 
> 
> start.x.co.ordinate    start.y.co.ordinate  end.x.co.ordinate
> end.y.co.ordinate   line.colour.value
>     300 300 2289 20289 2 300 300 2692 20467 1 300 300 3010 20608 2 300 300
> 2727 19828 1 300 300 2606 20056 2 300 300 16244 21416 1 300 300 16154 21899
> 2 300 300 16941 21434 1 300 300 17356 20205 2 300 300 16928 21245 1 300 300
> 16011 21024 2 300 300 17323 20053 1 300 300 17312 20435 2 300 300 17175
> 21259 1 300 300 16851 21268 2
> 
> 	[[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.

-- 
Andrew Robinson  
Program Manager, ACERA 
Department of Mathematics and Statistics            Tel: +61-3-8344-6410
University of Melbourne, VIC 3010 Australia               (prefer email)
http://www.ms.unimelb.edu.au/~andrewpr              Fax: +61-3-8344-4599
http://www.acera.unimelb.edu.au/

Forest Analytics with R (Springer, 2011) 
http://www.ms.unimelb.edu.au/FAwR/
Introduction to Scientific Programming and Simulation using R (CRC, 2009): 
http://www.ms.unimelb.edu.au/spuRs/



More information about the R-help mailing list