【问题标题】:adding frequency of data to bar plot using ggplot使用 ggplot 将数据频率添加到条形图
【发布时间】:2017-10-24 19:43:00
【问题描述】:

这是我使用 ggplot 创建条形图的代码。

library(tidyverse)
    dat= data_frame(rating=1:5, No=c(167,82, 132, 182, 200), Yes=c(28, 22, 20, 27, 29))
    dat = dat %>%
      gather(key=color, value=value, -rating)
    d = ggplot(data=dat, aes(x=rating, y=value, fill=color)) + 
      geom_bar(stat='identity', position='dodge')+ labs(fill = "headache", y = "frequency") 

    d = d + scale_fill_manual(values=c("blue", "yellow"))

    d = d  + theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
                                 panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) 

我需要将每个数据的频率添加到每个条的顶部。有什么建议吗?

【问题讨论】:

标签: r ggplot2


【解决方案1】:

尝试添加这个:

d + geom_text(aes(label = value), 
              size = 3, 
              color = "black",
              position = position_dodge(width = 0.9),
              vjust = -2)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-27
    • 1970-01-01
    • 2021-11-01
    • 2020-12-02
    • 1970-01-01
    • 2017-10-23
    • 2018-05-09
    相关资源
    最近更新 更多