簡體   English   中英

使用ggplot將數據頻率添加到條形圖

[英]adding frequency of data to bar plot using ggplot

這是我使用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")) 

我需要將每個數據的頻率添加到每個條形的頂部。 有什么建議嗎?

嘗試添加此:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM