【R语言】洛伦兹曲线和基尼系数计算

数据框“global_df”的形式为:
【R语言】洛伦兹曲线和基尼系数计算_第1张图片

load("global_df.Rdata")
load("bs_df.Rdata")
library(ggplot2)
library(gridExtra)
p_g<-ggplot(data=global_df)+theme_bw()+theme( panel.grid =element_blank(),
                                              title = element_text(size=11,family = "STKaiti"), 
        
                                             axis.title=element_text(size=10,family ="STKaiti"), 
                                             axis.text=element_text(size=9,face= "bold"),
                                             plot.margin = unit(c(0.5,3.5,0.5,3.5),"mm")
                                             )+scale_y_continuous(limits=c(0,1),expand=c(0,0))+scale_x_continuous(limits=c(1,199),expand=c(0,0))

p_g<-p_g+geom_point(aes(x=num,y=ex_s_p),color='blue',size=1.5)
p_g<-p_g+geom_point(aes(x=num,y=im_s_p),color='yellow',size=1.5)
p_g<-p_g+geom_line(aes(x=num,y=1/198*num-1/198),color='red',size=1.2,linetype='dashed')
p_g<-p_g+xlab("国家数目")+ylab("贸易额/总量")+geom_line(aes(x=rep(c(25,40),1990/2),y=rep(c(0.75,0.75),1990/2)),linetype=1,color='blue',size=1.2)
p_g<-p_g+geom_text(aes(x=57,y=0.7,label="出口,=0.77"),family="STKaiti",size=3)+geom_line(aes(x=rep(c(25,40),1990/2),y=rep(c(0.65,0.65),1990/2)),linetype=1,color='yellow',size=1.2)+geom_text(aes(x=57,y=0.6,label="进口,=0.73"),family="STKaiti",size=3)

num=-c(1:10)+200
num1=num
num1[6]=185
num1[9]=180
name=as.character(id_global$country)[num]
im_global_vec<-global_df[global_df$year=="2014",]$im_s_p[num]
top10<-data.frame(num=num,num1=num1,name=name,im=im_global_vec)
p_g<-p_g+geom_point(data=top10,aes(x=num,y=im),pch=18,color="red",size=2)+
  geom_text(data=top10,aes(x=num1-5,y=im-0.01,label=name),size=2.5,family="STKaiti")+
  geom_point(aes(x=65/2,y=0.8),pch=18,color="red",size=2)+geom_text(aes(x=88,y=0.8,label="核心国家(地区)"),family="STKaiti",size=3)

【R语言】洛伦兹曲线和基尼系数计算_第2张图片

#基尼系数计算
global_gini.ex<-c()
global_gini.im<-c()
for (i in c(1:10))
{
     
  y=2006+i
  global_gini.ex[i]<-ineq(global_df[year==y,]$ex_s)
  global_gini.im[i]<-ineq(global_df[year==y,]$im_s)
}

你可能感兴趣的:(杂,r语言,ggplot2)