是否可以删除带有多个垃圾箱的直方图中的空间? [英] Is it possible to remove space in histogram with many bins?

查看:103
本文介绍了是否可以删除带有多个垃圾箱的直方图中的空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在绘制带有许多bin(1000+)的直方图.代码如下:

I am plotting a histogram with many bins (1000+). Code is below:

library(tidyverse)
xx=rpois(1000,lambda = 40)
y=density(xx,n=1000,from=0)

ggplot(data.frame(x = y$x, y = y$y), aes(x, y)) + 
  geom_line() + 
  geom_histogram(stat='identity',aes(fill=y))

如您所见,它在我的小节之间添加了不必要的间距(我想要一个无缝的渐变).有没有办法删除这些空间?作为参考,我知道使用geom_segment来获得这些结果的方法,因此目前,我仅对解决直方图问题感兴趣(如果可能的话).在aes中设置color=y可以工作,但是我更大的目标是使用一个hacky代码,该代码可以让我用两个完全独立的渐变来绘制两个密度曲线(因此,我需要将geom_segmentcolor保存为其他密度)曲线).

As you can see, it is adding unwanted spacing in between my bars (I would like a seamless gradient). Is there a way to remove these spaces? For reference, I am aware of the way to use geom_segment to achieve these results so for right now I am only interested in fixing the histogram issue (if possible). Setting color=y inside the aes works but my larger goal here is a hacky code that lets me essentially graph two density curves with two completely separate gradients (thus I need to save my geom_segment and color for the other density curve).

谢谢!

推荐答案

添加width=语句(根据您的比例而变化),以强制钢筋重叠.它的确给了我一个警告消息position_stack requires non-overlapping x intervals,但它仍然在打印时没有空格.我的下图如下:

Adding a width= statement (this varies depending on your scale) that forces the bars to overlap works. It does give me a warning message position_stack requires non-overlapping x intervals but it still plots without the spaces. My exmaple below:

library(tidyverse)
xx=rpois(1000,lambda = 40)
y=density(xx,n=2000,from=0)

ggplot(data.frame(x = y$x, y = y$y), aes(x, y)) + 
  geom_histogram(stat='identity',color=NA,aes(fill=y),width=0.1) +
  geom_line()

这篇关于是否可以删除带有多个垃圾箱的直方图中的空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆