Bianca Kung
Bianca Kung

Reputation: 11

R ggplot boxplot Error: Aesthetics must be either length 1 or the same as the data (100): x, y

I am trying to create a boxplot with the data frame grades_software, software as a discrete variable X (R/SPSS) and grades as a continuous variable Y.

I used the following code:

library(ggplot2)
ggplot(grades_software, aes(software, grades_software$final_score)) + 
geom_boxplot(fill = fill, colour = line) +
  scale_y_continuous(name = "final_score",
                     breaks = seq(0, 175, 25),
                     limits=c(0, 175)) +
  scale_x_discrete(name = "software") +
  ggtitle("Distribution of Final Course Scores by Software Used")

However, I get the error stated above:

Aesthetics must be either length 1 or the same as the data (100): x, y

I also don't know what's the function of putting breaks = seq and limits in the code.

Upvotes: 1

Views: 686

Answers (1)

Neoromanzer
Neoromanzer

Reputation: 434

You don't need to specify $ for the columns with ggplot.

Try

library(ggplot2)
ggplot(grades_software, aes(software, final_score)) + 
geom_boxplot(fill = fill, colour = line) +
  scale_y_continuous(name = "final_score",
                     breaks = seq(0, 175, 25),
                     limits=c(0, 175)) +
  scale_x_discrete(name = "software") +
  ggtitle("Distribution of Final Course Scores by Software Used")

With breaks you control the gridlines of the graph. Seq creates a sequence of gridlines seq(from, to, by). In you example... set gridlines from 0 to 175 every 25. Limits, om the other hand, is a numeric vector of length two providing limits of the scale. In your case, from 0 to 175.

Upvotes: 2

Related Questions