I am attempting to make a scatter plot in ggplot with the variable "year" as the x axis and the variable "gdp" as the y axis. However, these two variables are not of equal length. I tried to fix this by omitting NA values but the lengths are still different. How do I proceed/equalize the length of each variable so that the plot can be made? Thanks in advance
mutate(nations,gdp=(gdp_percap*population)/1,000,000,000,000)
gdp
ggplot(na.omit(nations), aes(x= year, y = gdp)) +
geom_point()
Error: Aesthetics must be either length 1 or the same as the data (4303): y
When you say the variables are not of equal length, do you mean values are missing from some data points? I don't quite understand what you mean by different in length.
Hi @Ness329,
Your nations dataframe should have 4 columns (all the same length, by definition in a dataframe) named: year, gdp_percap, population, gdp. Is that correct?
You could post the output from
head(nations, n=20)
so we can see what your data looks like, and then work out why ggplot is complaining.
HTH
To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one: