【发布时间】:2018-05-06 22:07:40
【问题描述】:
直方图中断
在基本图中,当您使用hist 函数时,它会使用nclass.Sturges 函数自动计算中断,但是在 ggplot 中,您必须提供中断。
如果我绘制经典 faithfull 数据的直方图,我会得到以下图表:
data("faithful")
hist(faithful$waiting)
这行得通
我发现在这个question,你可以模仿
library(tidyverse)
data("faithful")
brx <- pretty(range(faithful$waiting), n = nclass.Sturges(faithful$waiting), min.n = 1)
ggplot(faithful, aes(waiting)) +
geom_histogram(color="darkgray", fill="white", breaks=brx)
但这不是
但我想在我的 ggplot 函数中添加中断,所以我尝试了这个:
ggplot(faithful, aes(waiting)) +
geom_histogram(color="darkgray", fill="white",
aes(breaks=pretty(range(waiting),
n = nclass.Sturges(waiting),
min.n = 1)))
这给了我以下错误:
Warning: Ignoring unknown aesthetics: breaks
Error: Aesthetics must be either length 1 or the same as the data (272): breaks, x
我明白它的意思,但是我可以把长度为a的美学放在aes中,例如:
ggplot(faithful, aes(waiting)) +
geom_histogram(color="darkgray", fill="white", breaks=brx,
aes(alpha = 0.5))
我做错了什么?
【问题讨论】:
-
根据您的数据,
brx是长度为 13 的向量,而不是长度为 1。无论如何geom_histogram(使用与geom_bar相同的美学)不理解breaks作为一种美学映射,所以即使像aes(breaks = 1)这样的东西也会被忽略。