99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀R語言數(shù)據(jù)可視化—仿網(wǎng)易數(shù)獨圓環(huán)條形圖
R語言數(shù)據(jù)可視化—仿網(wǎng)易數(shù)獨圓環(huán)條形圖
2017-05-07
收藏

R語言數(shù)據(jù)可視化—仿網(wǎng)易數(shù)獨圓環(huán)條形圖

一個案例,告訴你如何靈活的運用ggplot2來制作花樣繁多的信息圖!

雖然ggplot2的內置圖層只有屈指可數(shù)的幾十個,可是圖表組合之后的可能性是無限的。

實際上還是萬變不離其宗,就如同使用Excel模仿復雜圖表一樣,再復雜的圖表也是有不同的元素和模塊組合起來的,只要能夠用心去拆解,都可以找到行之有效的方法。如同庖丁解牛,只要洞悉骨架和經(jīng)脈,才能隨心所欲的下刀。

setwd("F:/數(shù)據(jù)可視化/R/R語言學習筆記/可視化/ggplot2/商務圖表")

library("ggplot2")

library("tidyr")

library("dplyr")

library("grid")

library("showtext")

library('"Cairo")

library("scales")

font.add("myfont","msyhl.ttc")

mydata<-read.csv("mydata.csv",stringsAsFactors=FALSE,check.names=FALSE) 

mydata$index<-1:nrow(mydata)

mydata$angle1<-1.5*seq(-1,-59)

mydata$angle2<-1.5*seq(59,1)

label<-strsplit(mydata$Country,"")

for (i in 1:length(label)){

label[[i]]<-paste0(label[[i]],collapse="\n")

}

mydata$label<-unlist(label)

mydata$label[37:59]<-gsub("\n","",mydata$label[37:59])

mynewdata<-mydata%>%gather(Class,Value,2:4)

圖形一:

mynewdata$Class<-factor(mynewdata$Class,levels=c("環(huán)保優(yōu)先","其他/未回答","經(jīng)濟優(yōu)先"),order=T)

p1<-ggplot(data=mynewdata,aes(x=index,y=Value,fill=Class))+

geom_bar(stat="identity",width=0.95)+

geom_text(aes(y=105,label=ifelse(index<=36,label,""),angle=angle1),hjust=.5,vjust=0,family="myfont")+

geom_text(aes(y=105,label=ifelse(index>36,label,""),angle=angle2),hjust=0,vjust=0.5,family="myfont")+

geom_text(aes(y=Value,label=Value,angle=angle2),position=position_stack(vjust=.9),family="myfont")+

xlim(0.5,236.5)+ylim(-120,105)+

coord_polar(theta="x")+

guides(fill=guide_legend(title=NULL))+

scale_fill_manual(values=c("#2EA7E0","#B5B5B6","#CBE510"))+

theme(

text=element_text(size=20),

line=element_blank(),

rect=element_blank(),

axis.text=element_blank(),

axis.title=element_blank(),

legend.position=c(.95,.75),

legend.key.size  =unit(.8,'cm'),

)

以上可以制作出原始案例的外圍圓環(huán)圖:

mydata2<-data.frame(year=c("1998年","2004年","2009年","2014年"),經(jīng)濟優(yōu)先=c(.24,.27,.23,.28),未回答=c(.25,.21,.23,.15),環(huán)保優(yōu)先=c(.51,.52,.54,.57),smallyear=rep(.15,4),check.names = FALSE)

names(mydata2)[3]<-"其他/未回答"

mydata2$index<-1:nrow(mydata2)

mynewdata2<-mydata2%>%gather(Class,Value,2:5)

mynewdata2$Class<-factor(mynewdata2$Class,levels=c("smallyear","環(huán)保優(yōu)先","其他/未回答","經(jīng)濟優(yōu)先"),order=T)

p2<-ggplot(data=mynewdata2,aes(x=index,y=Value,fill=Class))+

geom_bar(stat="identity",width=0.99)+

geom_text(aes(y=Value,label=ifelse(mynewdata2$Class=="smallyear",levels(mynewdata2$year),""),angle=c(rep(0,12),11.25*seq(-1,-7,-2))),position=position_stack(vjust=.5),family="myfont",size=7.5)+

geom_text(aes(y=Value,label=ifelse(mynewdata2$Class!="smallyear",percent(mynewdata2$Value),"")),position=position_stack(vjust=.5),family="myfont",size=6)+

xlim(0.5,16.5)+ylim(-.5,1.25)+

coord_polar(theta="x")+

guides(fill=FALSE)+

scale_fill_manual(values=c("#EFEFEF","#2EA7E0","#B5B5B6","#CBE510"))+

theme(

line=element_blank(),

rect=element_blank(),

axis.text=element_blank(),

axis.title=element_blank(),

legend.position="none"

)

以上可以做出內側小圓環(huán)圖:

CairoPNG(file="circletile1.png",width=2000,height=2000)

showtext.begin()

vie<-viewport(width=0.5,height=0.5,x=0.5,y=0.5)

p1;print(p2,vp=vie)

grid.text(label="六成中國人認為\n環(huán)境比經(jīng)濟更重要",,x=.99,y=.90,gp=gpar(col="black",fontsize=60,draw=TRUE,just="right")

showtext.end()

dev.off()

合并一步,一幅完整的圖表便出爐了!


數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師考試動態(tài)
數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調,回調的第一個參數(shù)驗證碼對象,之后可以使用它調用相應的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }