參考學習視頻:http://www.tianshansoft.com/python
數據集:https://pan.baidu.com/s/1o7BFzFO程序員
數據中包含228個變量,提取其中的一些較有價值的變量進行描述性分析編程
survey <-read.csv(stringsAsFactors = F,file = 'F:\\R/數據科學社區調查/multipleChoiceResponses.csv',header=T,sep=',') class(survey) table(survey$Country) #統計每一個國家參與人數
查看國家時,發現國家中中國被切分紅共和國,民國,臺灣,此處自行統一爲中國網絡
#將大陸,中華民國,臺灣統一爲中國 survey$Country <- ifelse(survey$Country=="People 's Republic of China" | survey$Country=='Republic of China' | survey$Country=='Taiwan' ,'China',survey$Country)
#將數據按國家分類,並求年齡的中位數 Country_age <- survey %>% group_by(Country) %>% summarise(Age_median=median(Age,na.rm = T)) %>% arrange(Age_median) head(Country_age)
#繪圖,探索數據科學從業者年齡中位數最小的十個國家 p1 <-ggplot(data = head(Country_age,10),aes(reorder(Country,Age_median),Age_median,fill=Country))+ geom_bar(stat='identity')+coord_flip()+ labs(x='年齡',y='國家',title='探索不一樣國家數據從業者的平均年齡')+ geom_text(aes(label=round(Age_median,0)),hjust=1.5)+ theme(legend.position = 'none',plot.title=element_text(hjust = 0.3))
#繪圖,探索數據科學從業者年齡中位數最大的十個國家 p2 <- ggplot(data = tail(Country_age,10),aes(reorder(Country,Age_median),Age_median,fill=Country))+ geom_bar(stat='identity')+coord_flip()+ labs(x='年齡',y='國家')+ geom_text(aes(label=round(Age_median,0)),hjust=1.5)+ theme(legend.position = 'none')
#合併兩張圖 library(Rmisc) multiplot(p1,p2,cols = 1)
首先對數據整理,得出受訪人數最多的前十個職位,且降序排列機器學習
#數據科學從業者的職位分類 jobtitle<-table(survey$CurrentJobTitleSelect)%>% #統計頻數 as.data.frame()%>% #轉化爲數據框 arrange(desc(Freq)) #按頻數倒序排列(大在前) jobtitle <- jobtitle[-1,] #人數最多的一行爲空值,即職業一欄無填寫
接下來進行繪圖,將數據可視化編程語言
ggplot(data=head(jobtitle,10),aes(x=reorder(Var1,Freq),Freq,fill=Var1))+ #選取受訪人數最多的前十個職業 geom_bar(stat = 'identity')+ labs(x='職業',y='人數',title='受訪人數最多的十個職位')+ coord_flip()+ #翻轉座標軸 geom_text(aes(label=Freq),hjust=1.5)+ #添加數據標籤 theme(legend.position = 'none',plot.title = element_text(hjust = 0.2)) #去除圖例,調整標題位置
從圖中可看出數據科學家參加問卷調查的人數最多,達2433人。排名第十的爲程序員,只有462人ide
diff_nation <- survey[which(survey$Country=='China'),] #提取出國家爲中國的調查者信息 diff_nation1 <- survey[which(survey$Country=='United States'),] #提取出國家爲美國的調查者信息 china_jobtitle <- table(diff_nation$CurrentJobTitleSelect)%>%as.data.frame()%>%arrange(desc(Freq)) #探索在中國的受訪人數較多職位 usa_jobtitle <- table(diff_nation1$CurrentJobTitleSelect)%>%as.data.frame()%>%arrange(desc(Freq)) #探索在美國的受訪人數較多職位
圖中可看到,中國的受訪者中,有361人沒有填寫當前職位這一欄。美國也有1072人。在繪圖的過程當中,須要將這些空值篩選掉函數
p3<-ggplot(china_jobtitle[c(2:11),],aes(reorder(Var1,Freq),Freq,fill=Var1))+ #數據集中國前十位熱門職業 geom_bar(stat = 'identity')+ labs(x='職業',y='受訪人數(中國)',title='中美兩國受訪者的當前職位對比')+ coord_flip()+ #翻轉座標軸 geom_text(aes(label=Freq),hjust=1)+ theme(legend.position = 'none',plot.title = element_text(size = 15,face = 'bold.italic')) #去除圖例,設置標題大小,字體 p4<-ggplot(usa_jobtitle[c(2:11),],aes(reorder(Var1,Freq),Freq,fill=Var1))+ #數據集中國前十位熱門職業 geom_bar(stat = 'identity')+ labs(x='職業',y='受訪人數(美國)')+ coord_flip()+ #翻轉座標軸 geom_text(aes(label=Freq),hjust=1)+ theme(legend.position = 'none') #合併兩圖 multiplot(p3,p4)
study_tool <- table(survey$MLToolNextYearSelect) %>% as.data.frame()%>% arrange(desc(Freq))
繪圖過程與前面大同小異,因此可將繪圖函數封裝,代入變量便可工具
##############============封裝繪圖函數========##################### fun1 <-function(data1,xlab1,ylab1,xname1,yname1,titlename1){ ggplot(data = data1,aes(x=xlab1,y=ylab1,fill=xlab1))+ geom_bar(stat = 'identity')+ labs(x=xname1,y=yname1,title=titlename1)+ coord_flip()+ #翻轉座標軸 geom_text(aes(label=ylab1),hjust=1)+ #數據標籤 theme(legend.position = 'none',plot.title = element_text(size = 15,face = 'bold.italic')) #去除圖例,設置標題大小,字體 } ########################################################################
代入變量學習
#function(data,xlab1,ylab1,var1,xname1,yname1,titlename1) data <- study_tool[c(2:11),] xname1 <- '明年將學習的學習工具' yname1 <- '人數' titlename1 <- '受訪者明年將學習的學習工具調查' fun1(data,reorder(data$Var1,data$Freq),data$Freq,xname1,yname1,titlename1)
china_studytool <- survey %>% filter(survey$MLToolNextYearSelect !=''&Country=='China') %>% group_by(MLToolNextYearSelect) %>% summarise(count=n())%>% #n() 彙總 arrange(desc(count))
以上爲提取中國受訪者明年將學習的學習工具數據。
圖中可見,中國數據科學從業者明年即將學習的學習工具熱度較高的爲Python,TensorFlow,Spark,jupyter,R。而美國爲TensorFlow,python,sparkR,其餘,比較符合國際趨勢。