Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hHw to make width of bars equal in ggplot2 barplot?

Tags:

r

ggplot2

I am trying to make the width of all bars in the following plot equal. Can anybody help me? is it possible? or is there any way to plot this data?

library(ggplot2)
dat <- data.frame(x = c('I','I','I','I','II','II'),
                  y = LETTERS[1:6],
                  z = abs(rnorm(6)))

ggplot(dat, aes(y,z))+ 
  geom_bar(stat = "identity") +
  facet_wrap(~x,scales="free")

I also tried using arguments size and width inside the geom_bar but its not working. enter image description here

like image 430
Koundy Avatar asked Feb 12 '23 18:02

Koundy


1 Answers

Really the problem is that each of the facet panels is being forced to be the same size and then the plot inside expands to fill all the available room. With facet_grid you can adjust the space for each facet panel (but you cannot seem to do this with facet_wrap). Try

ggplot(dat, aes(y,z))+ 
  geom_bar(stat = "identity") +
  facet_grid(~x,scales="free", space="free_x")

which gives me

enter image description here

like image 117
MrFlick Avatar answered Feb 14 '23 11:02

MrFlick