Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different space between bars geom_bar

I tried my best to read related questions to my issue. The most relevant was: question. However I was not able to figure out how to solve my issue. I had a dataset like

   structure(list(COMPANY = structure(1:5, .Label = c("Architecten", 
" 2.0", " Adema", "B.V.", 
" Alex"), class = "factor"), `YOUR COMPANY PERFORMANCE ORIENTATION` = c(5, 
7, 6, 4.5, 4.5), `AVERAGE PERFORMANCE ORIENTATION` = c(5.17, 
5.17, 5.17, 5.17, 5.17), `YOUR COMPANY LEARNING ORIENTATION` = c(5, 
5.6, 5.8, 6.2, 3.8), `AVERAGE LEARNING ORIENTATION` = c(5.67, 5.67, 5.67, 5.67, 5.67)), .Names = c("COMPANY", "YOUR COMPANY PERFORMANCE ORIENTATION", 
"AVERAGE PERFORMANCE ORIENTATION", "YOUR COMPANY LEARNING ORIENTATION", 
"AVERAGE LEARNING ORIENTATION"), class = "data.frame", row.names = c(NA, 
5L))

By using melt (from library reshape2) I was able to obtain:

structure(list(COMPANY = structure(c(1L, 2L, 3L, 4L, 5L, 1L, 
+ 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), class = "factor", .Label = c("Architecten", 
" 2.0", " Adema", "B.V.", 
" Alex")), variable = structure(c(1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 
4L, 4L), .Label = c("YOUR COMPANY PERFORMANCE ORIENTATION", "AVERAGE PERFORMANCE ORIENTATION", 
"YOUR COMPANY LEARNING ORIENTATION", "AVERAGE LEARNING ORIENTATION"
), class = "factor"), value = c(5, 7, 6, 4.5, 4.5, 5.17, 5.17, 
5.17, 5.17, 5.17, 5, 5.6, 5.8, 6.2, 3.8, 5.67, 5.67, 5.67, 5.67, 
5.67)), .Names = c("COMPANY", "variable", "value"), row.names = c(NA, 
-20L), class = "data.frame")

By using the following code, I was able to plot the average and company variables side by side:

plot_try <-ggplot(mydataset,aes(COMPANY,value,fill=variable))+
geom_bar(stat="identity",position="dodge")+ theme(axis.ticks = element_blank(), axis.text.x = element_blank())+ facet_wrap(~COMPANY)

i want to add space between the first two bars ("your company performance orientation" and "average performance orientation") and the last 2 bars, but I am not able to do that. Any suggestion on what to add to my code? Thanks.

like image 391
user2993492 Avatar asked Feb 13 '26 01:02

user2993492


1 Answers

Use your melted dataframe mydataset and add new variable that contains grouping number for each two bars you need to combine. Then use this new variable for the x values.

mydataset$group<-rep(c(1,2),each=10)
#as suggested by @Henrik more general solution
mydataset$group <- grepl("LEARNING", mydataset$variable)

ggplot(mydataset,aes(group,value,fill=variable))+
  geom_bar(stat="identity",position="dodge")+ 
  theme(axis.ticks = element_blank(), axis.text.x = element_blank())+ 
  facet_wrap(~COMPANY)

enter image description here

like image 152
Didzis Elferts Avatar answered Feb 15 '26 17:02

Didzis Elferts



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!