如何使用百分比值按年份制作堆叠条形图?GGR图
这是我的数据如何使用百分比值按年份制作堆叠条形图?GGR图,r,ggplot2,R,Ggplot2,这是我的数据 print(data) A tibble: 12 x 3 `Age Group` Percent Year <chr> <dbl> <dbl> 1 Adults 0.78 2017 2 Youth 0.05 2017 3 Children 0.17 2017 4 Adults 0.76 2018 5 Youth 0.0
print(data)
A tibble: 12 x 3
`Age Group` Percent Year
<chr> <dbl> <dbl>
1 Adults 0.78 2017
2 Youth 0.05 2017
3 Children 0.17 2017
4 Adults 0.76 2018
5 Youth 0.07 2018
6 Children 0.17 2018
7 Adults 0.77 2019
8 Youth 0.05 2019
9 Children 0.18 2019
10 Adults 0.06 2020
11 Youth 0.76 2020
12 Children 0.17 2020
打印(数据)
一个tibble:12x3
`年龄组`年百分比
2017年1月0.78日
2青年0.05 2017
3名儿童2017年0月17日
4成人0.76 2018
5青年0.07 2018
6名儿童0.17 2018
7成人0.77 2019
8青年0.05 2019
9名儿童0.18 2019
10名成年人0.06 2020
11青年0.76 2020
12儿童0.17 2020
我的目标是为每年制作一个堆叠条形图,但我希望标签上的值采用百分比格式(即70%)
我需要做什么 我们可以使用
scales::percent
library(ggplot2)
ggplot(df1, aes(Year, y = Percent, fill = `Age Group`)) +
geom_col() +
geom_text(aes(label = scales::percent(Percent)),
position = position_stack(vjust = 0.5)) +
scale_y_continuous(labels = scales::percent_format())
数据
df1您是否需要ggplot(df1,aes(x=Year,y=Percent,fill=
年龄组)+geom\u col()+scale\u y\u continuous(labels=scales::Percent\u format())
df1 <- structure(list(`Age Group` = c("Adults", "Youth", "Children",
"Adults", "Youth", "Children", "Adults", "Youth", "Children",
"Adults", "Youth", "Children"), Percent = c(0.78, 0.05, 0.17,
0.76, 0.07, 0.17, 0.77, 0.05, 0.18, 0.06, 0.76, 0.17), Year = c(2017L,
2017L, 2017L, 2018L, 2018L, 2018L, 2019L, 2019L, 2019L, 2020L,
2020L, 2020L)), class = "data.frame", row.names = c("1", "2",
"3", "4", "5", "6", "7", "8", "9", "10", "11", "12"))