Removing space between bars of the bar plot

25 views
Skip to first unread message

msus msus

unread,
Jul 18, 2018, 10:25:23 AM7/18/18
to ggplot2
Hi all,

I would like to remove the too much white space between bars of the attached bar plot.   But, I could not remove this.  I followed several solutions posted on stack overflow and in this google forum as well- nothing worked.  My graph is generated from metagenomics data.  Here's the code I originally used to generate this graph:

ggplot(data = psmelt(ps.taxa_rarefy_control_CR_JAX_TAC_hi_abundant), mapping = aes_string(x = "vendor", y = "Abundance")) + 
  geom_bar(aes(color=Family, fill=Family), stat="identity", position="fill", width = 0.2) + theme_bw() + 
  theme(panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"), legend.position = "bottom") + xlab("   ") +
  guides(color = guide_legend(nrow=6)) +
  scale_x_discrete(expand = c(0, 0))
  
I will greatly appreciate if you please let me know how can I remove the space between CR, JAX and TAC bars.   I also tried the binwidth=0 within ggplot function, but it didn't work.


Thanks,

Saif
CR,JAX,TAC control_rarefied_hi_abundant_fill_Family.jpg

Brandon Hurr

unread,
Jul 18, 2018, 10:59:16 AM7/18/18
to saif....@gmail.com, ggplot2
Saif, 

I think there are two issues. 

1. you're specifying the width of the bars at 0.2, this will make your bars 20% of the width of the space. This makes them nice and narrow like you have above.
2. The second issue is that your legend is super large because of the names. That is stretching the plot out to fit it on the same window.

Here's an example:

library(tidyverse)


DF <- read.table(text="Rank ReallySuperDuperlongname1     ReallySuperDuperlongname2     ReallySuperDuperlongname3

1    500    250    50

2    400    100    30

3    300    155    100

4    200    90     10", header=TRUE)


# this is close to what you have

DF %>%

gather(., trait, value, -Rank) %>%

ggplot(., aes(x = Rank, y = value, fill = trait)) +

geom_bar(stat = "identity", position="fill", width = 0.2) +

theme_bw() + 

theme(panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"), legend.position = "bottom") +

xlab("   ") +

guides(color = guide_legend(nrow = 2)) +

scale_x_discrete(expand = c(0, 0))


# if you change the width to 0.9 (the default) you get fat bars that fill the space

DF %>%

gather(., trait, value, -Rank) %>%

ggplot(., aes(x = Rank, y = value, fill = trait)) +

geom_bar(stat = "identity", position="fill", width = 0.9) +

theme_bw() + 

theme(panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"), legend.position = "bottom") +

xlab("   ") +

guides(color = guide_legend(nrow = 2)) +

scale_x_discrete(expand = c(0, 0))




To fix the first issue, take the width argument out of the geom_bar() call. Or specify the default (0.9) as Above.


Regarding the legend, if you want to keep the width narrow but need that size of legend, I think you might have to extract the legend from the plot and use cowplot, patchwork, or gridExtra to patch it back together. 

HTH,
Brandon


--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages