Hi all, Let us have: x<-1:10 y<-x/2 plot(table(x), type="p") points(table(y), pch=2) Why does the last command plots the values of table(y) using the x coordinates of table(x)??? Am I doing something wrong? What would be a way of plotting the points of table(y) on their place? #this problem also occurs with: plot(table(y), type="p") points(table(x), pch=2) Thank you very much, Carlos
Carlos Gershenson wrote:> Hi all, > > Let us have: > > x<-1:10 > y<-x/2 > plot(table(x), type="p") > points(table(y), pch=2) > > > Why does the last command plots the values of table(y) using the x > coordinates of table(x)??? >It's just a coincidence. It's actually using the indices 1:10, regardless of the values of x. You've given points() a vector of 10 values to plot, and by default it plots them against their index. To get what you want you need temp <- table(y) points(names(temp), temp) The reason plot() worked is because it has a method for tables, but points() does not. In general the high level plot functions in R tend to have lots of methods, but the low level ones make you do the work. Duncan Murdoch> Am I doing something wrong? > What would be a way of plotting the points of table(y) on their place? > > #this problem also occurs with: > plot(table(y), type="p") > points(table(x), pch=2) > > > Thank you very much, > Carlos > > ______________________________________________ > 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. >
On Tue, 2007-11-27 at 13:01 -0500, Carlos Gershenson wrote:> Hi all, > > Let us have: > > x<-1:10 > y<-x/2 > plot(table(x), type="p") > points(table(y), pch=2) > > > Why does the last command plots the values of table(y) using the x > coordinates of table(x)??? > Am I doing something wrong? > What would be a way of plotting the points of table(y) on their place? > > #this problem also occurs with: > plot(table(y), type="p") > points(table(x), pch=2) > > > Thank you very much, > CarlosHi Carlos In your command plot(table(y), type="p"), you plot a numeric part of table(y), in this case: y 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 1 1 1 1 1 1 1 1 1 1 So you plot the number 1 always. Same for table (x). Have two possible solutions: plot(as.numeric(names(table(x))), type="p") points(as.numeric(names(table(y))), pch=2) OR x<-1:10 y<-x/2 plot(x, type="p") points(y, pch=2) If you need more help send a mail -- Bernardo Rangel Tura, M.D,MPH,Ph.D National Institute of Cardiology Brazil