7 C
London
Tuesday, March 11, 2025
HomeRFix Common Errors in RHow to Fix: Error in stripchart.default(x1, …) : invalid plotting method

How to Fix: Error in stripchart.default(x1, …) : invalid plotting method

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

One error you may encounter in R is:

Error in stripchart.default(x1, ...) : invalid plotting method 

This error usually occurs when you attempt to create a scatter plot using a data frame instead of a vector.

This tutorial shares exactly how to fix this error.

How to Reproduce the Error

Suppose we have the following data frame in R:

#create data frame
df frame(x=c(1, 2, 2, 4, 7, 8, 9),
                 y=c(5, 5, 8, 10, 13, 13, 18))

#view data frame
df

  x  y
1 1  5
2 2  5
3 2  8
4 4 10
5 7 13
6 8 13
7 9 18

Now suppose we attempt to use the following syntax to create a scatter plot:

#attempt to create scatter plot
plot(df[1], df[2])

Error in stripchart.default(x1, ...) : invalid plotting method

We receive an error because both df[1] and df[2] are actually data frames and the plot() function only accepts vectors as input.

We can use the class() function to verify that df[1] and df[2] are both data frames:

#display class of df[1] and df[2]
class(df[1]);class(df[2])

[1] "data.frame"
[1] "data.frame"

How to Fix the Error

The way to fix this error is to make sure that we use vectors as inputs for the plot() function.

For example, we can use the following syntax to create a scatter plot:

#create scatter plot
plot(df[, 1], df[, 2])

Or we could use the following syntax to create a scatter plot:

#create scatter plot
plot(df$x, df$y)

Notice that we’re able to create a scatter plot using either method without any errors because we used vectors as inputs to the plot() function each time.

Additional Resources

The following tutorials explain how to troubleshoot other common errors in R:

How to Fix in R: names do not match previous names
How to Fix in R: longer object length is not a multiple of shorter object length
How to Fix in R: contrasts can be applied only to factors with 2 or more levels

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories