如何在条形图中显示计数?
How to display counts in barplot?
所以我有这个数据框
>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" ...
我写了这个条形图代码
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))
现在,我想在每个条上添加计数 (Age_vector),我想将间隔更改为文本,例如“小于 18”而不是“ [10,18[
我试着在 ggplot 上做,但我做不到。
基础图形:
tb <- table(mtcars$cyl)
tb
# 4 6 8
# 11 7 14
bp <- barplot(tb)
text(bp[,1], tb-1, tb)
所以我有这个数据框
>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" ...
我写了这个条形图代码
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))
现在,我想在每个条上添加计数 (Age_vector),我想将间隔更改为文本,例如“小于 18”而不是“ [10,18[
我试着在 ggplot 上做,但我做不到。
基础图形:
tb <- table(mtcars$cyl)
tb
# 4 6 8
# 11 7 14
bp <- barplot(tb)
text(bp[,1], tb-1, tb)