我正在使用ggplot2在R中创建一个图表。在绘图下方和x轴上方有空间我想要删除。
基于此问题How to remove space between axis & area-plot in ggplot2?我尝试将expand = c(0,0)
添加到scale_x_continuous()
中,但这似乎无法解决问题。
我认为有一个简单的解决方案,我不知何故。先感谢您。
我的代码:
q_cbb <- ggplot(cbb.sum, aes(year, CBB.Incidence, fill=category)) +
geom_vline(xintercept = seq(2008.5,2011.5), color="gray") +
geom_hline(yintercept = seq(10,70,10), color="gray") +
geom_bar(stat="identity", position="dodge") +
geom_errorbar(aes(ymin=CBB.Incidence-se, ymax=CBB.Incidence+se), width=0.2, position=position_dodge(.9)) +
scale_x_continuous(limits=c(2007.5,2012.5), expand=c(0,0))+xlab("Year")+ylab("Mean Incidence (%)") +
ggtitle("CBB Incidence, Local vs All Other Var.") +
scale_fill_discrete(name = "Type", labels=c("All Other Vars.", "Local", "CBB resistant")) +
theme(plot.title = element_text(hjust = 0.5)) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
ylim(0,70) +
theme(axis.line = element_line(colour = "black"))
q_cbb
最后,这是我的数据:
category year N CBB.Incidence sd se ci
1 local 2008 21 23.023810 31.60221 6.896168 14.385155
2 local 2009 34 8.823529 19.40136 3.327306 6.769454
3 local 2010 39 14.530769 22.94635 3.674356 7.438345
4 local 2011 42 61.026190 31.16832 4.809376 9.712731
5 local 2012 21 37.928571 39.22975 8.560633 17.857167
6 other 2008 105 22.603810 30.12916 2.940305 5.830736
7 other 2009 81 13.583951 19.65220 2.183578 4.345459
8 other 2010 130 37.864615 34.44426 3.020962 5.977046
9 other 2011 91 45.787912 38.36813 4.022073 7.990550
10 other 2012 84 25.309524 31.10835 3.394199 6.750926
12 resistant 2008 256 39.829687 37.42764 2.339227 4.606665
13 resistant 2009 238 22.633613 28.69892 1.860275 3.664786
14 resistant 2010 467 32.500214 32.15739 1.488067 2.924152
15 resistant 2011 464 48.953233 40.72924 1.890808 3.715628
16 resistant 2012 441 22.929478 27.36686 1.303184 2.561239