0

I am trying to create a ggplot using this code.

df <- read.csv("2019.csv", header = TRUE, sep = "," )
head(data)
ggplot(data = df, aes(x = 'GDP.per.capita', y = 'Social.support')) +
  geom_line(color = "red", size = 1) +       
  geom_point(size = 2)

but on the console i'm getting a warning message

1: In geom_line(color = "red", size = 1) :
  All aesthetics have length 1, but the data has 156 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing a single
  row.
2: In geom_point(size = 2) :
  All aesthetics have length 1, but the data has 156 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing a single
  row.

Went to YT but everyone was doing the same thing I'm doing. they are not getting any message but I'm getting this message.

2
  • what is 'YT' ...? oh, YouTube. Commented Dec 30, 2024 at 23:50
  • Don't know if this should be closed as 'due to a typo' - I guess it could be a common enough mistake? Commented Dec 30, 2024 at 23:53

1 Answer 1

1

You shouldn't put your variables to be plotted in quotation marks.

This works fine:

ggplot(data = mtcars, aes(x = mpg, y = hp)) + geom_point()

This gives a warning like the one you're seeing (and a plot that is junk):

ggplot(data = mtcars, aes(x = 'mpg', y = 'hp')) + geom_point()

Warning message: In geom_point() : All aesthetics have length 1, but the data has 32 rows. ℹ Please consider using annotate() or provide this layer with data containing a single row.

Sign up to request clarification or add additional context in comments.

3 Comments

thank you so much... it worked. but on those videos whatever they were doing i did the same thing. for them it work.. funny.. but thank you so much
The only thing I can think of is that things may have changed between version of ggplot2 (or the other packages it depends on). If this solved your problem, you are encouraged to click on the check-mark to accept it ...
I suspect in the video you saw, they used backticks (on my american keyboard, under the ~ tilde) instead of single quotes. The single quotes are treated by R as a character string, ie you wanted to literally use the single value 'GDP.per.capita' or 'mpg', whereas backticks allow you to refer to variable names that are otherwise not syntactic, e.g. with spaces between words. stat.ethz.ch/R-manual/R-devel/library/base/html/Quotes.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.