以下の画像を作成するためにggplot2を使用しました。テーマ(外国、経済など)ごとにデータをfacet_wrapの個々のファセットとして表示する方法を検討したいと思います。ラベル(73(1.02%)、67(.93%)など)をバーに戻すことができなくなることを除いて、必要なものすべてを再構築できます。ラベルをプロットするにはどうすればよいですか?
これが私のコードです:
dat3 <- structure(list(grouping.var = structure(c(1L, 3L, 1L, 3L, 1L,
3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L), class = "factor", .Label = c("BIDEN",
"RADDATZ", "RYAN")), word.count = c(7240, 7153, 7240, 7153, 7240,
7153, 7240, 7153, 7240, 7153, 7240, 7153, 7240, 7153, 7240, 7153
), variable = structure(c(4L, 4L, 2L, 2L, 1L, 1L, 5L, 5L, 3L,
3L, 7L, 7L, 6L, 6L, 8L, 8L), .Label = c("foreign", "economic",
"rebublican_people", "health", "democratic_people", "romney",
"obama_any_name", "obama_by_name"), class = "factor"), value = c(29,
36, 27, 47, 67, 73, 13, 28, 45, 24, 13, 20, 26, 21, 1, 14), labs = c("29(.0040)",
"36(.0050)", "27(.0037)", "47(.0066)", "67(.0093)", "73(.0102)",
"13(.0018)", "28(.0039)", "45(.0062)", "24(.0034)", "13(.0018)",
"20(.0028)", "26(.0036)", "21(.0029)", "1(.0001)", "14(.0020)"
), loc = c(22.5, 29.5, 20.5, 40.5, 60.5, 65.75, 6.5, 21.5, 38.5,
17.5, 6.5, 13.5, 19.5, 14.5, 7, 7.5), cols = c("black", "white",
"white", "white", "white", "white", "white", "white", "white",
"white", "white", "white", "white", "white", "white", "white"
)), .Names = c("grouping.var", "word.count", "variable", "value",
"labs", "loc", "cols"), row.names = c(NA, -16L), class = "data.frame")
ggplot(dat3, aes(x=variable, y=value, fill=grouping.var)) +
geom_bar(position="dodge", stat="identity") +
coord_flip() + theme_bw() +
theme(legend.position="top", legend.background = element_rect(color="white"),
panel.grid.major=element_blank(),panel.grid.minor=element_blank(),
legend.title=element_blank(), axis.ticks.y = element_blank(),
axis.text.y = element_blank()) +
ylab("") + xlab("") + facet_wrap(~variable, ncol=2, scales = "free_x") +
scale_fill_manual(values=c("#0000FF", "#FF0000"),
guide = guide_legend(reverse=TRUE))
geom_text(aes(label = labs, y = loc, x = variable),
size = 5, position = position_dodge(width=0.9), color=dat3$cols)