防止在带有许多条的geom_bar中增加间距/条的宽度 [英] Preventing incosistent spacing/bar widths in geom_bar with many bars

查看:67
本文介绍了防止在带有许多条的geom_bar中增加间距/条的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在具有很多条形图的条形图中,会出现以下问题:条形之间的间距和/或条形的宽度变得不整齐,并且随着图的宽度的变化而变化.

  set.seed(23511)dat<-data.frame(x = 1:540,y = rnorm(540))库(ggplot2)ggplot(dat)+geom_bar(aes(x = x,y = y),stat ="identity") 

有没有办法解决这个问题?我试过玩 width 和整个图的大小都无济于事.

为回应alistaire的评论,以下是RStudio的前几个酒吧的屏幕截图.查看前10个值.

  x y1 1 0.94509602 2 0.92773783 3 0.43710334 4 -1.03330735 5 2.04733976 6 0.81741237 7 0.42778428 8 -0.43368879 9 0.215680110 10 0.4918345 

..对我来说,很明显,对于前三个正值,条形/条形之间的间隔要窄,而对于第二组3个正值,条形之间无间隔/条形更宽./p>

解决方案

我认为这是一个像素问题.如果条形的x从1.5到2.7像素,则将为一个像素宽;如果从1.9到3.1(相同宽度),则将为2像素宽.

您可以用线条代替条形.

  ggplot(data = dat,aes(x = x,y = y))+geom_segment(aes(xend = x,yend = 0),大小= 0.6) 

我认为您有时仍然会遇到像素问题,但是使用 size 进行控制可能会更容易.

In a bar plot with lots of bars the problem occurs that the spacing between bars and/or the widths of the bars becomes incosistent, also changing with changing the width of the plot.

set.seed(23511)
dat <- data.frame(x = 1:540, y = rnorm(540))

library(ggplot2)
ggplot(dat) +
  geom_bar(aes(x = x, y = y), stat = "identity")

Is there a way to solve this? I tried playing with width and the overall plot size to no avail.

In response to alistaire's comment here's a screenshot of the first few bars from RStudio. Looking at the first 10 values..

    x          y
1   1  0.9450960
2   2  0.9277378
3   3  0.4371033
4   4 -1.0333073
5   5  2.0473397
6   6  0.8174123
7   7  0.4277842
8   8 -0.4336887
9   9  0.2156801
10 10  0.4918345

.. to me it clearly looks like for the first 3 positive values there's space between the bars/the bars are narrower than for the second set of 3 positive values where there's no space between the bars/the bars are wider.

解决方案

I think this is a pixel issue. If the x of a bar goes from 1.5 to 2.7 pixels, it will be one pixel wide, if it goes from 1.9 to 3.1 (same width) it will be 2 pixels wide.

You could do lines instead of bars.

 ggplot(data=dat, aes(x=x, y=y)) + 
   geom_segment(aes(xend=x, yend=0), size = 0.6) 

I think you still sometimes run into pixel issues, but it's maybe easier to control with size.

这篇关于防止在带有许多条的geom_bar中增加间距/条的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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