美文网首页
geom_bar()之报错

geom_bar()之报错

作者: 啊吧啊吧鸭 | 来源:发表于2020-09-06 23:05 被阅读0次

geom_bar()绘制的条形图在单变量情况下,x轴对应变量、y轴对应变量的个数。但当绘制双变量时,直接添加变量则会出现如下报错:

rm(list=ls())
data("iris")
library(ggplot2)
base <- ggplot(iris,aes(iris$Species,iris$Sepal.Length))
p1 <- base +
  geom_bar()
p1
Error: stat_count() can only have an x or y aesthetic.
Run `rlang::last_error()` to see where the error occurred.
In addition: Warning messages:
1: Use of `iris$Species` is discouraged. Use `Species` instead. 
2: Use of `iris$Sepal.Length` is discouraged. Use `Sepal.Length` instead.

主要原因是:geom_bar()想要在Y轴显示X轴变量的个数,而不是我们输入的Y轴变量,因此做以下修改即可正常画图。

p2 <- base+
  geom_bar(stat = "identity")
p2

相关文章

网友评论

      本文标题:geom_bar()之报错

      本文链接:https://www.haomeiwen.com/subject/gamhektx.html