【问题标题】:R - ggplot2 - Right interval option in geom_histogramR - ggplot2 - geom_histogram 中的右区间选项
【发布时间】:2014-09-17 13:47:40
【问题描述】:

拥有范围从 0.42 到 1 的数据:

> summary(performance$SPC8)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 0.4210  0.7805  0.8590  0.8359  0.9220  1.0000    

我做了以下直方图:

ggplot(performance, aes(x=SPC8)) + 
  geom_histogram(aes(y = 100*(..count.. /74)), binwidth=.1, colour="black", fill="cadetblue3") + 
  geom_vline(aes(xintercept=mean(SPC8, na.rm=T)),color="red", linetype="dashed", size=1) +
  format_options

如何更改上面的代码,以便在“1”之后没有 bin?

【问题讨论】:

  • @PGreen 我没有和你说话,我同意答案完全解决了这个问题。事实上,我正在编译相同的答案,但花了太长时间来生成一个可重复的示例(这实际上是你提出问题时的工作)。
  • @Roland 抱歉,我没有看到 David Arenburg cmets,我以为您指的是我的问题。无论如何谢谢:-)

标签: r plot ggplot2 histogram


【解决方案1】:

geom_histogram() 使用 stat_bin() 将您的数据划分到 bin 中。 stat_bin() 的默认值是 right=FALSE,这意味着类以包含此值的值开始,以不包含此值的值结束,例如,类 0.9-1 将包含 0.9 但不包含 1。将其更改为相反方向只需将right=TRUE 添加到geom_histogram()

 ggplot(performance, aes(x=SPC8)) + 
  geom_histogram(aes(y = 100*(..count.. /74)), binwidth=.1, colour="black",
         fill="cadetblue3",right=TRUE) + 
  geom_vline(aes(xintercept=mean(SPC8, na.rm=T)),color="red", linetype="dashed", size=1)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-10
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-03
    相关资源
    最近更新 更多