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"))
はどうもありがとうございました!
ボックスのラベルを中心におよそSO上のいくつかの質問があります。たとえば、[ここ](http://stackoverflow.com/a/34904604/496488)と[ここ](http://stackoverflow.com/a/6645506/496488)。 – eipi10
私は上記のコメントの "ボックス"ラベルではなく、 "バー"ラベルを意味します。 – eipi10
こんにちは@ eipi10、私は私の問題を解決しようとするときにそれらの投稿を見たが、それぞれのプロットは、私の美しさを持っていないし、エラーメッセージが生成されます。私のものは "全体の一部"なので、私はyに置くことはできません - これはgeom_textをまったく使用できないということですか? – Sarah