我是一个总R新手学习ggplot。
我不明白为什么第一个代码段有效而第二个代码段无效。我想找到一个很好的binwidth而不用猜测,所以我尝试了一个无效的实验。

library(ggplot2)
attach(diamonds)
d <- diamonds
x <- ggplot(d, aes(x = price))
x <- x + geom_histogram(binwidth = 50)
x
# worked fine, but using the sequence and substituting i didn't
i <- seq(1, 101, by = 10)  #tried to avoid the first arg as zero, but didn't work
x <- ggplot(d, aes(x = price))
x <- x + geom_histogram(binwidth = i)
x

第二个抛出错误
Error in seq.default(round_any(range[1], size, floor), round_any(range[2],  :
  'from' must be of length 1
Error in exists(name, envir = env, mode = mode) :
  argument "env" is missing, with no default

我不明白它想要什么。
非常感谢

最佳答案

试试这个:

i<-seq(1,101, by=10)
x1<- ggplot(d, aes(x=price))
x2<-lapply(i,function(i)
x1+geom_histogram(binwidth=i)

)
To access each plot:
x2[[1]] # for bw 1
x2[[2]] #bw 11 and so on

关于r - 如何动态设置直方图binwidth,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30278699/

10-12 19:11