How to make the width of bars and spaces between them fixed for several barplots using ggplot
, having different number of bars on each plot?
Here is a f
Wrapped the other suggestions in a function that only requires a single graph.
fixedWidth <- function(graph, width=0.1) {
g2 <- graph
#store the old widths
old.unit <- g2$grobs[[4]]$children[[2]]$width[[1]]
original.attibutes <- attributes(g2$grobs[[4]]$children[[2]]$width)
#change the widths
g2$grobs[[4]]$children[[2]]$width <- rep(width,
length(g2$grobs[[4]]$children[[2]]$width))
#copy the attributes (units)
attributes(g2$grobs[[4]]$children[[2]]$width) <- original.attibutes
#position adjustment (why are the bars justified left???)
d <- (old.unit-g2$grobs[[4]]$children[[2]]$width[[1]])/2
attributes(d) <- attributes(g2$grobs[[4]]$children[[2]]$x)
g2$grobs[[4]]$children[[2]]$x <- g2$grobs[[4]]$children[[2]]$x+d
return(g2)
}