2

So I have this data table

   AA  BB  CC  DD
W1 3.5 3.5 3.4 3.5
w2 3.4 3.7 3.6 3.5
w3 3.5 3.4 3.5 3.5
w4 3.5 3.4 3.5 3.5
w5 3.5 3.4 3.5 3.5
w6 3.5 3.4 3.5 3.5
w7 3.5 3.4 3.5 3.5
w8 3.5 3.4 3.5 3.5

and code

qw<-barplot(as.matrix(t(tabela.matrix1)), beside=TRUE, 
           col=c("yellow", "cornflowerblue", "yellowgreen","orchid4"))
text(qw, 0, round(as.matrix(t(tabela.matrix1)), 1),cex=1,pos=3,srt=90) 


#legend("bottom",
     # c("AA","BB","CC", "DD"),
      # fill=terrain.colors(4)
)

that outputs this barplot enter image description here

Now I would like to plot this barplots, place the legend outside barplot and also rotate leters w1, w2, w3, w4... for 45 degrees.:

enter image description here

Picture above was created in excel.

rawr
  • 20,481
  • 4
  • 44
  • 78
Miha
  • 2,559
  • 2
  • 19
  • 34
  • 2
    For your legend see http://stackoverflow.com/questions/3932038/plot-a-legend-outside-of-the-plotting-area-in-base-graphics ie You can also use `xpd` for the labels. Try `mat <- as.matrix(t(tabela.matrix1)) ;qw <- barplot(mat, beside=TRUE, axisnames = FALSE, col=c("yellow", "cornflowerblue", "yellowgreen","orchid4")) ; text(colMeans(qw[2:3,]), -0.25, labels = colnames(mat), xpd=TRUE, srt=45); legend(1,-0.5, c("AA","BB","CC", "DD"), fill=terrain.colors(4), horiz=TRUE, xpd=TRUE)` – user20650 Sep 20 '15 at 11:46
  • 1
    @user20650, thanks. That was helpful. – Miha Sep 20 '15 at 13:53
  • You're welcome... please feel free to expand upon it and add it as an answer – user20650 Sep 20 '15 at 14:18

2 Answers2

3

Although the facetting works as well, it is also possible to make the bars smaller. Off course with adjusting the dodging of bars and text:

ggplot(xym, aes(x = Var1, y = value, fill = Var2)) +
  theme_bw() +
  scale_fill_brewer(palette = "Set1") + 
  theme(legend.position = "bottom", axis.text.x = element_text(angle = 90,vjust = 0.2)) +
  geom_bar(stat = "identity", width = 0.7, position = position_dodge(width=0.7)) +
  geom_text(aes(x = Var1, y = 0.05, label = round(value, 2), fill = Var2), 
            angle = 90, position = position_dodge(width = 0.7), size = 4)

enter image description here

RHA
  • 3,677
  • 4
  • 25
  • 48
1

This one comes close. I'm not happy with the dodging.

xy <- matrix(runif(4*8), nrow = 8, ncol = 4)
colnames(xy) <- c("AA", "BB", "CC", "DD")
rownames(xy) <- paste("w", 1:nrow(xy), sep = "")

library(ggplot2)
library(reshape2)
xym <- melt(xy)

ggplot(xym, aes(x = Var1, y = value, fill = Var2)) +
    theme_bw() +
    scale_fill_brewer(palette = "Set1") + 
    theme(legend.position = "bottom", axis.text.x = element_text(angle = 90,vjust = 0.2)) +
    geom_bar(stat = "identity", position = "dodge") +
    geom_text(aes(x = Var1, y = 0.05, label = round(value, 2), fill = Var2), 
              angle = 90, position = position_dodge(width = 1.03), size = 4)

enter image description here


This is a trivial extension of @Roman's solution, putting the Ws into facets.

ggplot(xym, aes(x = Var2, y = value, fill = Var2)) +
  theme_bw() +
  scale_fill_brewer(palette = "Set1") + 
  theme(legend.position = "bottom", axis.text.x = element_text(angle = 90,vjust = 0.2)) +
  geom_bar(stat = "identity", position = "dodge") +
  geom_text(aes(x = Var2, y = 0.1, label = round(value, 2), fill = Var2), 
            angle = 90, position = position_dodge(width = 1.03), size = 4)+
  facet_grid(.~Var1, scales="free_x")

jlhoward
  • 58,004
  • 7
  • 97
  • 140
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
  • @ Roman Luštnik, how can I add morespace between group of bars. In other words, add space like in my picture above (excel version)? – Miha Sep 20 '15 at 14:00
  • I added the faceting approach to your answer. Please feel free to remove. – jlhoward Sep 20 '15 at 18:25
  • Thank you for the helpful addition, @jlhoward. – Roman Luštrik Sep 20 '15 at 18:27
  • 1
    I've added an example with improved dodging, you were not happy about Hope it makes you happier ;-). – RHA Sep 20 '15 at 19:29
  • As it was rejected as an edit, i will have to post it below as an answer. – RHA Sep 21 '15 at 12:59
  • @RomanLuštrik 3 people did, because "This edit was intended to address the author of the post and makes no sense as an edit. It should have been written as a comment or an answer". So i posted it as an answer, although it felt a bit like stealing your code. Although the improvement is gigantic off course... ;-) – RHA Sep 23 '15 at 13:15
  • Is it possible to add more space between labels in legend? I tried **"p + guides(fill=guide_legend( keywidth=0.1, keyheight=0.1, default.unit="inch") )** but it is not what I am looking for. – Miha Sep 30 '15 at 13:33
  • 1
    @Miha see http://stackoverflow.com/questions/11366964/is-there-a-way-to-change-the-spacing-between-legend-items-in-ggplot2 – Roman Luštrik Sep 30 '15 at 16:18