RPM
RPM

Reputation: 1

Error: "Aesthetics must either be length one or the same length" in R

I try run the following function in R:

GainChart <- function(score, good.label, n.breaks = 50, ...){

  df <- data.frame(percentiles = seq(0, 1, length = n.breaks),
                   gain = Gain(score, good.label, seq(0, 1, length = n.breaks)))

  p <-  ggplot(df, aes(percentiles, gain))  + geom_line(size = 1.2, colour = "darkred")
  p <- p + geom_line(aes(x = c(0,1), y = c(0,1)), colour = "gray", size = 0.7)
  p <- p + scale_x_continuous("Sample Percentiles", labels = percent_format(), limits = c(0, 1))
  p <- p + scale_y_continuous("Cumulative Percents of Bads", labels = percent_format(), limits = c(0, 1))
  p
}

And I received the following error message: "Error: Aesthetics must be either length 1 or the same as the data (50): x, y"

The command for call the function is:

GainChart(data_sampni$score,data_sampni$TOPUP_60d)

Upvotes: 0

Views: 725

Answers (2)

Greg Snow
Greg Snow

Reputation: 49640

The help for geom_line says that if data=NULL (the default) then the data is inherited from the parent graph. That is where your mismatch is coming from.

Iy you change the second line to something like:

p <- p + geom_line(aes(x,y), data=data.frame(x=c(0,1), y=c(0,1)))

Then it should work.

Upvotes: 0

scoa
scoa

Reputation: 19867

Your second geom_line() doesn't make sense. It seems that you are trying to draw a line between [0,0] and [1,1]. In that case, use geom_abline():

+ geom_abline(aes(intercept = 0, slope = 1))

Upvotes: 0

Related Questions