Barplot定制

时间:2015-09-20 10:16:53

标签: r

所以我有这个数据表

   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

和代码

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)
)

输出此条形图 enter image description here

现在我想绘制这个条形图,将图例放在条形图之外,并将图形旋转w1,w2,w3,w4 ... 45度。

enter image description here

上面的图片是在excel中创建的。

2 个答案:

答案 0 :(得分:3)

虽然刻面也很有效,但也可以缩小横条。当然,调整酒吧和文字的躲避:

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

答案 1 :(得分:1)

这个很接近。我对躲避并不满意。

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

这是@ Roman解决方案的一个微不足道的扩展,将W置于各个方面。

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")