Created
August 7, 2016 17:48
-
-
Save shuozhang1985/9a51675f3ebb95a7ffb71d6799ffce83 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
device_data=read.csv('./data/phonedata.csv', header=T, stringsAsFactors = F) | |
device_map=dplyr::filter(device_data, !is.na(longitude), !is.na(latitude), !is.na(group))%>% | |
dplyr::filter(longitude>=73, longitude<136, latitude>=4, latitude<54) | |
N=nrow(device_map) | |
agegroup=rep(0, N) | |
for (i in 1:N){ | |
if (device_map$age[i]<=26){ | |
agegroup[i]='post-90s' | |
} | |
else if (device_map$age[i]>26&device_map$age[i]<=36){ | |
agegroup[i]='post-80s' | |
} | |
else if (device_map$age[i]>36&device_map$age[i]<=46){ | |
agegroup[i]='post-70s' | |
} | |
else if (device_map$age[i]>46&device_map$age[i]<=56){ | |
agegroup[i]='post-60s' | |
} | |
else if (device_map$age[i]>56){ | |
agegroup[i]='post-50s' | |
} | |
} | |
device_map=mutate(device_map, agegroup=agegroup) | |
agedis=device_map%>% | |
dplyr::filter(phone_brand_English %in% c('Xiaomi', 'Huawei', 'OPPO', | |
'vivo', 'samsung')) %>% | |
dplyr::group_by(agegroup, phone_brand_English)%>% | |
summarise(n=n())%>% | |
mutate(percent=n/sum(n)) | |
ageplot=ggplot(data=agedis, aes(x=agegroup, y=percent, | |
fill=phone_brand_English))+ | |
geom_bar(stat = 'identity')+ | |
xlab('age group')+ | |
ylab('percent of number')+ | |
ggtitle('User Age Group Distribution of Top 5 Phone Brands')+ | |
theme_bw() | |
ggplotly(ageplot) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment