R ggplot中的移位条

R ggplot中的移位条,r,ggplot2,count,frequency,R,Ggplot2,Count,Frequency,我在ggplot中创建了一个条形图,以显示scheme列中的计数是如何随时间变化的(即从2001年到2016年) x轴是年,y轴显示频率(我使用填充=)来获取计数 数据集由两列组成(year和scheme)填充字符值: year scheme 2016 yes 2016 yes 2016 yes 2016 yes 2015 yes 2015 yes 2014 yes 2013 yes .... 2006 no 2006 no 2006 no 2006 no 2005 no 2005 no 20

我在ggplot中创建了一个条形图,以显示
scheme
列中的计数是如何随时间变化的(即从2001年到2016年)

x轴是
,y轴显示频率(我使用
填充=
)来获取计数

数据集由两列组成(
year
scheme
)填充字符值:

year scheme

2016 yes
2016 yes
2016 yes
2016 yes
2015 yes
2015 yes
2014 yes
2013 yes
....
2006 no
2006 no
2006 no
2006 no
2005 no
2005 no
2004 no
2003 no
2002 no
2002 no
2001 no
2001 no
我的代码:

a <- ggplot(s) + 
    stat_bin(aes(x=year, fill=scheme, group=scheme), geom="bar", position = "dodge",bins=30)
b <- a + scale_x_continuous(breaks = c(2001:2016), labels = factor(2001:2016))
c <- b + theme(axis.text.x=element_text(size = 10, colour = "black"))

a使用
binwidth=1
而不是
bins=30
。当您指定应该有30个BIN时,您要求将年份划分为端点为
seq(2001、2016,length.out=30)
中顺序值的段


所有奇怪的间隙都来自没有包含整数的垃圾箱。

我认为是scale\u x\u continuous造成了这个问题。您是否尝试删除该选项,然后将美学更改为
x=因子(年份)
?或者,您可以尝试使用scale_x_date选项将比例设置为日期-
scale_x_date(format=“%Y”)
可能会起作用。非常感谢。我只将
bins=30
更改为
binwidth=0.5
,看起来和我想要的一模一样。