を中心にされていない私は、次のデータでグラフを作っていますggplot2ラベル正しく
library(dplyr)
library(forcats)
library(ggplot2)
library(scales)
mydf <- tibble(type = c(rep(c(rep("TypeA", 4), rep("TypeB", 4)), 2)),
Ratings = rep(c("Top", "Middle", "Low", "NA"), 4),
Prop = c(0.62, 0.15, 0.15, 0, 0.32, 0.16, 0.47, 0, 0.38, 0.31, 0.31, 0, 0.16, 0.16, 0.63, 0.05),
Question = c(rep("Question1", 8), rep("Question2", 8)))
mydf$Ratings <- factor(mydf$Ratings) %>% fct_inorder() %>% fct_rev()
そしてここでは、私のコードは、チャートを作るためにです:
mydf %>% filter(Prop > 0) %>% ggplot(aes(x = Question, y = Prop, fill = Ratings)) +
geom_bar(position = "fill", stat = "identity") +
geom_text(aes(label = percent(round(Prop,2))), position = position_stack(vjust = 0.5)) +
facet_grid(~ type) + scale_y_continuous(labels = percent) +
guides(fill = guide_legend(reverse = TRUE))
それは以下のチャートを生成します。私は具体的にはposition = position_stack(vjust = 0.5)
を使用して、バーの中央にラベルの中央を配置しました。明らかに、質問1のラベルは正しく表示されません。これはバグですか?私のデータを正しく設定していませんか?
をあなたは、高さを定義する別の変数を作成する必要がありますラベルの詳細については、[here](https://www.getdatajoy.com/examples/r-plots/stacked-bar-graph)を参照してください。 – figurine
[この回答](http://stackoverflow.com/a/6645506/5221626)では、これはggplot2 v2.2では必要なくなりました。 – Phil