[R] problem with plotting table

Duncan Murdoch murdoch at stats.uwo.ca
Tue Nov 27 21:57:49 CET 2007


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.
>



More information about the R-help mailing list