2017-05-15 3 views
1

ggplot2(とR)を初めて使用し、各ボックス内にそのブロックを構成する割合を示す塗りつぶし棒グラフを作成しようとしています。ここでgeom_textを使用した塗りつぶし棒グラフの中央のラベル

は、私はラベルを追加したいと考えているに私の現在の図の例である:

##ggplot figure 
library(gpplot2) 
library(scales) 

#specify order I want in plots 
ZIU$Affinity=factor(ZIU$Affinity, levels=c("High", "Het", "Low")) 
ZIU$Group=factor(ZIU$Group, levels=c("ZUM", "ZUF", "ZIM", "ZIF")) 

ggplot(ZIU, aes(x=Group))+ 
geom_bar(aes(fill=Affinity), position="fill", width=1, color="black")+ 
scale_y_continuous(labels=percent_format())+ 
scale_fill_manual("Affinity", values=c("High"="blue", "Het"="lightblue", "Low"="gray"))+ 
labs(x="Group", y="Percent Genotype within Group")+ 
ggtitle("Genotype Distribution", "by Group") 

I would like to add labels centered in each box with the percentage that box represents

私はこのコードを使用してラベルを追加しようとしましたが、それはエラーメッセージを生成し続けます"エラー:geom_textには、次の不足している美学が必要です。y"しかし、私のプロットには美的感がありません。これはgeom_textを使用できないことを意味しますか? (geom_text文の残りの部分は私が望むものを達成する場合にも、私は各ボックスに白いラベルを中心に、yの審美的な問題が解決された後かどうかわからないです。)

ggplot(ZIU, aes(x=Group)) + 
geom_bar(aes(fill=Affinity), position="fill", width=1, color="black")+ 
geom_text(aes(label=paste0(sprintf("%.0f", ZIU$Affinity),"%")), 
    position=position_fill(vjust=0.5), color="white")+ 
scale_y_continuous(labels=percent_format())+ 
scale_fill_manual("Affinity", values=c("High"="blue", "Het"="lightblue", "Low"="gray"))+ 
labs(x="Group", y="Percent Genotype within Group")+ 
ggtitle("Genotype Distribution", "by Group") 

また、誰がための提案を持っている場合認識されるNA値を排除します!私は

geom_bar(aes(fill=na.omit(Affinity)), position="fill", width=1, color="black") 

を試してみましたが、エラーになった「エラー:美学は、いずれかの長さ1またはデータと同じ(403)でなければなりません:記入し、xは」

dput(sample) 
structure(list(Group = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L), .Label = c("ZUM", "ZUF", "ZIM", "ZIF"), class = "factor"), 
StudyCode = c(1, 2, 3, 4, 5, 6, 20, 21, 22, 23, 143, 144, 
145, 191, 192, 193, 194, 195, 196, 197, 10, 24, 25, 26, 27, 
28, 71, 72, 73, 74, 274, 275, 276, 277, 278, 279, 280, 290, 
291, 292), Affinity = structure(c(3L, 2L, 1L, 2L, 3L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 3L, 2L, 3L, 1L, 1L, 1L, 3L, 
2L, 1L, 2L, 2L, 1L, 2L, 2L, 3L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 
3L, 2L, 2L, 2L), .Label = c("High", "Het", "Low"), class = "factor")), .Names = c("Group", 
"StudyCode", "Affinity"), row.names = c(NA, 40L), class = c("tbl_df", 
"tbl", "data.frame")) 

はどうもありがとうございました!

+1

ボックスのラベルを中心におよそSO上のいくつかの質問があります。たとえば、[ここ](http://stackoverflow.com/a/34904604/496488)と[ここ](http://stackoverflow.com/a/6645506/496488)。 – eipi10

+0

私は上記のコメントの "ボックス"ラベルではなく、 "バー"ラベルを意味します。 – eipi10

+0

こんにちは@ eipi10、私は私の問題を解決しようとするときにそれらの投稿を見たが、それぞれのプロットは、私の美しさを持っていないし、エラーメッセージが生成されます。私のものは "全体の一部"なので、私はyに置くことはできません - これはgeom_textをまったく使用できないということですか? – Sarah

答えて

1

リンクされた例は、ggplot内部でカウントするのではなく、データがあらかじめ集約されているため、審美的です。あなたのデータを用いて、類似のアプローチは、次のようになります。

library(scales) 
library(tidyverse) 

# Summarize data to get counts and percentages 
ZIU %>% group_by(Group, Affinity) %>% 
    tally %>% 
    mutate(percent=n/sum(n)) %>% # Pipe summarized data into ggplot 
    ggplot(aes(x=Group, y=percent, fill=Affinity)) + 
    geom_bar(stat="identity", width=1, color="black") + 
    geom_text(aes(label=paste0(sprintf("%1.1f", percent*100),"%")), 
      position=position_stack(vjust=0.5), colour="white") + 
    scale_y_continuous(labels=percent_format()) + 
    scale_fill_manual("Affinity", values=c("High"="blue", "Het"="lightblue", "Low"="gray")) + 
    labs(x="Group", y="Percent Genotype within Group") + 
    ggtitle("Genotype Distribution", "by Group") 

enter image description here

別のオプションは、相対値がより明確になるかもしれないラインプロットを、使用することです。 Groupの値が自然なシーケンスではないと仮定すると、Groupの値によるAffinityの値を区別するためのガイドとしてその行が存在します。

ZIU %>% group_by(Group, Affinity) %>% 
    tally %>% 
    mutate(percent=n/sum(n)) %>% # Pipe summarized data into ggplot 
    ggplot(aes(x=Group, y=percent, colour=Affinity, group=Affinity)) + 
    geom_line(alpha=0.4) + 
    geom_text(aes(label=paste0(sprintf("%1.1f", percent*100),"%")), show.legend=FALSE) + 
    scale_y_continuous(labels=percent_format(), limits=c(0,1)) + 
    labs(x="Group", y="Percent Genotype within Group") + 
    ggtitle("Genotype Distribution", "by Group") + 
    guides(colour=guide_legend(override.aes=list(alpha=1, size=1))) + 
    theme_classic() 

enter image description here

+0

これは素晴らしいです!そんなにeipi10ありがとう! :) – Sarah

関連する問題