ggplot 分组堆积条形图更改刻度标签

时间:2021-04-14 14:25:02

标签: r ggplot2

我有一个要为其创建分组堆叠条形图的数据集。

df <- 
structure(list(value = structure(c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 
3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 
4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 
5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), .Label = c("1", 
"2", "3", "4", "5"), class = "factor"), variable = structure(c(1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 
4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 
7L, 7L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 
10L, 10L), .Label = c("Q1_1", "Q1_2", "Q1_3", "Q1_4", "Q1_5", 
"Q2_1", "Q2_2", "Q2_3", "Q2_4", "Q2_5"), class = "factor"), 
    Freq = c(75L, 214L, 759L, 1788L, 512L, 934L, 918L, 701L, 
    300L, 495L, 526L, 917L, 959L, 554L, 392L, 121L, 335L, 654L, 
    856L, 1382L, 178L, 327L, 577L, 673L, 1593L, 744L, 841L, 876L, 
    597L, 290L, 787L, 1151L, 777L, 382L, 251L, 656L, 1253L, 869L, 
    406L, 164L, 586L, 1150L, 731L, 407L, 474L, 245L, 529L, 699L, 
    842L, 1033L), sq = c("1", "1", "1", "1", "1", "2", "2", "2", 
    "2", "2", "3", "3", "3", "3", "3", "4", "4", "4", "4", "4", 
    "5", "5", "5", "5", "5", "1", "1", "1", "1", "1", "2", "2", 
    "2", "2", "2", "3", "3", "3", "3", "3", "4", "4", "4", "4", 
    "4", "5", "5", "5", "5", "5"), mq = c("Q1", "Q1", "Q1", 
    "Q1", "Q1", "Q1", "Q1", "Q1", "Q1", "Q1", "Q1", "Q1", 
    "Q1", "Q1", "Q1", "Q1", "Q1", "Q1", "Q1", "Q1", "Q1", 
    "Q1", "Q1", "Q1", "Q1", "Q2", "Q2", "Q2", "Q2", "Q2", 
    "Q2", "Q2", "Q2", "Q2", "Q2", "Q2", "Q2", "Q2", "Q2", 
    "Q2", "Q2", "Q2", "Q2", "Q2", "Q2", "Q2", "Q2", "Q2", 
    "Q2", "Q2"), tq = c("A1", 
    "A1", "A1", 
    "A1", "A1", 
    "A2", "A2", "A2", "A2", 
    "A2", "A3", "A3", 
    "A3", "A3", 
    "A3", "A4", "A4", 
    "A4", "A4", "A4", "A6", "A6", 
    "A6", "A6", "A6", 
    "A1", "A1", 
    "A1", "A1", 
    "A1", "A2", 
    "A2", "A2", "A2", "A2", 
    "A3", "A3", 
    "A3", "A3", 
    "A3", "A4", "A4", 
    "A4", "A4", "A4", "A5", "A5", 
    "A5", "A5", "A5"
    )), row.names = c(NA, -50L), class = "data.frame")

r$> head(df)                                                                    
  value variable Freq sq mq tq
1     1     Q1_1   75  1 Q1 A1
2     2     Q1_1  214  1 Q1 A1
3     3     Q1_1  759  1 Q1 A1
4     4     Q1_1 1788  1 Q1 A1
5     5     Q1_1  512  1 Q1 A1
6     1     Q1_2  934  2 Q1 A2

ggplot(df, aes(x=mq, y= Freq, fill= value)) + 
geom_bar(aes(fill=value) ,stat="identity") + facet_grid(.~sq)

给了我这个情节(这几乎是我想要的) plot output

现在我想让 x 轴刻度标签成为我的数据集的 tq 列中的任何内容。对于 mqsq 的每种组合,只有一个可能的 tq 值。

为清楚起见编辑:sq 应该确定“组”,对于每个 mq 应该有一个堆叠条(每组),并且标签应该是相应的唯一条目该组合的 tq 列。

2 个答案:

答案 0 :(得分:2)

一种方法是使用 tidyr::unite 组合列以生成 x 变量并使用 scales = "free_x" 从构面中删除空列:

library(tidyr)
df %>%
  unite(comb, c(sq,mq,tq), sep = "-",remove = FALSE) %>%
ggplot(aes(x=comb, y= Freq, fill= value)) + 
  geom_bar(aes(fill=value) ,stat="identity") +
  facet_grid(.~sq, scales = "free_x") 

enter image description here

从这里您可以使用自定义标签函数来去除 x 刻度中的无关信息:

library(stringr)
df %>%
  unite(comb, c(sq,mq,tq), sep = "-",remove = FALSE) %>%
ggplot(aes(x=comb, y= Freq, fill= value)) + 
  geom_bar(aes(fill=value) ,stat="identity") +
  facet_grid(.~sq, scales = "free_x") +
  scale_x_discrete(labels = function(x)str_remove(x,".*-")) + 
  labs(x = "Important X Variable")

enter image description here

答案 1 :(得分:2)

df <- df %>%  
  unite(mq_tq, c("mq", "tq"), remove=FALSE)


ggplot(df, aes(x=mq_tq, y= Freq, fill= value)) + 
  geom_bar(aes(fill=value) ,stat="identity") +
  facet_grid(.~sq, scales = "free_x")  
  scale_x_discrete( name="") 

enter image description here

相关问题