ggplot:错误:美学必须是长度1或与数据(10)相同:x,y,组 [英] ggplot: Error: Aesthetics must be either length 1 or the same as the data (10): x, y, group

查看:5903
本文介绍了ggplot:错误:美学必须是长度1或与数据(10)相同:x,y,组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 > 

我有一个小的数据集,我想用线图进行绘图:增益
分位数正数Total_Examples Positive_Prevalence提升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 $ b $ 9 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%) )

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

 错误:美学必须是长度为1或与数据(10)相同:x,y,group 

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



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


$ b $ (增益)


ggplot(增益)+
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 =数据集的百分比,
labels = c(0%,10%,20%,30%,40% 50%,60%,70%,80%,90%,100%))

您的建议将不胜感激。 您的数据长度为10,而你在 geom_line 中的美学长度为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)


I have a small dataset and I want to plot it with a line graph:

 > 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

My code is the following:

    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%"))

However, I get an error message :

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

I have read posts relating to this sort of error and usually the error implies that the variables called within the aesthetics are not present in the original dataframe. But this is not the case here.

I also tried this code, but it returns the same error message as well:

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%"))

Your advice will be appreciated.

解决方案

Your data is of length 10, while your y aesthetic in geom_line is of length 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

Try seq(0,0.9,by=0.1) or seq(0.1,1,by=0.1)

这篇关于ggplot:错误:美学必须是长度1或与数据(10)相同:x,y,组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆