標籤:

信息圖形化

一、折線圖

month <- c("7月","8月","9月","10月","11月","12月")nmonth <- factor(month,order=TRUE,levels=c("7月","8月","9月","10月","11月","12月"))nmoney <- c(2.0,2.1,2.2,2.1,2.3,2.4)nmoneydata <- data.frame(month,money)n##折線圖nlibrary(ggplot2)nggplot(data=moneydata,aes(x=month,y=money,group=1))+geom_line()n

###添加點nggplot(data=moneydata,aes(x=month,y=money,group=1))+geom_line()+ggtitle("The company profit")+geom_point(size=4, shape=20)n

折線圖能夠很好的體現數據趨勢。

二、餅圖

name <- c("體育","策略","動作","射擊","其他")nname <- factor(name,order=TRUE,levels=c("其他","射擊","動作","策略","體育"))nsale <- c(27500,11500,6000,3500,1500)nsaledata <- data.frame(name,sale)nlabel = as.vector(saledata$name) nlabel = paste(label, "(",round(saledata$sale/sum(saledata$sale)*100,1),"%)",sep="") np = ggplot(saledata, aes(x = "", y =sale, fill = name)) + ngeom_bar(stat = "identity",position = stack, width = 1) + ## width >= 1 時中心的雜點將消失n coord_polar(theta = "y")+labs(x = "", y = "", title = "")+ ## 將標籤設為空ntheme(axis.ticks = element_blank())+ ## 把左上角多出來的「小鬍子」去掉n theme(legend.title = element_blank(), legend.position = "top")+ ## 將圖例標題設為空,並把圖例放在上方n scale_fill_discrete(breaks = saledata$name, labels = label)+ ## 將原來的圖例標籤換成現在的labeln theme(axis.text.x = element_blank())+ ## 白色的外框即是原柱狀圖的X軸,把X軸的刻度文字去掉即可n geom_text(aes(y = saledata$sale/2 + c(0, cumsum(saledata$sale)[-length(saledata$sale)]), x = sum(saledata$sale)/45000, label = label), size = 5) ## 在圖中加上百分比:x 調節標籤到圓心的距離, y 調節標籤的左右位置n

餅圖可以將數據劃分為互有明顯區別的幾組,但是一般不要求使用,因為如果在餅圖中沒有註明每塊扇形的大小,不能直觀看出各分類的佔比,所以一般不採用餅圖做分析。

三、環形圖

name <- c("體育","策略","動作","射擊","其他")nname <- factor(name,order=TRUE,levels=c("其他","射擊","動作","策略","體育"))nsale <- c(27500,11500,6000,3500,1500)nsaledata <- data.frame(name,sale)nlabel = as.vector(saledata$name) nlabel = paste(label, "(",round(saledata$sale/sum(saledata$sale)*100,1),"%)",sep="") np = ggplot(saledata, aes(x = "", y =sale, fill = name)) + ngeom_bar(stat = "identity", width = 0.2) + ## 當width < 1 時餅圖將變成餅環 n coord_polar(theta = "y")+labs(x = "", y = "", title = "")+ ## 將標籤設為空ntheme(axis.ticks = element_blank())+ ## 把左上角多出來的「小鬍子」去掉n theme(legend.title = element_blank(), legend.position = "top")+ ## 將圖例標題設為空,並把圖例放在上方n scale_fill_discrete(breaks = saledata$name, labels = label)+ ## 將原來的圖例標籤換成現在的labeln theme(axis.text.x = element_blank())+ ## 白色的外框即是原柱狀圖的X軸,把X軸的刻度文字去掉即可n geom_text(aes(y = saledata$sale/2 + c(0, cumsum(saledata$sale)[-length(saledata$sale)]), x = sum(saledata$sale)/45000, label = label), size = 3)+ ## 在圖中加上百分比:x 調節標籤到圓心的距離, y 調節標籤的左右位置ntheme(panel.grid=element_blank()) + ## 去掉白色圓框和中間的坐標線n theme(panel.border=element_blank()) ## 去掉最外層正方形框n

跟餅圖一樣的意義。

四、條形圖

area <- c("A","B","C","D","E")nsale <- c(1000,5000,7500,8000,9500)nsalemoney <- data.frame(area,sale)nggplot(data=salemoney,aes(x=area,y=sale))+ngeom_bar(fill="skyblue",stat="identity",position="dodge")+ ##stat="identity"表示原始數據集不作任何統計變換。ngeom_text(mapping = aes(label = sale)) ##標註標籤n

area <- c("A","A","B","B","C","C")nsale <- c(1000,7000,5000,8000,9500,3000)nyear <- c("2016","2017","2016","2017","2016","2017")nsalemoney <- data.frame(area,sale,year)nggplot(data=salemoney,aes(x=area,y=sale,fill=year))+ngeom_bar(stat="identity",position="dodge")+ngeom_text(mapping = aes(label = sale))n

area <- c("A","A","B","B","C","C")nsale <- c(1000,7000,5000,8000,9500,3000)nyear <- c("2016","2017","2016","2017","2016","2017")nsalemoney <- data.frame(area,sale,year)nggplot(data=salemoney,aes(x=area,y=sale,fill=year))+ngeom_bar(stat="identity",position="stack")+ngeom_text(mapping = aes(label = sale))n

條形圖可以作為數據間的大小對比,相對於餅圖而言,它更加的精準。

五、直方圖

set.seed(1234)nscore <- c(rnorm(100,mean = 450, sd = 2))nscoredata <- data.frame(score)nggplot(data=scoredata,aes(x=score))+ngeom_histogram(position="identity",colour="blue",fill="blue")n

累積頻數圖:

age <- c("0","18","25","40","55","80","100")nage <- factor(age,order=TRUE,levels=c("0","18","25","40","55","80","100"))nCum.freq <- c(0,2000,4500,6500,8500,9400,9500)nagedata <- data.frame(age,Cum.freq)nggplot(data=agedata,aes(x=age,y=Cum.freq,group=1))+ngeom_line()n

主要運用在數值型圖形,可以體現每個區間的寬度、頻數、頻數密度、累積頻數等等。


推薦閱讀:

哪些城市孕育了獨角獸?
數據分析學習階段性回顧和總結
三十而立,何為立,為何立!!!
動輒數百TB級數據的分析平台 海量並發無壓力
數據分析探索之旅(二):R 的基本操作與數據結構

TAG:数据分析 |