3

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.

Community
  • 1
  • 1
  • +1 for posting your first R question with an easily reproducible example together with the code you have tried. Cheers. – Henrik Nov 30 '13 at 16:28

2 Answers2

3

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

Didzis Elferts
  • 95,661
  • 14
  • 264
  • 201
1

This does not directly answer your question about space between the bars, but provides another way to think about presenting your data. Assuming df has your original data (before melt(...)).

gg <- reshape(df,idvar="COMPANY",
              varying=list(c(2,4),c(3,5)), v.names=c("Individual","Average"),
              timevar="Metric", times=c("Performance","Learning"),
              direction="long")

ggplot(gg)+
  geom_point(aes(x=COMPANY,y=Individual))+
  geom_hline(aes(yintercept=Average))+
  geom_linerange(aes(x=COMPANY,ymin=Average,ymax=Individual,
                     color=factor(sign(Individual-Average))))+
  facet_grid(~Metric) +
  labs(x="",y="Scores") +
  theme(legend.position="none",
        axis.text.x=element_text(angle=90, vjust=0.4, hjust=1, size=15, face="bold"))

Produces this:

jlhoward
  • 58,004
  • 7
  • 97
  • 140
  • Thanks. This is really interesting. I need to play a bit with it in order to see how to make it suitable to produce single company reports. Thanks a lot. – user2993492 Nov 30 '13 at 22:25