簡體   English   中英

在 R 中增加堆積條形圖中條形之間的空間

[英]Increase space between bars in a stacked bar chart in R

我有這樣的數據:

data_in <- read_table2("Programcomponent    name    descending_order    descending_order2   group   value
A   Biology 1   1   Group1  1445
B   Science 2   2   Group1  832
C   Sociology   3   4   Group1  661
C-2 Sociology2  4   4   Group1  342
D   Arts    5   6   Group1  221
B-2 Science2    8   3   Group1  150
A   Biology 1   1   Group2  901
B   Science 2   2   Group2  580
C   Sociology   3   4   Group2  442
C-2 Sociology2  4   4   Group2  293
D   Arts    5   6   Group2  163
B-2 Science2    8   3   Group2  142
A   Biology 1   1   Group3  321
B   Science 2   2   Group3  170
C   Sociology   3   4   Group3  251
C-2 Sociology2  4   4   Group3  107
D   Arts    5   6   Group3  93
B-2 Science2    8   3   Group3  108
")

我正在嘗試創建這樣的圖表。 除了我希望將 B 列和 B-2 列組合在一起,並將 C、C-2 列組合在一起。 我如何在條之間添加空格,使 A 是一組,B,B2 是另一組,C,C2 是另一組,D 是另一組?

如果可能,尋找一個沒有刻面的選項。

ggplot(data_in,
       aes(fill = group, y = value, #alpha = factor(alphatotal),
           x = reorder(`Programcomponent`, descending_order2), label = value)) + 
  # Specify bar plot
  geom_bar(position = "identity", stat = "identity") + 
  geom_text_repel(aes(label=value),vjust=0,direction = "y")

在此處輸入圖像描述

使用facet_grid

data_in$grp = substring(data_in$Programcomponent, 0, 1)
graphics.off()
ggplot(data_in, aes(x = Programcomponent, y = value, fill = group)) +
    geom_bar(position = "identity", stat = "identity") + 
    facet_grid(~ grp, space = "free", scales = "free")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM