代码之家  ›  专栏  ›  技术社区  ›  Chris Ruehlemann

将基本R堆叠条形图移植到ggplot2,并使用B/W结构而不是颜色区分条形图级别

  •  0
  • Chris Ruehlemann  · 技术社区  · 3 年前

    我有一张这样的桌子:

    p_well <- structure(c(0, 0, 0.45, 0, 0, 0, 68.1, 0, 0, 0, 0, 0.45, 0.23, 
                          0, 12.22, 0.23, 0, 0, 0, 0.23, 0.23, 1.36, 1.13, 0.23, 0, 0.23, 
                          0, 0.45, 0, 0, 0, 0.23, 0.23, 0, 0, 0.45, 0, 0.45, 0, 0.9, 0.68, 
                          0.9, 0, 1.13, 0, 0, 0, 0, 0, 0.9, 0, 0.23, 0.23, 0.23, 0, 0, 
                          0.45, 0.23, 0, 0.45, 0.23, 0, 0, 0, 0, 5.88, 0, 0.45, 0, 0, 0, 
                          0.23), class = "table", .Dim = 8:9, .Dimnames = structure(list(
                            c("adjective", "as_well", "dispreferred_marker", "manner_adverb", 
                              "quote_marker", "restart_marker", "turn_preface", "unclear"
                            ), c("W1", "W2", "W3", "W4", "W5", "W6", "W7", "W8", "W9"
                            )), .Names = c("", "")))
    

    并希望在中绘制堆叠条形图 ggplot2 .我知道如何在 base R ,就像这样:

    par(mar = c(4.4,4,3,1))
    barplot(p_well, main = "Functions of 'well' by positions in turn", cex.main = 0.9,
            cex.axis = 0.8, cex.lab = 0.8, cex.names = 0.8,
            names.arg = colnames(p_well),
            xlab = "Positions", ylab = "%",
            col = c("red", "orange", "yellow", "green", "darkgreen", "lightblue", "blue"))
    legend("topright", rownames(p_well), 
           fill =  c("red", "orange", "yellow", "green", "darkgreen", "lightblue", "blue"),
           bty = "n", cex = 0.8)
    

    enter image description here

    但是,我想将地块迁移到 ggplot2 而且 我需要使用黑色/白色结构,而不是用颜色来区分不同级别的条形图 。怎么能做到呢?

    0 回复  |  直到 3 年前
        1
  •  1
  •   neuron    3 年前

    我想这可能就是你想要的

    p_well <- structure(c(0, 0, 0.45, 0, 0, 0, 68.1, 0, 0, 0, 0, 0.45, 0.23, 
                            0, 12.22, 0.23, 0, 0, 0, 0.23, 0.23, 1.36, 1.13, 0.23, 0, 0.23, 
                            0, 0.45, 0, 0, 0, 0.23, 0.23, 0, 0, 0.45, 0, 0.45, 0, 0.9, 0.68, 
                            0.9, 0, 1.13, 0, 0, 0, 0, 0, 0.9, 0, 0.23, 0.23, 0.23, 0, 0, 
                            0.45, 0.23, 0, 0.45, 0.23, 0, 0, 0, 0, 5.88, 0, 0.45, 0, 0, 0, 
                            0.23), class = "table", .Dim = 8:9, .Dimnames = structure(list(
                              c("adjective", "as_well", "dispreferred_marker", "manner_adverb", 
                                "quote_marker", "restart_marker", "turn_preface", "unclear"
                              ), c("W1", "W2", "W3", "W4", "W5", "W6", "W7", "W8", "W9"
                              )), .Names = c("", "")))
    
    p_well <- as.data.frame(p_well)
    
    ggplot(p_well, aes(fill=Var1, x=Var2, y=Freq )) + 
      geom_bar(position="stack", stat="identity") +
      xlab("Positions") +
      ylab("%") +
      theme_bw() +
      scale_fill_grey(start = 0, end = .9) +
      theme(legend.title=element_blank(),
            axis.line = element_line(colour = "black"),
            panel.grid.major = element_blank(),
            panel.grid.minor = element_blank(),
            panel.border = element_blank(),
            panel.background = element_blank(),
            legend.position = c(.8,.7))
    

    example

    如果要删除底部的间隙

    ggplot(p_well, aes(fill=Var1, x=Var2, y=Freq )) + 
      geom_bar(position="stack", stat="identity") +
      xlab("Positions") +
      ylab("%") +
      theme_bw() +
      scale_fill_grey(start = 0, end = .9) +
      scale_y_continuous(expand = c(0,0)) +
      theme(legend.title=element_blank(),
            axis.line = element_line(colour = "black"),
            panel.grid.major = element_blank(),
            panel.grid.minor = element_blank(),
            panel.border = element_blank(),
            panel.background = element_blank(),
            legend.position = c(.8,.7))
    

    example2

    您也可以使用反转顺序 forcats::fct_rev() 但你也需要从 scale_fill_grey(start = 0, end = .9) scale_fill_grey(start = .9, end = 0)

    ggplot(p_well, aes(x=Var2, y=Freq,fill = forcats::fct_rev(Var1))) + 
      geom_bar(position="stack", stat="identity") +
      xlab("Positions") +
      ylab("%") +
      theme_bw() +
      scale_fill_grey(start = .9, end = 0) +
      scale_y_continuous(expand = c(0,0)) +
      theme(legend.title=element_blank(),
            axis.line = element_line(colour = "black"),
            panel.grid.major = element_blank(),
            panel.grid.minor = element_blank(),
            panel.border = element_blank(),
            panel.background = element_blank(),
            legend.position = c(.8,.7))
    

    example3

    请将图案添加到可以使用的绘图中 ggpattern() 然而,使用黑白标尺和一些条形图的大小,很难区分各组之间的差异

    library(ggpattern)
    ggplot(p_well, aes(x=Var2, y=Freq)) + 
      geom_bar_pattern(position="stack",stat="identity",
                       mapping=aes(pattern=Var1)) +
      xlab("Positions") +
      ylab("%") +
      theme_bw() +
      scale_fill_grey(start = .9, end = 0) +
      scale_y_continuous(expand = c(0,0)) +
      theme(legend.title=element_blank(),
            axis.line = element_line(colour = "black"),
            panel.grid.major = element_blank(),
            panel.grid.minor = element_blank(),
            panel.border = element_blank(),
            panel.background = element_blank(),
            legend.position = c(.8,.7)) 
    

    example4

    使填充物的颜色在不同的灰色色调中变化

    ggplot(p_well, aes(x=Var2, y=Freq, fill=Var1)) + 
      geom_bar_pattern(position="stack",stat="identity",
                       mapping=aes(pattern=Var1)) +
      xlab("Positions") +
      ylab("%") +
      theme_bw() +
      scale_fill_grey(start = .9, end = 0) +
      scale_y_continuous(expand = c(0,0)) +
      theme(legend.title=element_blank(),
            axis.line = element_line(colour = "black"),
            panel.grid.major = element_blank(),
            panel.grid.minor = element_blank(),
            panel.border = element_blank(),
            panel.background = element_blank(),
            legend.position = c(.8,.7)) 
    

    example5