[R] Scatter plot - using colour to group points?

David Winsemius dwinsemius at comcast.net
Tue Nov 22 07:00:31 CET 2011


On Nov 21, 2011, at 10:18 PM, R. Michael Weylandt wrote:

> I don't think you can do different colors for a single line (not an
> ifelse thing, just a what would that mean sort of thing), but a plot
> type like "b" "o" or "h" will work the same way.

I think Jim Lemon has a multicolored line function in package:plotrix.

--  
David.
>
> Michael
>
> On Mon, Nov 21, 2011 at 4:23 PM, SarahH <sarah.g10 at hotmail.co.uk>  
> wrote:
>> I got the colour vector with ifelse to work, great! Thank you.
>>
>> Is it possible to use the ifelse colour vector with other plot  
>> types? For
>> example with type=l ? I tried but the graphic came back with blue  
>> lines for
>> both sites and also a straight line connecting the start and end  
>> point of
>> the data?
>>
>> Thanks
>> Sarah
>>
>>
>>
>>
>>
>>
>> Michael Weylandt wrote
>>>
>>> I think the easiest way to do this is to set up a color vector with
>>> ifelse and hand that off to the plot command: something like
>>>
>>> col = ifelse(TEMP3[,"SITE"] == "BG1", "blue", "green") # Syntax is
>>> ifelse(TEST, OUT_IF_TRUE, OUT_IF_FALSE)
>>>
>>> For more complicated schemes, a set of nested ifelse()'s can get you
>>> what you need. There are some other tricks with factors as well, but
>>> they require a little more advanced use of R. Just for the record,
>>> they'd look something like this:
>>>
>>> X = letters[c(1,2,3,3,1,2,1,3,3,1,2,2,1)]
>>>
>>> colX = c("red","green","blue")[as.factor(X)]
>>>
>>> Hope this helps,
>>> Michael
>>>
>>> On Mon, Nov 21, 2011 at 2:17 PM, SarahH <sarah.g10 at .co> wrote:
>>>> Dear All,
>>>>
>>>> I am very new to R - trying to teach myself it for some MSc  
>>>> coursework.
>>>>
>>>> I am plotting temperature data for two different sites over the  
>>>> same time
>>>> period which I have downloaded from a university weather station  
>>>> data
>>>> archive.
>>>>
>>>> I am using the following code to create the plot
>>>>
>>>> plot ( x = TEMP3[,"TIME"], y = TEMP3[,"TEMP"], type = "p", col =
>>>> TEMP3[,"SITE"], pch = 3, main = "Temperature changes", xlab =  
>>>> "Date",
>>>> ylab =
>>>> "Temberature[C]")
>>>>
>>>> I managed to use col = TEMP3["SITE"] to plot the two different  
>>>> sites( BG1
>>>> and EA7) in different colours, but I am struggling to change the  
>>>> colours.
>>>>
>>>> I wanted to up a colour scheme to match the site, so tried
>>>>
>>>> BG1 <- "blue"
>>>> EA7 <- "green"
>>>>
>>>> before the plot function, but the graphic just came out with red  
>>>> and
>>>> black
>>>> as before.
>>>>
>>>> There are other datasets in which there are more than two sites  
>>>> so I
>>>> would
>>>> really like to learn how to use colour to distinguish between  
>>>> them on a
>>>> plot.
>>>>
>>>> Any direction would be very greatly received!
>>>>
>>>> Thank you very much
>>>>
>>>> Sarah
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> View this message in context:
>>>> http://r.789695.n4.nabble.com/Scatter-plot-using-colour-to-group-points-tp4092794p4092794.html
>>>> Sent from the R help mailing list archive at Nabble.com.
>>>>
>>>> ______________________________________________
>>>> R-help@ 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.
>>>
>>> ______________________________________________
>>> R-help@ 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.
>>>
>>
>>
>> --
>> View this message in context: http://r.789695.n4.nabble.com/Scatter-plot-using-colour-to-group-points-tp4092794p4093337.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> 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.
>
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list