R語言繪圖

weixin_34148340發表於2017-05-27

R語言的視覺化完整版
https://wenku.baidu.com/view/ea6a948ca300a6c30c229f96.html

install.packages('devtools')
require(devtools)
devtools::install_github('taiyun/recharts')

在Rstuido中圖片可以預覽 在R中自動生成html 可以download儲存

library(recharts)

df2 = data.frame(
  saleNum=c(10,20,30,40,50,60,70,15,25,35,45,55,65,75,25,35,45,55,65,75,85),
  seller=c(rep("小黃",7), rep("小紅",7), rep("小白",7)),
  weekDay = c(rep(c("週一","週二","週三","週四","週五","週六","週日"),3))
)
eBar(dat= df2, xvar=~weekDay, yvar=~saleNum, series=~seller)
#png(file="myplot.png", bg="transparent")  無效

ggplot柱狀圖
http://blog.csdn.net/bone_ace/article/details/47267981
標尺 顏色 座標軸等
http://blog.sina.com.cn/s/blog_69ffa1f90101sigb.html

挑戰不可能之——ggplot環形字型地圖
http://www.sohu.com/a/133110897_572440

各種條形圖 以及主題

https://zhuanlan.zhihu.com/p/27316157
ggplot2——主題篇

圓弧條形圖

http://blog.csdn.net/scutshuxue/article/details/8836848
country<-c("寮國","菲律賓","尼泊爾","孟加拉","阿富汗","新加坡","泰國","蒙古","紐西蘭","印度尼西亞","印度","澳大利亞","臺灣","日本","香港","柬埔寨","馬來西亞","巴基斯坦","韓國","斯里蘭卡","中國","越南")  
percent<-c(90,81,80,77,75,74,73,72,68,68,68,67,65,63,61,60,59,58,53,51,49,48)  
d<-data.frame(country,percent)  
png("d:\\test.png",width = 2048, height = 2048)  
f<-function(name,value){  
      
    xsize=200  
    plot(0, 0,xlab="",ylab="",axes=FALSE,xlim=c(-xsize,xsize),ylim=c(-xsize,xsize))  
    for(i in 1:length(name)){  
        info = name[i]  
        percent = value[i]  
        k = (1:(360*percent/100)*10)/10  
        r=xsize*(length(name)-i+1)/length(name)  
        #print(r)  
        x=r*sin(k/180*pi)  
        y=r*cos(k/180*pi)  
        text(-18,r,info,pos=2,cex=3)  
        text(-9,r,paste(percent,"%"),cex=3)  
        lines(x,y,col="red")  
    }  
}  
  
#用R開啟,圖片在檔案中顯示正常 R中不正常  
f(country,percent)  
dev.off()


#7個一組
dd<-read.table("clipboard",header=TRUE)
class(dd$percent[1])
class(d$percent[1])
dd$percent<-as.numeric(dd$percent)
length(index)

png("d:\\test1.png",width = 2048, height = 2048)  
f<-function(name,value){  
  
  xsize=800  
  plot(0, 0,xlab="",ylab="",axes=FALSE,xlim=c(-xsize,xsize),ylim=c(-xsize,xsize))  
  # color<-c("red","green","purple","black","yellow","blue","red","green","purple","black","yellow","red","green","purple","black","yellow","red","green","purple","black","yellow","blue")

  a<-rep(rainbow(5,alpha=0.5),c(7,7,7,7,7))

  for(i in 1:length(name)){  
    info = name[i]  
    percent = value[i]  
    k = (1:(360*percent/100)*10)/10
    r=xsize*(length(name)-i+1)/length(name)
    #print(r)  
    x=r*sin(k/180*pi) 
    y=r*cos(k/180*pi)  
    text(-80,r,info,pos=2,cex=2.5)  
    text(-27,r,paste(percent,"%"),cex=2.5)  
    lines(x,y,col=a[i],lwd=10,type="l")
  }  
}  

f(dd$index,dd$percent)  
dev.off() 

地圖
ggmap
https://cos.name/2013/01/drawing-map-in-r-era/
http://www.dataguru.cn/article-3381-1.html

REmap
http://blog.csdn.net/yc_1993/article/details/51407981
通過REmap包畫航線圖
http://jingyan.baidu.com/article/5d6edee2eff2c599ebdeec7e.html
用R語言繪製動態地圖,程式碼奉上!(REmap包詳解)

直方圖重疊overlapping
http://www.iqiyi.com/w_19rspw1ech.html
hist(aim0$sum,xlim=c(-10,110),ylim=c(0,1300),main="score of unsatisfy vs satisfy",xlab="score",ylab=" Number",col=rgb(1,0,0,0.5))
hist(aim1$sum,add=TRUE,col=rgb(0,1,0,0.5))
R語言視覺化——圖表巢狀(母子圖)
https://zhuanlan.zhihu.com/p/27242202
ggplot2  以diamonds為例
https://www.plob.org/article/7264.html
plt.style.use('ggplot')
plt.plot(data_num1['date_string'],data_num1['U22'],'bo:',label='U22_0')
plt.plot(data1_1['date_string'],data1_1['U22'],'m*:',label='U22_1')

plt.legend()

#fill的用法
####修正因子水平 橫軸標籤順序
mydata2$Name=factor(mydata2$Name, levels=as.vector(data_barplot$Name))
mydata2$class=1

cs=c('C7','C30','C44','C45','C33')
Service_access=c('U9','U6','U4','U7')
Service_quality=c('U14','U12','U22')
net_access=c('C3','C2','C27','C41','C29','C12','C40')
net_keep=c('C25','C23','C26','C54','C56','C9','C18')

for (i in 1:26){
  myname=mydata2$Name[i]
  mydata2$class[i]=ifelse(myname %in% cs,'語音接入性',ifelse(myname %in% Service_access,'業務接入性',ifelse(myname %in% Service_quality,'業務質量',ifelse(myname %in% net_access,'網路接入性','網路保持性'))))
}

#螢幕取色 colorpix
#調色盤定製
cbbPalette <- c("#C23531", "#2F4554", "#61A0A8", "#D48265", "#9CC5B2") 

ggplot(mydata2,aes(Name,Score,fill=class))+
  geom_bar(stat="identity",position="dodge")+
  labs(x="index",y='Relative score')++scale_fill_manual(values=cbbPalette) 


相關文章