This “error in stripchart.default(x1, …) : invalid plotting method” message occurs when using the plotting function with a data frame, and the coordinates entered are a data frame and not numeric vectors. The way to fix this problem is to make sure that the plotting coordinates are numeric. The process for doing this is simple but not obvious.
Description of the problem
The plot function is the base r function for plotting graphs. It is simple to use, but the values entered as coordinates must be numeric values. They can be vectors or data frame columns, but they must be numeric values. If you try using other types, you will get an error message. If the type is a data frame, then you will get our current message. It results from an easy mistake because the correct format to get the column values as numeric values when using numeric column labels is a little unintuitive. However, once you understand the problem, it is easy to fix and avoid. The first time you encountered this problem, the solution will be unexpected, but it makes sense once you understand the cause of the problem.
Explanation of the Problem
Here we have three examples using the plot function that causes this error message.
> df = data.frame(x = c(1,2,3,4,5,6,7,8),
+ y = c(2,3,4,5,6,7,8,9))
> plot(df[1], df[2])
Error in stripchart.default(x1, …) : invalid plotting method
> class(df[1])
[1] “data.frame”
> class(df[ , 1])
[1] “numeric”
Note that in this example the column types we entered were not numeric values and so it produced our problem. However, the df[ , 1] format produces the needed numeric value.
> x = c(1, 2, 3, 4, 5)
> y = c(1, 4, 9,16,25)
> df = data.frame(x,y)
> plot(df[1], df[2])
Error in stripchart.default(x1, …) : invalid plotting method
> class(df[1])
[1] “data frame”
> class(df$y)
[1] “numeric”
> class(y)
[1] “numeric”)
Note that in this example the column types we entered were not numeric values and so it produced our problem. However, the df$y format produces the needed numeric value. Also using the original vectors produce a numeric value.
> df = cars
> plot(df[1], df[2])
Error in stripchart.default(x1, …) : invalid plotting method
> class(df[1])
[1] “data frame”
> class(df[ , 1])
[1] “numeric”
Note that in this example the column types we entered were not numeric values and so it produced our problem. However, the df[ , 1] format produces the needed numeric value. You can use the header function to get a look at the cars base r object.
How to fix the problem
Here we have four examples of how to fix this problem. The middle two show alternative ways of fixing the second example above.
> df = data.frame(x = c(1,2,3,4,5,6,7,8),
+ y = c(2,3,4,5,6,7,8,9))
> plot(df[ , 1], df[ , 2])
In this example, we use the df[ , 1], and df[ , 2] format to produce numeric vectors from the data frame columns.
> x = c(1, 2, 3, 4, 5)
> y = c(1, 4, 9,16,25)
> df = data.frame(x,y)
> plot(df$x, df$y)
In this example, we use the df$x, and df$y format to produce numeric vectors from the data frame columns.
> x = c(1, 2, 3, 4, 5)
> y = c(1, 4, 9,16,25)
> plot(x, y)
In this alternative method, we skipped the data frames altogether and use the original vectors as coordinates. This works because each of them is a numeric vector.
> df = cars
> plot(df[ , 1], df[ , 2])
In this example, we use the df[ , 1], and df[ , 2] format to produce numeric vectors from the cars object.
When using the plotting function, this is an easy problem to encounter, but there are several ways to fix it depending upon the situation. Once you understand the nature of this problem, avoiding it and fixing it if necessary, will be a simple task. You are only likely to encounter it if you use the numeric column designations rather than column names when using the plotting function. If you use the column names in df$name it is impossible to cause this problem but numeric labels offer the option of using a variable.