标签:txt nis 手写 index 自己 sha identity name src
蜀汉英雄传是一款gba游戏,打游戏的过程中搜集了其中的人物数据,但是没有给出人物归属,所以得处理一下。数据见百度网盘:链接: https://pan.baidu.com/s/1TMclLvFsnIRdytuZQPBVxA 提取码:cdrw
import pandas as pd import numpy as np pd.options.display.max_rows=10 heros=pd.read_excel(‘C:/Users/Administrator/Desktop/University/蜀汉英雄传人物.xlsx‘) colnames=[‘人物‘,‘武力‘,‘智力‘,‘速度‘,‘武器‘,‘天赋1‘]+[‘天赋‘+str(i) for i in range(2,10)] heros.columns=colnames country=pd.read_csv(‘C:/Users/Administrator/Desktop/University/人物归属.txt‘,header=None,sep=‘、‘) s1= country.iloc[0,:][country.iloc[0,:].notnull()] country1=pd.DataFrame(list(s1[0]*s1.shape[0])) s2= country.iloc[1,:][country.iloc[1,:].notnull()] country2=pd.DataFrame(list(s2[0]*s2.shape[0])) s3= country.iloc[2,:][country.iloc[2,:].notnull()] country3=pd.DataFrame(list(s3[0]*s3.shape[0])) s=pd.concat([s1,s2,s3],axis=0) own=pd.concat([country1,country2,country3],axis=0) belong=pd.concat([s,own],axis=1) belong.columns=[‘人物‘,‘国家‘] tidy=pd.merge(heros,belong,how=‘inner‘,left_on=[‘人物‘],right_on=[‘人物‘]) tidy=tidy.drop_duplicates() tidy.to_csv(‘C:/Users/Administrator/Desktop/University/最终数据.csv‘,index=False)
这里自己动手写一个图层geom_sword(),专门用来画剑。
library(grid)
library(ggplot2)
GeomSword<-ggproto(‘GeomSword‘,Geom,
required_aes=c(‘x‘,‘y‘),
draw_key=draw_key_polygon,
default_aes=aes(fill=‘white‘,handle=‘grey‘,size=0.1,angle=0),
draw_panel=function(data,panel_params,coord){
coords<-coord$transform(data,panel_params)
Body<-lapply(1:nrow(coords),function(i){
vp=viewport(x=coords$x[i],y=coords$y[i],
width=coords$size[i],height=coords$size[i]*(1+sqrt(5))/2,
angle=coords$angle[i],
just=c(‘center‘,‘center‘),
default.units=‘native‘)
polygonGrob(vp=vp,
gp=grid::gpar(fill=coords$fill[i]),
x=c(1/2,1/3,5/12,7/12,2/3),
y=c(8/9,2/3,1/3,1/3,2/3))
})
class(Body)<-"gList"
Handle<-lapply(1:nrow(coords),function(i){
vp=viewport(x=coords$x[i],y=coords$y[i],
width=coords$size[i],height=coords$size[i]*(1+sqrt(5))/2,
angle=coords$angle[i],
just=c(‘center‘,‘center‘),
default.units=‘native‘)
polygonGrob(vp=vp,
gp=grid::gpar(fill=coords$handle[i]),
x=c(1/3,1/3,11/24,11/24,
13/24,13/24,2/3,2/3),
y=c(1/3,1/3-1/12,1/3-1/12,1/18,
1/18,1/3-1/12,1/3-1/12,1/3))
})
class(Handle)<-"gList"
ggplot2:::ggname(‘geom_sword‘,gTree(children=gList(Body,Handle)))
})
geom_sword<-function(mapping=NULL,data=NULL,
stat=‘identity‘,position=‘identity‘,
na.rm=FALSE,show.legend=NA,
inherit.aes=TRUE,...){
ggplot2::layer(
geom=GeomSword,
data=data,mapping=mapping,
stat=stat,position=position,
show.legend=show.legend,
inherit.aes=inherit.aes,
params=list(na.rm=na.rm,...)
)
}
图层写完后,R读入数据,画图
heros<-read.csv(‘C:/Users/Administrator/Desktop/University/最终数据.csv‘,
header=TRUE,sep=‘,‘,fileEncoding=‘utf-8‘)
ggplot(heros,aes(武力,智力))+
geom_sword(angle=seq(0,360,length=nrow(heros)),aes(fill=速度))+
facet_wrap(vars(国家))
效果如下:

先打个草稿,以后再来补充解释吧
标签:txt nis 手写 index 自己 sha identity name src
原文地址:https://www.cnblogs.com/Enjoy-Respect-9527/p/13129863.html