【报错】Error: stat_bin() must not be used with a y aesthetic.

在进行ggplot绘图的过程中,出现了“Error: stat_bin() must not be used with a y aesthetic.”的报错。
代码具体如下:

> ggplot(aes(x = gender, y = age),
+        data = subset(pf, !is.na(gender))) + geom_histogram()
Error: stat_bin() must not be used with a y aesthetic.
> ggplot(aes(x = age, y = friend_count),
+        data = subset(pf, !is.na(gender))) + geom_histogram()+
+   facet_wrap(~gender,ncol = 1)
Error: stat_bin() must not be used with a y aesthetic.

stat_bin表明统计变换是计数,计数会被投射到y轴,与y=1冲突了, 所以才出现stat_bin() must not be used with a y aesthetic.
这里主要是因为直方图是单因素变量的可视化方法,aes包裹中不能添加y轴。正确的代码可更换为一下:

ggplot(aes(x = friend_count),
       data = subset(pf, !is.na(gender))) + geom_histogram()

ggplot(aes(x = friend_count),
       data = subset(pf, !is.na(gender))) + geom_histogram()+
  facet_wrap(~gender,ncol = 1)

版权声明:本文为weixin_41100555原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。