FMH
2009-Sep-24 18:28 UTC
[R] Color of the plot which correspond to the group of the observations
Dear All, ? Let: dp: depth of the river tp: temperature with respect to depth These pair of observations are in 3 different groups i.e: Obs. 1,3,5,7?from the first group Obs. 2,4 and?10?from second group Obs 6,8 and 9?from third group. We can have a simple scatter plot, between depth as y-axis and temperature as x-axis, with each pairs are denoted by a red dot, by using a plot function shown below. ? ##################### ?dp <- c(1,4,3,2,5,7,9,8,9,2) ?tp <- 1:10 ?plot(tp,dp, type= 'p', col = 'red') ?##################### Could someone?please give some?advices on the way??to have a plot in which the color of each?pair of observation corrrespond to?its group? For instance, we might have red, blue and green dots corresponding to groups 1,2 and 3, respectively.? Thank you Fir
Richard M. Heiberger
2009-Sep-24 18:52 UTC
[R] Color of the plot which correspond to the group of the observations
It is easier in lattice dp <- c(1,4,3,2,5,7,9,8,9,2) tp <- 1:10 gg <- rep(1:3, c(3,3,4)) ddff <- data.frame(dp=dp, tp=tp, gg=gg) xyplot(dp ~ tp, groups=gg, data=ddff)
baptiste auguie
2009-Sep-24 18:53 UTC
[R] Color of the plot which correspond to the group of the observations
Try these three options, dp <- c(1,4,3,2,5,7,9,8,9,2) tp <- 1:10 group <- factor(c(1, 2, 1, 2, 1, 3, 1, 3, 3, 2), label=letters[1:3]) plot(tp,dp, type= 'p', col = group) d <- data.frame(dp=dp, tp=tp, group=group) library(lattice) xyplot(dp~tp, data=d, groups=group, auto.key=TRUE) library(ggplot2) qplot(tp, dp, data=d, colour=group) HTH, baptiste 2009/9/24 FMH <kagba2006 at yahoo.com>:> Dear All, > > Let: > > dp: depth of the river > tp: temperature with respect to depth > > These pair of observations are in 3 different groups i.e: > Obs. 1,3,5,7?from the first group > Obs. 2,4 and?10?from second group > Obs 6,8 and 9?from third group. > > We can have a simple scatter plot, between depth as y-axis and temperature as x-axis, with each pairs are denoted by a red dot, by using a plot function shown below. > > ##################### > ?dp <- c(1,4,3,2,5,7,9,8,9,2) > ?tp <- 1:10 > ?plot(tp,dp, type= 'p', col = 'red') > ?##################### > > Could someone?please give some?advices on the way??to have a plot in which the color of each?pair of observation corrrespond to?its group? For instance, we might have red, blue and green dots corresponding to groups 1,2 and 3, respectively. > > > Thank you > Fir > > > > > ______________________________________________ > 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. >
Jim Lemon
2009-Sep-24 21:02 UTC
[R] Color of the plot which correspond to the group of the observations
On 09/25/2009 04:28 AM, FMH wrote:> Dear All, > > Let: > > dp: depth of the river > tp: temperature with respect to depth > > These pair of observations are in 3 different groups i.e: > Obs. 1,3,5,7 from the first group > Obs. 2,4 and 10 from second group > Obs 6,8 and 9 from third group. > > We can have a simple scatter plot, between depth as y-axis and temperature as x-axis, with each pairs are denoted by a red dot, by using a plot function shown below. > > ##################### > dp<- c(1,4,3,2,5,7,9,8,9,2) > tp<- 1:10 > plot(tp,dp, type= 'p', col = 'red') > ##################### > > Could someone please give some advices on the way to have a plot in which the color of each pair of observation corrrespond to its group? For instance, we might have red, blue and green dots corresponding to groups 1,2 and 3, respectively. >Hi Fir, You can do it manually like this: plot(tp,dp,type="p",col=c("red","blue","red","blue","red","green", "red","green","green","blue")) but it is a lot easier if there is a variable that identifies which group contains each observation: obs.group<-c(1,2,1,2,1,3,1,3,3,2) plot(tp,dp,type="p",col=c("red","blue","green")[obs.group]) If you want the color to be scaled to one of the variables: plot(tp,dp,type="p", col=color.scale(tp,extremes=c("blue","red"))) Jim