相关疑难解决方法(0)

使用ggplot中的geom_text标记条形图

我用以下代码创建了这个图:

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 ~ .)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

如何标记位置文本,以便每个标签score位于variable每个栏顶部的相应栏上?

r ggplot2

6
推荐指数
1
解决办法
3万
查看次数

标签 统计

ggplot2 ×1

r ×1