繁体   English   中英

将基础 R 堆叠条形图 plot 迁移到 ggplot2 并使用黑白结构区分条形图而不是 Z672848E3CE522FF4

[英]Migrate base R stacked bar plot to ggplot2 and distinguish bar levels with B/W structures instead of colors

我有一张这样的桌子:

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 中绘制一个堆叠条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)

在此处输入图像描述

但是,我想将 plot 迁移到ggplot2 另外,我需要使用黑白结构而不是 colors 来区分条的不同级别 怎么可能呢?

我认为这可能是您正在寻找的

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

例子

如果你想从底部消除间隙

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

示例2

您也可以使用forcats::fct_rev()反转顺序,但您还需要将黑白 colors 从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))

示例3

向 plot 添加模式,您可以使用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)) 

例子4

使填充的颜色以不同的灰度变化

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

示例5

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM