Bar plots with identical bar width *and* identical bar spacing across *several* plots

Similarly to this question, I want to generate several and separate bar plots that always have the same bar width. That said, I also want that in each of these plots, the spacing between the bars is the same.

I also need these plots to be saved individually, and that each saved image size is proportional to the number of bars. I do not want to use facets or grids (which means similar questions on SO does not apply to me). Furthermore, I need a geom_bar() solution together with ggsave().

REPRODUCIBLE EXAMPLE

Libraries

library(tidyverse)
library(ggplot2)

Mock data (in reality, I have hundreds of variables and up to 15 levels for some factors)

df <- structure(list(Animal = structure(c(1L, 2L, 2L, 2L, 1L, 1L, 1L, 
                                          1L, 3L, 3L), levels = c("Cat", "Dog", "Horse"), class = "factor"), 
                     Sex = structure(c(2L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L), levels = c("Female", 
                                                                                           "Male"), class = "factor")), row.names = c(NA, 10L), class = "data.frame")

Function to generate bar plots

# Create counter
counter <- 0

# Function
make_bar_plots <- function(df){ 
  
  # We loop over the columns
  for (col in colnames(df)){ 
    
    # (1) Preparation ##########################################################
    # Store N
    N <- sum(!is.na(df[col]))
    # Store number of levels
    n_levels <- length(unique(df[!is.na(df[col]), col]))[1]
    # Update counter at each iteration
    counter <- counter + 1
    
    # (2) Basic bar plot #######################################################
    # We only plot factors
    if (is.factor(df[[col]])) {
      p <- ggplot(data = df[col] %>% filter(!is.na(df[col])),
                  aes(x=fct_rev(fct_infreq(!!sym(col)))))
      # Compute percents for bars
      p <- p + geom_bar(aes(y = (..count..)/sum(..count..)),
                        width = 0.35*n_levels/3, # We make column width proportional to number of levels!
                        fill = 'black',
                        size = 1.5
      ) 
      
      # (3) Annotations ########################################################
      
      # Add percent as annotation
      p <- p + geom_text(aes(label = scales::percent((..count..)/sum(..count..),
                                                     accuracy = 0.1L),
                             y = ((..count..)/sum(..count..))),
                         stat="count", hjust = -0.1, size=4.2)
      # Add a title
      p <- p + labs(title = paste(col),
                    subtitle = paste0('N = ', format(N, big.mark = ',')
                    ))
      
      # (4) Aesthetics #########################################################
      p <- p + theme_minimal()
      p <- p + theme(axis.title.y = element_blank(),
                     axis.title.x = element_blank(),
                     axis.text.x = element_blank(),
                     axis.text.y = element_text(colour='black', size = 12.5),
                     plot.margin = margin(r=25, l=1, b=0, t=1),
                     plot.title = element_text(size = 16.5),
                     plot.subtitle = element_text(size = 12.5),
                     panel.grid = element_blank()
                     )
      # Flip coordinates
      p <- p + coord_flip(clip = 'off')
      
      # (5) Print and export   #################################################
      print(p)
      # Export
      ggsave(paste(counter, '_', col, '.png'), path = 'plots/', 
             bg = 'white'
      )
    } 
  }
}

Output

make_bar_plots(df)

Plot 1

Plot 2

Discussion

We see that bar width is the same in both plots. This was achieved with the argument width = 0.35*n_levels/3 that is included in the function.

That said, the spacing between bars is different for Cat, Dog, and Horse (Plot 1), compared to Male and Female (Plot 2).

What I have tried:

  1. In the ggsave() call, to adjust the height of the generated image using a proportion (e.g., height = 2*n_levels/3). That said, the resulting plots have different bar width, see illustration below.
  2. In the aesthetics part of the function, to adjust aspect ratio (e.g., p + theme(aspect.ratio = 1/4)). This too makes the width of the bars different across plots.
  3. A combination of the above, with the same undesired results.

Illustration when using height = 2*n_levels/3

Plot 1

Plot 2

My 2 questions:

  1. How do we obtain the same spacing between bars across all plots, while also keeping bar width identical across all plots?
  2. Furthermore, how do we save an image whose height will be automatically adjusted to the number of bars? In my example, the height of the Sex plot (2 bars) would be smaller than that of the Animal plot (3 bars). Currently, both have the same height.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật