3. Percentage of users by age group
13821 단어 R
※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)
Reference
이 문제에 관하여(3. Percentage of users by age group), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/Tatsuki-Oike/items/2079f87ccfba62827c97
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
> 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)
Reference
이 문제에 관하여(3. Percentage of users by age group), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/Tatsuki-Oike/items/2079f87ccfba62827c97
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
> 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)
Reference
이 문제에 관하여(3. Percentage of users by age group), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/Tatsuki-Oike/items/2079f87ccfba62827c97텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)