使用连续数据的空间直方图(宽度= ...不起作用) [英] Histogram with spaces using continuous data (width=... doesn't work)
本文介绍了使用连续数据的空间直方图(宽度= ...不起作用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这对离散数据是没有问题的:
b = data.frame(x = sample(LETTERS [1:3],size = 50,replace = T))
ggplot b,aes(x = x))+ geom_bar(width = .3)
然而,使用连续数据, width
似乎没有效果。
pre $ $ c $ a = data.frame(x = rnorm(100))
ggplot(a ,aes(x = x,width = .5))+
geom_bar(width = .3,binwidth = 1)
如何将具有间隔条的直方图归档为连续数据?
解决方案
我认为这样做是一个非常糟糕的主意
以下是一种可能性:
面包机ks < - 漂亮(范围(a $ x),n = 6,min.n = 1)
mids <-0.5 *(break [-1L] + breaks [-length(breaks)])
ggplot(a,aes(x = cut(x,breaks = breaks,labels = mids)))+
geom_bar(width = .3)
I'm trying to plot a histogram using ggplot which has some space between the bars.
This is no problem with discrete data:
b= data.frame(x=sample(LETTERS[1:3],size=50, replace=T))
ggplot(b, aes(x=x)) + geom_bar(width=.3)
However, using continuous data, width
seems to have no effect.
a= data.frame(x=rnorm(100))
ggplot(a, aes(x=x, width=.5)) +
geom_bar(width=.3, binwidth=1)
How can a histogram with spaced bars be archived for continuous data?
解决方案
I think doing this is a really bad idea (and ggplot2 doesn't support it).
Here is one possibility:
breaks <- pretty(range(a$x), n = 6, min.n = 1)
mids <- 0.5 * (breaks[-1L] + breaks[-length(breaks)])
ggplot(a, aes(x = cut(x, breaks = breaks, labels = mids))) +
geom_bar(width=.3)
这篇关于使用连续数据的空间直方图(宽度= ...不起作用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文