3. Percentage of users by age group

13821 단어 R
In this article, I check the percentage of users by age group.
※If you have a data, you don’t have to read contents1.

Contents



1. Creation of simulation data
2. Bar and pie charts

1. Creation of simulation data


> age <- rpois(1000, lambda=30)
> age <- aggregate(age, list(age), length)
> age10 <- age %>% filter(Group.1>9 & Group.1<20)
> age20 <- age %>% filter(Group.1>19 & Group.1<30)
> age30 <- age %>% filter(Group.1>29 & Group.1<40)
> age40 <- age %>% filter(Group.1>39 & Group.1<50)
> age50 <- age %>% filter(Group.1>49)
> Data <- c(sum(age10$x), sum(age20$x), sum(age30$x),
+             sum(age40$x), sum(age50$x))
> Data <- data.frame(c(10, 20, 30, 40, 50), Data)
> names(Data) <- c("age.classification", "number.of.users")
> Data
  age.classification number.of.users
1                 10              14
2                 20             436
3                 30             486
4                 40              64
5                 50               0

2. Bar and pie charts


> library(ggplot2)
> p1 <- ggplot(data=Data, aes(x=age.classification))+
+   geom_bar(aes(y=number.of.users), size=0.9, stat='identity')+
+   labs(title="Number of users by age")+
+   ylab("number of users")
> library(ggrepel)
> Data$per <- round(Data$number.of.users/sum(Data$number.of.users)*100,1)
> Data$per <- paste0(Data$age.classification, "\n",Data$per, "%")
> p2 <- Data %>%
+   ggplot(aes(x=0, y=number.of.users, fill=factor(number.of.users)))+
+   geom_col()+
+   coord_polar("y")+
+   geom_text_repel(aes(label = per), position = position_stack(vjust = 0.5))+
+   theme_void()+
+   theme(legend.position="none")
> gridExtra::grid.arrange(p1, p2)

좋은 웹페이지 즐겨찾기