Home > OS >  How to remove whitespace between bars in geom bar ggplot2
How to remove whitespace between bars in geom bar ggplot2

Time:11-01

I would like to remove unnecessary white space in the following plot: figure

Data:

structure(list(hypoxic2 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L), levels = c("No", "Yes"), class = "factor"), Season = structure(c(3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), levels = c("Pre", "Peak", "Post"
), class = "factor")), row.names = c(NA, -185L), class = "data.frame")

Code used to generate plot above:

ggplot(df, aes(x = Season, ..count..))  
  geom_bar(aes(fill = hypoxic2),
           position = position_dodge2(width = 1, preserve = "single"))   
  #width = 0.9,
  #position = "dodge")   
  ggtitle("TITLE")  
  labs(x="Season", fill = "Hypoxic (DO < 2 mg/L)", y = "Frequency")   
  ggsci::scale_fill_igv(palette = "default")  
  scale_x_discrete(labels = c("Pre-Hypoxic",
                              "Peak-Hypoxic",
                              "Post-Hypoxic"))   
  scale_y_continuous(limits = c(0,100),
                     expand = expand_scale(mult = c(0, 0.01),
                                           add = c(0, 0)))  
  #scale_y_continuous(limits = c(0,100), breaks = seq(0,100,20),
  #                   expand = expand_scale(mult = c(0, 0.01),
  #                                         add = c(0, 0)))
  theme_bw()  
  theme(legend.position = c(0.85,0.9),
        plot.title = element_text(hjust=0.5, face = "bold", family = "Arial",
                                  size=15),
        legend.background = element_rect(fill = "white", color = 1),
        axis.ticks.x = element_blank(),
        axis.title = element_text(size=15),
        legend.title.align = 0.5,
        legend.title = element_text(face = "bold"),
        axis.title.y = element_text(size = 15, 
                                    margin = margin(t = 0, r = 10, b = 0, l = 0), 
                                    color = "black"),
        axis.title.x.top = element_text(size = 14, 
                                        margin = margin(t = 0, r = 0, b = 10, l = 0), 
                                        color = "black"),
        text = element_text(family = "Times New Roman"),
        axis.text.y = element_text(color = "black", 
                                   size = 10, 
                                   angle = 0, 
                                   vjust = 0.5, 
                                   hjust = 1,
                                   margin = margin(t = 0, r = 5, b = 0, l = 0)),
        axis.text.x = element_text(color = "black", 
                                   size = 12, 
                                   angle = 0, 
                                   vjust = 0, 
                                   hjust = 0.5,
                                   margin = margin(t = 0, r = 5, b = 0, l = 0)),
        strip.text.y = element_text(size=18), 
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank())

I feel like there is a lot of wasted space here. How do I remove some of the white space without completely putting all the bars together? Can I achieve this without increasing bar width?

Can I remove some of the white space (black ink in the figure below)?: what I would like to remove

I looked at other questions on here, such as enter image description here

  • Related