Constant width in ggplot barplots

How to make the width of bars and spaces between them fixed for several strokes using ggplot , having a different number of bars on each chart?

Here is a failed attempt:

 m <- data.frame(x=1:10,y=runif(10)) ggplot(m, aes(x,y)) + geom_bar(stat="identity") 

enter image description here

 ggplot(m[1:3,], aes(x,y)) + geom_bar(stat="identity") 

enter image description here

Adding width=1 to geom_bar(...) doesn't help either. I need a second chart automatically to have a smaller width and the same width and spaces as the first.

+4
source share
2 answers

Edit:

It seems OP just wants this:

 library(gridExtra) grid.arrange(p1,arrangeGrob(p2,widths=c(1,2),ncol=2), ncol=1) 

I am not sure if it is possible to go absolute width to geom_bar . So here is an ugly hack:

 set.seed(42) m <- data.frame(x=1:10,y=runif(10)) p1 <- ggplot(m, aes(x,y)) + geom_bar(stat="identity") p2 <- ggplot(m[1:3,], aes(x,y)) + geom_bar(stat="identity") g1 <- ggplotGrob(p1) g2 <- ggplotGrob(p2) 

I used str to find the correct grob and child. You can use more sophisticated methods to generalize this if necessary.

 #store the old widths old.unit <- g2$grobs[[4]]$children[[2]]$width[[1]] #change the widths g2$grobs[[4]]$children[[2]]$width <- rep(g1$grobs[[4]]$children[[2]]$width[[1]], length(g2$grobs[[4]]$children[[2]]$width)) #copy the attributes (units) attributes(g2$grobs[[4]]$children[[2]]$width) <- attributes(g1$grobs[[4]]$children[[2]]$width) #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 #plot grid.arrange(g1,g2) 

enter image description here

+4
source

Other suggestions are wrapped in a function that requires only one schedule.

 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) } 
0
source

Source: https://habr.com/ru/post/1498828/


All Articles