【问题标题】:Label bar plot with geom_text in ggplot [duplicate]在ggplot中使用geom_text标记条形图[重复]
【发布时间】:2013-08-29 18:33:57
【问题描述】:

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

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 的相应条形上方?

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    根据linked question 中的答案,将position = position_dodge(width=0.9) 添加到geom_text 调用中排列值:

    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), 
                position = position_dodge(width=0.9),
                size=4) +
      facet_grid(age ~ .)
    

    但是,我还想指出其他一些事情。你不应该在aes() 调用中使用meltedLikert$score;您应该只引用作为data 传递的数据框中的内容。此外,meltedLikert 已经是 data.frame,因此没有必要在其上调用 data.frame()(尽管不会造成任何伤害)。

    真正的改进在于您如何开始创建制表。请考虑一下:

    tabulatedLikert <- ldply(likert[-1], function(sc) {
      as.data.frame(table(age = likert$age, score = sc))
    })
    ggplot(tabulatedLikert, aes(x=.id, y=Freq, fill=score)) +
      geom_bar(position="dodge", stat="identity") +
      geom_text(aes(label=score), position=position_dodge(width=0.9), size=4) +
      facet_grid(age ~ .)
    

    您可以通过在原始数据中修复条形来修复它们的顺序:

    likert2 <- mutate(likert,
                      score1 = factor(score1, levels=c("unlikely", "likely", "very likely")),
                      score2 = factor(score2, levels=c("disagree", "neutral", "agree")),
                      score3 = factor(score3, levels=c("no", "maybe", "yes")))
    tabulatedLikert2 <- ldply(likert2[-1], function(sc) {
      as.data.frame(table(age = likert2$age, score = sc))
    })
    ggplot(tabulatedLikert2, aes(x=.id, y=Freq, fill=score)) +
      geom_bar(position="dodge", stat="identity") +
      geom_text(aes(label=score), position=position_dodge(width=0.9), size=4) +
      facet_grid(age ~ .)
    

    当然,在这一点上,颜色实际上并没有添加任何东西,因为所有内容都直接标记在图表上,所以我将完全摆脱它们。

    ggplot(tabulatedLikert2, aes(x=.id, y=Freq, group=score)) +
      geom_bar(position="dodge", stat="identity", fill="gray70") +
      geom_text(aes(label=score), position=position_dodge(width=0.9), size=4) +
      facet_grid(age ~ .)
    

    【讨论】:

    • 感谢您的深入回答 - 我在那里学到了很多东西
    • 另一种可能性: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")
    猜你喜欢
    • 1970-01-01
    • 2013-03-03
    • 1970-01-01
    • 1970-01-01
    • 2017-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多