我用下面的代码创建了这个图表:
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
顶部对齐?
ggplot(tabulatedLikert2,aes(x = score,y = Freq))+ geom_bar(stat =“identity”,fill =“gray70”)+ geom_text(aes(label = score),size = 4)+ facet_grid(age〜。id,scales =“free_x”)
- bdemarest