直方图断点没有连续的断点

时间:2015-09-30 20:33:30

标签: r ggplot2 histogram

如何使用预定义的断点而不是连续断点在直方图中设置X轴。

breaks=c(0,5,10,100,200,1000)

根据bin范围内的频率选择断点。如何实现这一目标而不是breaks=c(0,5,10,15,20)个连续断点?

编辑: 我已分别计算频率并使用geom_bar绘制 expected output

期望与hist图表类似的输出。

3 个答案:

答案 0 :(得分:1)

也许我不理解这个问题。但你不能这样做吗?

x <- rnorm(1000, mean= 10, sd= 5)
range(x) # need to have the whole range of x in the histogram
hist(x, breaks= c(range(x)[1], 0, 5, 7, 12, 20, range(x)[2]))

library(ggplot2)
brks=c(range(x)[1], 0, 5, 7, 12, 20, range(x)[2])
ggplot(data.frame(x), aes(x=x)) + 
  geom_histogram(breaks= brks) + scale_x_continuous(breaks= brks)

显然,您可以为任何格式添加theme(... element_text())选项

答案 1 :(得分:1)

if you're still interested, maybe something like this:

library(plyr)
library(ggplot2)
library(dplyr)

data("diamonds")
head(diamonds)
c(0,1000,4000,20000) -> breaks
cut(diamonds$price, breaks = breaks) -> diamonds$newprice

diamonds %>%
  group_by(cut, newprice) %>% 
  summarise(num = n()) %>%
  as.data.frame -> newdi

ggplot(data = newdi) +
  geom_bar(aes(x = newprice, y = num, fill = cut), stat = "identity", position = "dodge") 

enter image description here

答案 2 :(得分:0)

一般来说,直方图将具有相同大小的二进制数,因为它旨在作为概率分布的估计值。

您可以自己进行计数聚合,然后利用public interface IFoo { int MyProp { get; } } [Fact] public void Test { var mock = new Mock<IFoo>(); mock.SetupAllProperties(); mock.Object.MyProp = 12; // this does not work because there is a get. } 创建一个包含特定bin间隔的条形图。