簡體   English   中英

錯誤:R中的“美學長度必須相同或相同”

[英]Error: “Aesthetics must either be length one or the same length” in R

我嘗試在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
}

並且我收到以下錯誤消息:“錯誤:美學必須為長度1或與數據(50)相同:x,y”

調用該函數的命令是:

GainChart(data_sampni$score,data_sampni$TOPUP_60d)

您的第二個geom_line()沒有任何意義。 似乎您正在嘗試在[0,0]和[1,1]之間畫一條線。 在這種情況下,請使用geom_abline()

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

geom_line的幫助說,如果data=NULL (默認值),則數據從父圖繼承。 那就是您不匹配的來源。

即,您將第二行更改為:

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

然后它應該工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM