Help with plot() Function

Hello,

Im new to R.
I want to plot some data in my Markdown-File. I am doing this like the following:

data <- read.csv('D:/MKI/SEM4/DataScience/Projekt/imdb_top_1000.csv', header = TRUE)
data_without_na <- na.omit(data)

plot(data&IMDB_Rating, data$Meta_Score)

But i get the following message:
'x' and 'y' lengths differ

Meta_Score has some NA in it so i think its because of that.
But how can i fix that?

I would be realy glad if someone could help me:)

We need to see some data.

A handy way to supply some sample data is the dput() function. In the case of a large dataset something like dput(head(mydata, 100)) should supply the data we need. Just do dput(mydata) where mydata is your data. Copy the output and paste it here.

For general reference see FAQ: How to do a minimal reproducible example ( reprex ) for beginners

Welcome to the community @jns_25! I noticed data&IMDB_Rating is using โ€œ&โ€ instead of โ€œ$โ€. Does changing that fix the issue?

data$IMDB_Rating

Hi,

So this is the Dataset iยดm using:

Maybe this helps

My bad. But the error is still there :frowning:

the column is not called Meta_Score its called Meta_score

1 Like

You can try the following:

  • Use na.omit() function to remove rows with NA values from both variables before plotting.

Copy code

data_without_na <- na.omit(data[,c("IMDB_Rating", "Meta_Score")])
plot(data_without_na$IMDB_Rating, data_without_na$Meta_Score)
  • Use the na.exclude() function in the plot command to exclude NA values

Copy code

plot(data$IMDB_Rating, data$Meta_Score, na.rm=TRUE)
  • Alternatively, you can use the ggplot2 package to plot the data, which can handle missing values more gracefully.

Copy code

library(ggplot2)
ggplot(data, aes(x=IMDB_Rating, y=Meta_Score)) + geom_point()

This should fix the error message, and allow you to plot your data.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.