我昨天运行这段代码并且它有效但是在今天运行之后,我得到了这个错误。 错误:StatBin 需要一个连续的 x 变量,而 x 变量是离散的。也许你想要 stat="count"?
library(rattle)
dsname <- "weatherAUS"
ds <- get(dsname)
p <- ggplot(ds, aes(Location, fill=Location))
p <- p + geom_bar(width=1, colour="white")
p <- p + theme(legend.position="none")
p <- p + coord_flip()
p <- p + geom_text(stat="bin",
color="white",
hjust=1.0,
size=3,
aes(y=..count.., label=..count..))
我想知道如何解决这个问题。
最佳答案
根据我的评论,在不检查可能会破坏的内容的情况下,update.packages
甚至 install.package
都不是一个好主意。 ggplot2 2.0 有一些核心差异,Hadley 非常善于沟通。
这是代码的工作版本:
library(rattle)
library(ggplot2)
data(weatherAUS)
p <- ggplot(weatherAUS, aes(Location, fill=Location))
# using geom_bar will automatically make a new "count" column
# available in an internal, transformed data frame. the help
# for geom_bar says as much
p <- p + geom_bar(width=1, colour="white")
# geom_text can then access this computed variable with
# ..count.. (I still thin that's horrible syntax, hadley :-)
p <- p + geom_text(aes(y=..count.., label=..count..),
stat="count", color="white",
hjust=1.0, size=3)
p <- p + theme(legend.position="none")
p <- p + coord_flip()
p
# to be more explicit to other readers of your code, you
# could also do this instead of the `geom_bar` call
p <- p + stat_count(width=1, colour="white", geom="bar")
除非这只是为了练习 ggplot2,否则为每个条形图单独着色对于“生产”可视化来说可能不是一个好主意。
关于r - 错误 : StatBin requires a continuous x variable the x variable is discrete. 也许你想要 stat ="count"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34838496/