Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constant width in ggplot barplots

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 failed try:

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 as well. I need the second plot automatically to have less width and the same bar width and spaces as the first one.

like image 733
Ali Avatar asked Aug 25 '13 12:08

Ali


People also ask

How to increase the width of bars in ggplot2 barplot?

Return : ggplot2 BarPlot. When we want to Increase the Width of Bars and Decrease Space between Bars, we simply have to use width parameter to the geom_bar () function. Here we set the value of the width parameter as 0.98.

What is the use of width parameter in ggplot2?

width : Represents Width of the bars. Return : ggplot2 BarPlot. When we want to Increase the Width of Bars and Decrease Space between Bars, we simply have to use width parameter to the geom_bar () function.

How many rows and variables are in a ggplot2 Dataframe?

It shows that our example data consists of five rows and two variables. The column x defines the groups (i.e. the bars) in our data. The column y specifies the height of each bar. In this tutorial, we also need to install and load the ggplot2 package.

What does the column x mean in ggplot2?

The column x defines the groups (i.e. the bars) in our data. The column y specifies the height of each bar. In this tutorial, we also need to install and load the ggplot2 package.


1 Answers

Edit:

It appears the OP simply wants this:

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

I am not sure, if it's possible to pass absolute widths 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 could 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

like image 56
Roland Avatar answered Sep 21 '22 07:09

Roland