Hello to all,I have a data file as Class V1 V2A -2.0 0.0A 0.9 0.7B 0.1 0.6C 4.1 0.4C 1.0 1.9B 1.1 0.5 I am plotting this data in R as V1 verses V2> temp<-read.table('temp.dat', header=T)> attach(temp)> plot (V1,V2, col='red')> text(x=V1, y=V2, labels=Class, pos=4) But I want to change the 'plotting symbol' by the 'Class of the row' (which is A,B,C).in other words "I want to use the A,B,C instead of red circle and want different color for each class e.g. Red for 'A', Green for 'B' and so on" How to do that ??Thanks in advance Regards Arvind [[alternative HTML version deleted]]
On Aug 27, 2010, at 9:09 AM, arvin mer wrote:> Hello to all,I have a data file asThis came to the list mangled because of your failure to follow Posting Guide advice to use plain text. POST IN PLAIN TEXT.>> dput(temp) structure(list(Class = structure(c(1L, 1L, 2L, 3L, 3L, 2L), .Label = c("A", "B", "C"), class = "factor"), V1 = c(-2, 0.9, 0.1, 4.1, 1, 1.1 ), V2 = c(0, 0.7, 0.6, 0.4, 1.9, 0.5)), .Names = c("Class", "V1", "V2"), class = "data.frame", row.names = c(NA, -6L))> I am plotting this data in R as V1 verses V2> > temp<-read.table('temp.dat', header=T)> > attach(temp)> plot (V1,V2, col='red')> > text(x=V1, y=V2, labels=Class, pos=4)> But I want to change the 'plotting symbol' by the 'Class of the > row' (which is A,B,C).in other words "I want to use the A,B,C > instead of red circle and want different color for each class e.g. > Red for 'A', Green for 'B' and so on" > How to do that ??Thanks in advanceIt would seem superfluous to plot with a character and then also label it with characters. so see if this gets you any further. Note: I did not attach() your dataframe, but rather used with(): with(temp, plot(V1 ,V2, col=c("red", "green", "blue") [as.numeric(Class)])) ?plot # tells me that one cannot use a vector of characters for plotting symbols with(temp, text(x=V1, y=V2, labels=Class, pos=4)) # If you wanted to plot as characters you could use text after plotting blanks I suppose. with(temp, text(x=V1, y=V2, labels=Class, col=c("red", "green", "blue") [as.numeric(Class)]))> Regards > Arvind >David Winsemius, MD West Hartford, CT
Hi, This should do it. I let plot() choose whatever colors it wanted, but if you really wanted you could set them manually. Many thanks to David for the data. plot(temp$V1, temp$V2, pch = as.character(temp$Class), col = temp$Class) # As an alternative using ggplot2 # I do not use "A", "B", "C", but there are different shapes and there is a nice legend library(ggplot2) qplot(x = V1, y = V2, colour = Class, shape = Class, data = temp, geom = "point") HTH, Josh On Fri, Aug 27, 2010 at 6:09 AM, arvin mer <arvindmer at yahoo.com> wrote:> Hello to all,I have a data file as > Class?V1 ? ? ? ?V2A ? ? -2.0 ? ?0.0A ? ? 0.9 ? ?0.7B ? ? 0.1 ? ?0.6C ? ? 4.1 ? ?0.4C ? ? 1.0 ? ?1.9B ? ? 1.1 ? ?0.5 > I am?plotting?this data in R as V1 verses V2> temp<-read.table('temp.dat', header=T)> attach(temp)> plot (V1,V2, col='red')> text(x=V1, y=V2, labels=Class, pos=4) > But I want to change the ?'plotting symbol' ?by the 'Class of ?the row' (which is A,B,C).in other words "I want to use the A,B,C instead of red?circle?and want different?color?for each class e.g. Red for 'A', Green for 'B' ?and so on" > How to do that ??Thanks in advance > Regards > Arvind > > > > > ? ? ? ?[[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. > >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/