2017-12-22 3 views
2

ggplot2でグラフを作成しようとしています。このページのグラフに似たパーセンテージの積み上げ棒グラフを作成しようとしていますが、棒グラフにパーセントラベルを追加しようとしています:How to draw stacked bars in ggplot2 that show percentages based on group?積み重ね棒グラフにパーセントラベルを追加するggplot2

私が試してみたパーセントラベルはコードのようなものを使用し

geom_text(AES(ラベル=ラベル)、位置= position_stack(vjust = 0.5)、 サイズ= 2)

が、それは私のために働いていません。

私のデータは次のようになります。 graph1

コード:割合のない

County Group Plan1 Plan2 Plan3 Plan4 Plan5 Total 
County1 Group1 2019 597 513 5342 3220 11691 
County2 Group1 521 182 130 1771 731 3335 
County3 Group1 592 180 126 2448 1044 4390 
County4 Group1 630 266 284 2298 937 4415 
County5 Group1 708 258 171 2640 1404 5181 
County6 Group1 443 159 71 1580 528 2781 
County7 Group1 492 187 157 1823 900 3559 
County8 Group1 261 101 84 1418 357 2221 

私のグラフは、このようになります上記のgeom_text()のコードを使用した後

melt(df[df$Group=="Group1",],measure.vars = c("Plan1","Plan2","Plan3","Plan4", "Plan5"),variable.name = "Counties",value.name = "value") %>% 
ggplot(aes(x=County,y=value,fill=Counties))+ 
    geom_bar(stat = "identity",position="fill", color="black", width=0.9) + 
    labs(y="Percent", fill="Plan Type") + ylab("Percentage") + coord_flip() + scale_y_continuous(labels=scales::percent) 

、それは変わりますこの混乱に: graph2

コード:

melt(df[df$Group=="Group1",],measure.vars = c("Plan1","Plan2","Plan3","Plan4", "Plan5"),variable.name = "Counties",value.name = "value") %>% 
ggplot(aes(x=County,y=value,fill=Counties))+ 
    geom_bar(stat = "identity",position="fill", color="black", width=0.9) + 
    labs(y="Percent", fill="Plan Type") + ylab("Percentage") + coord_flip() + scale_y_continuous(labels=scales::percent)+ 
geom_text(aes(label=paste0(round(value/100),"%")), position=position_stack(vjust=0.5)) 

任意の提案ですか?どんなアドバイス/ガイダンスも大歓迎です!ありがとうございました!!

+0

あなたはバーの 'のDF [7、3]を行う前に、割合を計算することができ、3:7]) '私はあなたがもっとグループを持っていると推測しているので、あなたは"グループ " – rawr

答えて

1

ラベルが%ではなく生の値であるため、あなたのアプローチは機能しませんでした。あなたは、あなた自身で統計を実行する必要があります。

df <- read.table(text="County Group Plan1 Plan2 Plan3 Plan4 Plan5 Total 
County1 Group1 2019 597 513 5342 3220 11691 
       County2 Group1 521 182 130 1771 731 3335 
       County3 Group1 592 180 126 2448 1044 4390 
       County4 Group1 630 266 284 2298 937 4415 
       County5 Group1 708 258 171 2640 1404 5181 
       County6 Group1 443 159 71 1580 528 2781 
       County7 Group1 492 187 157 1823 900 3559 
       County8 Group1 261 101 84 1418 357 2221", header = TRUE) 

library(tidyverse) 
df %>% 
    filter(Group == "Group1") %>% 
    select(-Total) %>% 
    gather(key = `Plan Type`, value = value, -County, -Group) %>% 
    group_by(County, Group) %>% 
    mutate(Percentage = value/sum(value)) %>% 
    ggplot(aes(x = County, y = Percentage, fill = `Plan Type`, label = paste0(round(Percentage*100), "%"))) + 
    geom_col(position = position_stack(), color = "black") + 
    geom_text(position = position_stack(vjust = .5)) + 
    coord_flip() + 
    scale_y_continuous(labels = scales::percent_format()) 

編集:

上記のコードは、より多くの計画のためだけでなく、それ以上のグループのためにも動作しますが、プロットは、そのために考慮されています。 [3:7] dfを -facet_wrapを追加します:

df %>% 
     filter(Group == "Group1") %>% 
     select(-Total) %>% 
     gather(key = `Plan Type`, value = value, -County, -Group) %>% 
     group_by(County, Group) %>% 
     mutate(Percentage = value/sum(value)) %>% 
     ggplot(aes(x = County, y = Percentage, fill = `Plan Type`, label = paste0(round(Percentage*100), "%"))) + 
     geom_col(position = position_stack(), color = "black") + 
     geom_text(position = position_stack(vjust = .5)) + 
     coord_flip() + 
     scale_y_continuous(labels = scales::percent_format()) + 
     facet_wrap(~Group) 
関連する問題