Inconsistent results with graph in R

I am experimenting with a plot in R, and I am trying to understand why it has the following behavior.

I send the table to the plot function, and this gives me a very good graph of variables, which is quite insightful. However, after I reorder the columns of the table and send it to the graph again, I get an odd scatter graph. What happened in the reordering and how can I avoid this?

smoke <- matrix(c(51,43,22,92,28,21,68,22,9),ncol=3,byrow=TRUE) colnames(smoke) <- c("High","Low","Middle") rownames(smoke) <- c("current","former","never") smoke <- as.table(smoke) plot(smoke) # This gives me a variwidth plot smoke = smoke[,c("Low", "Middle", "High")] # I reorder the columns plot(smoke) # This gives me a weird scatter plot 
+4
source share
2 answers

To investigate this, you need to do str () in two cases of "smoke":

 > str(smoke) table [1:3, 1:3] 51 92 68 43 28 22 22 21 9 - attr(*, "dimnames")=List of 2 ..$ : chr [1:3] "current" "former" "never" ..$ : chr [1:3] "High" "Low" "Middle" > str( smoke[,c("Low", "Middle", "High")] ) num [1:3, 1:3] 43 28 22 22 21 9 51 92 68 - attr(*, "dimnames")=List of 2 ..$ : chr [1:3] "current" "former" "never" ..$ : chr [1:3] "Low" "Middle" "High" 

The first is a table object, and the second is a matrix. You could also make class () on both and get a slightly more compact answer. To see why this is important, see also

 methods(plot) 

.... and we see that there is a plot.table* method. "*" Indicates that it is not "visible", and you needed to see the code that you need to use:

 getAnywhere(plot.table) 

As Ananda has shown, you can restore the table class for this smoke object, and then get the sending system to send the plot.table* object.

+5
source

When you rearrange the columns, you changed the smoke class from table to matrix, so a graph that returns different default results depending on its input returns a different graph.

Try:

 plot(as.table(smoke)) 
+4
source

All Articles