2

I'm making a chart in R using ggplot2. There is space below the plot and above the x-axis that I want to remove.

Based on this question How to remove space between axis & area-plot in ggplot2? I tried adding expand = c(0,0) into scale_x_continuous() but this does not seem to resolve the problem.

I'm thinking there is an easy solution that I'm somehow missing. Thank you in advance.

My code:

     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

The plot:

And lastly, here's my data:

    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
Community
  • 1
  • 1
JayB
  • 103
  • 2
  • 9
  • 2
    Add `scale_y_continous(expand = c(0, 0))` to your plot, instead of putting `expand` in the x axis. – Juan Bosco Jan 17 '17 at 00:04
  • 1
    Because you have set `limits`, the `expand` is relative to the `limits`, not the extent of your data. – Henrik Jan 17 '17 at 00:07
  • It is better to use theme() than geom_hline/vline to make background grid e.g. `theme(panel.grid.major = element_line(color = "pink"))` – Richard Telford Jan 17 '17 at 13:37
  • @RichardTelford Thanks! I didn't like the placement of the grid lines with panel.grid.major so I placed them in manually using geom_hline/vline. It just allowed me to have more control over where in the plot they are placed. – JayB Jan 17 '17 at 17:19
  • @JuanBosco Thanks! This solved it.I appreciate the help. – JayB Jan 17 '17 at 17:50

0 Answers0