R ggplot2错误:将变量映射到y并同时使用stat=";bin";。来自';用于数据分析的精美图形';
我正试图学习R ggplot2错误:将变量映射到y并同时使用stat=";bin";。来自';用于数据分析的精美图形';,r,graphics,plot,ggplot2,R,Graphics,Plot,Ggplot2,我正试图学习ggplot2将其应用于我自己的数据,但在尝试使用以下代码再现哈德利·威克姆(Hadley Wickham)的《数据分析的优雅图形》(图4.10(右),第4章:4.9.1结合geoms和stats,第61页)一书中的绘图时遇到了一个问题: d <- ggplot(diamonds, aes(carat)) + xlim(0, 3) d + stat_bin(aes(y = 1, fill = ..count..), binwidth = 0.1, geom = "tile",
ggplot2
将其应用于我自己的数据,但在尝试使用以下代码再现哈德利·威克姆(Hadley Wickham)的《数据分析的优雅图形》(图4.10(右),第4章:4.9.1结合geoms和stats,第61页)一书中的绘图时遇到了一个问题:
d <- ggplot(diamonds, aes(carat)) + xlim(0, 3)
d + stat_bin(aes(y = 1, fill = ..count..), binwidth = 0.1, geom = "tile", position="identity")
如何重现所需的绘图/修复错误?如果可能的话,你能解释一下吗。如何使用geom_tile
生成相同类型的绘图
非常感谢你的帮助 运行
?geom\u tile
中的等效示例时,会产生相同的错误,例如cars+stat\u bin(aes(填充=…计数…),geom=“tile”,binwidth=3,position=“identity”)
。尽管如此,仍然可以找到输出,同时也显示了我认为在较旧的ggplot2
版本中出现的警告消息
一种可能的解决方案是使用带有伪y变量的stat\u bin2d
,并使用binwidth
参数。binwidth
向量中的第一个数字(c(0.1,1)
)表示x值,第二个表示y值binwidth
未记录在帮助文本的“参数”部分中,但可以在
更新:有关错误消息的详细说明,请参阅请参阅:
根据我的经验,对于ggplot2版本0.92,只有geom_bar()的警告,没有默认的stat参数。但对于1.0.0版,它将以错误结束。如果使用geom_bar为条形图指定stat=“identity”。我使用的是OSX 10.9.4;R Studio版本0.98.507;ggplot2版本1.0.0要更彻底地处理特定错误,您可以查看
# Error : Mapping a variable to y and also using stat="bin".
# With stat="bin", it will attempt to set the y value to the count of cases in each group.
# This can result in unexpected behavior and will not be allowed in a future version of ggplot2.
# If you want y to represent counts of cases, use stat="bin" and don't map a variable to y.
# If you want y to represent values in the data, use stat="identity".
# See ?geom_bar for examples. (Defunct; last used in version 0.9.2)
ggplot(diamonds, aes(x = carat, y = factor(1))) + xlim(0, 3) +
stat_bin2d(binwidth = c(0.1, 1))