wageeh
wageeh

Reputation: 84

How to display counts in barplot?

So I have this 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))

enter image description here

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[

I tried to do it on ggplot but I couldn't.

Upvotes: 0

Views: 93

Answers (1)

r2evans
r2evans

Reputation: 160447

Base graphics:

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

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

enter image description here

Upvotes: 2

Related Questions