2016-07-22 46 views
0

Minitab中,很容易创建“分割点”直方图。这可以在ggplot2完成吗?ggplot2中的分界点直方图

例如,

df <- data.frame(x = c(0.08, 0.21, 0.25, 0.4, 0.5, 0.6)) 
ggplot(df, aes(x = x)) + geom_histogram(binwidth = 0.1) 

enter image description here

正如你可以在这里看到,R默认为 “中点” 直方图。包含0.08的酒吧在0.1处标有酒吧。包含0.210.25的栏正在标记为0.2等等。

我可以以某种方式改变这些酒吧因此第一条涵盖00.1之间的区域,而第二条涵盖0.20.3,等等之间的区域?

+2

不知道你想要输出什么,但这样做吗? 'ggplot(df,aes(x = x))+ geom_histogram(breaks = seq(0,max(df $ x),by = 0.1))' –

+0

边界参数也应该在这里: 'ggplot(df,aes (x = x))+ geom_histogram(binwidth = 0.2,boundary = 1)' – pHroc

回答

1

您可以通过两种方式摆脱问题:使用参数“中心”或使用“边界”。使用“center”可以指定其中一个元素的中心,“boundary”类似,但是您可以指定两个元素之间边界的值。值得注意的是,“中心”和“边界”可以高于或低于数据范围,在这种情况下,所提供的值将被移动足够的宽度数量。

在这种情况下,你已经知道了垃圾桶,它的边界的宽度,所以用这个参数,你可以很容易地做你问:

library(ggplot2) 
df <- data.frame(x = c(0.08, 0.21, 0.25, 0.4, 0.5, 0.6)) 

# This is "center" solution: 
ggplot(df, aes(x = x)) + geom_histogram(binwidth = 0.1, center=0.05) 

# This is with "boundary" parameter 
ggplot(df, aes(x = x)) + geom_histogram(binwidth = 0.1, boundary=0.1) 

您可以找到参考的细节和更多信息geom_histogram 。 希望这可以帮助