【问题标题】:Barplot customization条形图定制
【发布时间】:2015-09-20 10:16:53
【问题描述】:

所以我有这个数据表

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

输出此条形图

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

上面的图片是用excel创建的。

【问题讨论】:

  • 有关您的图例,请参阅stackoverflow.com/questions/3932038/…,即您也可以使用xpd 作为标签。试试mat &lt;- as.matrix(t(tabela.matrix1)) ;qw &lt;- 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,谢谢。这很有帮助。
  • 不客气......请随时扩展它并将其添加为答案

标签: r


【解决方案1】:

虽然刻面也可以,但也可以使条形变小。调整条形和文本的闪避是不正常的:

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)

【讨论】:

    【解决方案2】:

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

    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)
    


    这是@Roman 解决方案的一个简单扩展,将Ws 放入多个方面。

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

    【讨论】:

    • @Roman Luštnik,如何在栏组之间添加更多空间。换句话说,像我上面的图片一样添加空格(excel版本)?
    • 我在您的答案中添加了分面方法。请随意删除。
    • 感谢您的帮助,@jlhoward。
    • 我添加了一个改进闪避的例子,你不满意希望它让你更快乐 ;-)。
    猜你喜欢
    • 1970-01-01
    • 2013-07-11
    • 1970-01-01
    • 2020-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多