简体   繁体   English

如何在条形图中显示计数?

[英]How to display counts in barplot?

So I have this dataframe所以我有这个 dataframe

>Survey[1:4]
# A tibble: 268 x 4
   Horodateur          Gender Age     Time      
   <dttm>              <chr>  <chr>   <chr>     
 1 2021-04-23 09:59:16 Male   [18,24[ 1-5 hours 
 2 2021-04-23 10:11:35 Female [10,18[ 1-5 hours 
 3 2021-04-23 10:18:24 Male   [18,24[ >10 hours 
 4 2021-04-23 10:42:28 Female [18,24[ 5-10 hours
 5 2021-04-23 10:42:37 Female [18,24[ 5-10 hours
 6 2021-04-23 10:45:35 Female [24,34[ 1-5 hours 
 7 2021-04-23 10:48:09 Male   [18,24[ 5-10 hours
 8 2021-04-23 10:49:56 Male   [18,24[ 5-10 hours
 9 2021-04-23 10:50:39 Male   [24,34[ 0 hours   
10 2021-04-23 10:51:36 Male   [18,24[ 5-10 hours
# ... with 258 more rows
> str(Survey[1:4])
tibble [268 x 4] (S3: tbl_df/tbl/data.frame)
 $ Horodateur: POSIXct[1:268], format: "2021-04-23 09:59:16" "2021-04-23 10:11:35" ...
 $ Gender    : chr [1:268] "Male" "Female" "Male" "Female" ...
 $ Age       : chr [1:268] "[18,24[" "[10,18[" "[18,24[" "[18,24[" ...
 $ Time      : chr [1:268] "1-5 hours" "1-5 hours" ">10 hours" "5-10 hours" ...

I wrote this barplot code我写了这个条形图代码

A=sum(Survey$Age =="[10,18[")
B=sum(Survey$Age =="[18,24[")
C=sum(Survey$Age =="[24,34[")
D=sum(Survey$Age =="[34,65[")
Age_vector=c(A,B,C,D)
colors=c("#555b6e","#89b0ae","#bee3db","#ffd6ba")
b <- barplot(table(Survey$Age),col=colors,ylim=c(0,250))

在此处输入图像描述

Now, I want to add the number of counts ( Age_vector ) on each bar and I'd love to change the intervals to texts like " Less than 18" instead of "[10,18[现在,我想在每个条上添加计数( Age_vector ),我想将间隔更改为“小于 18”而不是“[10,18[

I tried to do it on ggplot but I couldn't.我试图在 ggplot 上做到这一点,但我做不到。

Base graphics:基础图形:

tb <- table(mtcars$cyl)
tb
#  4  6  8 
# 11  7 14 

bp <- barplot(tb)
text(bp[,1], tb-1, tb)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM