【问题标题】:plot histogram with a last bin tending to Inf绘制最后一个 bin 趋于 Inf 的直方图
【发布时间】:2013-11-02 19:58:54
【问题描述】:

我想绘制一个直方图,在其最后一个 bin 中包含 Inf

[0,2.5] ...... [50,Inf]

c (2.5, 5.0, 7.5, 10.0, 12.5, 15.0, 20.0, 25.0, 35.0, 50.0, Inf)

指每个 bin 的端点

[0,2.5] ...... [50,Inf]

hist ( c (2.5 , 5.0 , 7.5, 10.0, 12.5, 15.0, 20.0, 25.0, 35.0, 50.0, Inf ) , c (6.6 ,12.5 ,15.2 ,16.6 ,15.8 ,11.0 ,13.1 ,4.6 ,3.0 ,1.1 ,0.5 ))

我收到以下错误消息:maybe 'breaks' do not span range of 'x'

【问题讨论】:

  • 目前还不完全清楚您要做什么。第二行 (c(6.6,...)) 被解释为 breaks 参数(但它看起来不正确:这是您想要与第一个合并的第二个数据集吗?)。如果您没有 breaks 参数,则 Inf 值将被静默排除。
  • c (2.5 , 5.0 , 7.5, 10.0, 12.5, 15.0, 20.0, 25.0, 35.0, 50.0, 指的是每个 bin 的端点我想代表 c 中的每个值 (6.6 ,12.5 ,15.2 ,16.6 ,15.8 ,11.0 ,13.1 ,4.6 ,3.0 ,1.1 ,0.5 ) 按 bin : [0,2.5] ...... [50,Inf]

标签: r plot histogram


【解决方案1】:

这真的不是很好。

brkvec <- c (2.5 , 5.0 , 7.5, 10.0, 12.5, 15.0, 20.0, 25.0, 35.0, 50.0, 
             Inf )
dat <- c (6.6 ,12.5 ,15.2 ,16.6 ,15.8 ,11.0 ,13.1 ,4.6 ,3.0 ,1.1 ,0.5 )

hist (dat , breaks=brkvec )
## Error in hist.default(dat, breaks = brkvec) : 
##     some 'x' not counted; maybe 'breaks' do not span range of 'x'

在零处添加左断点来处理这个问题:

brkvec <- c(0,brkvec)
hist(dat, brkvec)

但是 R 不喜欢这样:

## Error in plot.window(xlim, ylim, "") : need finite 'xlim' values

如果我们想要这个,我们必须通过调整轴标签来破解它:

brkvec2 <- brkvec
brkvec2[brkvec2==Inf] <- max(brkvec2[is.finite(brkvec2)])+1
hist(dat,breaks=brkvec2,col="gray",freq=FALSE,axes=FALSE)
par(las=1,bty="l") ## cosmetic
axis(side=2)
axis(side=1,at=brkvec2,labels=brkvec)

...但是最后一个标签甚至不会出现,除非你有一个非常宽的情节,因为 R 抑制重叠标签的方式。您可能更喜欢:

tt <- table(cut(dat,brkvec))
barplot(tt,names=names(tt))

这并不代表任何明智的类别的宽度 方式,但很难表示(50,Inf] 的宽度 无论如何分类...

【讨论】:

  • 感谢您的帮助,感谢您的努力
【解决方案2】:
hist (c(6.6 ,12.5 ,15.2 ,16.6 ,15.8 ,11.0 ,13.1 ,4.6 ,3.0 ,1.1 ,0.5 )),breaks= c(0, 2.5 , 5.0 , 7.5, 10.0, 12.5, 15.0, 20.0, 25.0, 35.0, 50.0, Inf))

【讨论】:

  • Error: unexpected ',' in ... 后跟(修正错字后)Error in plot.window(xlim, ylim, "") : need finite 'xlim' values
  • oops 忘记了 inf,max(x) 呢,而不是 inf
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-22
  • 1970-01-01
相关资源
最近更新 更多