【问题标题】:ggplot: Error: Aesthetics must be either length 1 or the same as the data (10): x, y, groupggplot:错误:美学必须是长度1或与数据相同(10):x,y,组
【发布时间】:2017-06-07 04:12:27
【问题描述】:

我有一个小数据集,我想用折线图绘制它:

 > Gain
     quantile Positives Total_Examples Positive_Prevalence     Lift Cumsum_Positives
 1:     (0, ]         1              1                   1 1.428571        0.1428571
 2: (1.9,2.8]         1              1                   1 1.428571        0.2857143
 3: (2.8,3.7]         1              1                   1 1.428571        0.4285714
 4: (3.7,4.6]         1              1                   1 1.428571        0.5714286
 5: (4.6,5.5]         1              1                   1 1.428571        0.7142857
 6: (5.5,6.4]         1              1                   1 1.428571        0.8571429
 7: (6.4,7.3]         1              1                   1 1.428571        1.0000000
 8: (7.3,8.2]         0              1                   0 0.000000        1.0000000
 9: (8.2,9.1]         0              1                   0 0.000000        1.0000000
10:  (9.1,10]         0              1                   0 0.000000        1.0000000

我的代码如下:

    ggplot(Gain ) +
  geom_area(aes(x = quantile, y = Cumsum_Positives, group = 1), color = "red", fill = "red", alpha = 0.5, size = 2) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  geom_line(aes(x = quantile, y = seq(0, 1 , by = 0.1)  , group = 1), color = "blue", size = 2, alpha = 0.5) +
  scale_x_discrete(name ="% of the Data Set",
                   labels=c("0%", "10%","20%","30%", "40%", "50%", "60%","70%","80%", "90%", "100%"))

但是,我收到一条错误消息:

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

我已阅读与此类错误相关的帖子,通常该错误意味着在美学中调用的变量不存在于原始数据框中。但这里不是这样。

我也试过这段代码,但它也返回相同的错误消息:

Gain$index <- row.names(Gain)


ggplot(Gain ) +
  geom_area(aes(x = index, y = Cumsum_Positives, group = 1), color = "red", fill = "red", alpha = 0.5, size = 2) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  geom_line(aes(x = quantile, y = seq(0, 1 , by = 0.1)  , group = 1), color = "blue", size = 2, alpha = 0.5) +
  scale_x_discrete(name ="% of the Data Set",
                   labels=c("0%", "10%","20%","30%", "40%", "50%", "60%","70%","80%", "90%", "100%"))

您的建议将不胜感激。

【问题讨论】:

    标签: r ggplot2 aesthetics


    【解决方案1】:

    您的数据长度为 10,而 geom_line 中的 y 美学长度为 11。

    seq(0, 1 , by = 0.1)
    [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
    

    试试seq(0,0.9,by=0.1)seq(0.1,1,by=0.1)

    【讨论】:

      【解决方案2】:

      如果您的目标是从 (x = 0, y = 0) 到 (x = 0.9, y = 1) 画一条线,您也可以将 geom_line 替换为 geom_abline

      geom_abline(slope = 1/9,
                  intercept = -0.109,
                  color = "blue",
                  size = 2,
                  alpha = 0.5) +
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-17
        • 1970-01-01
        • 2016-10-13
        • 1970-01-01
        • 2017-11-04
        • 2022-08-20
        相关资源
        最近更新 更多