我用以下代码创建了这个图:
library(ggplot2); library(reshape2); library(plyr)
likert <- data.frame(age = c(rep("young", 5), rep("middle", 5), rep("old", 5)),
score1 = c(rep("unlikely", 1), rep("likely", 1), rep("very likely", 13)),
score2 = c(rep("disagree", 6), rep("neutral", 4), rep("agree", 5)),
score3 = c(rep("no", 5), rep("maybe", 7), rep("yes", 3)))
meltedLikert <- melt(dlply(likert, .(age), function(x) llply(x, table)))
names(meltedLikert) <- c("score", "count", "variable", "age")
ggplot(meltedLikert[meltedLikert$variable != "age",], aes(variable, count, fill=score)) +
geom_bar(position="dodge", stat="identity") +
geom_text(data=data.frame(meltedLikert), aes(variable, count, group=score, label=meltedLikert$score), size=4) +
facet_grid(age ~ .)
如何标记位置文本,以便每个标签score
位于每个条顶部的相应条variable
上?