I want to generate bar charts using ggplot2
that will follow a strict standard:
- they must be of certain dimensions (width and height)
- the width of bars must be fixed, in pixels, regardless of how many bars happen to be in the plot
- the space between bars has to be fixed, in pixels, regardless of number of bars
I work with RStudio, that allows responsiveness in its viewer. That means that when I expand the viewer's boundaries, the plot is stretched accordingly, increasing bars' width and spaces between them. Conversely, making the viewer's boundaries smaller would make the bars thinner and reduce space between them.
Similarly, in given boundaries of the viewer, plotting a bar chart would yield different bar width for 6 bars than when there are 2 bars only.
Demonstration
library(ggplot2)
library(dplyr)
p_all_bars <-
mpg %>%
ggplot(aes(x = class)) +
geom_bar()
p_two_bars <-
mpg %>%
filter(class == "compact" | class == "suv") %>%
ggplot(aes(x = class)) +
geom_bar()
p_all_bars
p_two_bars
If I save both plots with dimensions of width = 1000 pixels
and height = 650 pixels
it's clear that both bar width and space between bars are different from one plot (7 bars) to another (2 bars).
Bottom line
How can I set absolute values for plot's height and width, in pixels, as well for bars' width and space between bars, in pixels too -- regardless of number of bars in the plot?