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

John Kane jrkrideau at yahoo.ca
Tue Nov 22 00:01:50 CET 2011


Another approach would be to use ggplot2.  
Code can look a bit daunting to begin with but ggplot2 is a 
very versitile graphing package and well worth learning.

Simple example
=============================================================
library(ggplot2)
mydata <- data.frame(site=c("A","A","A", "B","B","B"), time1 = 1:6, t1=c(23,24,13,7,19,12),
                        t2=c(7, 4,6,8,5,9))
       
p <- ggplot(mydata, aes(x=time1)) +
    geom_point(aes(y= t1, colour= site)) +
     geom_point(aes(y = t2, colour=site))

p   <- ggplot(mydata, aes(x=time1)) +
    geom_point(aes(y= t1, colour= site)) +
     geom_point(aes(y = t2, colour=site))
     
p <- p + scale_x_continuous('Time')+
              scale_y_continuous('Temperature')

p
=============================================================

--- On Mon, 11/21/11, SarahH <sarah.g10 at hotmail.co.uk> wrote:

> From: SarahH <sarah.g10 at hotmail.co.uk>
> Subject: [R] Scatter plot - using colour to group points?
> To: r-help at r-project.org
> Received: Monday, November 21, 2011, 2:17 PM
> 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 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.
> 




More information about the R-help mailing list