仿照r语言50个ggplot2可视化案例
导入数据
clear
insheet using "https://raw.githubusercontent.com/selva86/datasets/master/gdppercap.csv" , names
- 1 修改变量名称
rename v2 y1952
rename v3 y1957
- 2 生成散点的标签 格式为 地区名+逗号+数值
第一步,首先生成逗号
gen str douhao = "," //str表示生成字符型
第二步,将1952和1957中的数值取整数,生成字符型数字标签
gen shuzi52 = int(y1952)
tostring shuzi52 ,replace
gen shuzi57 = int(y1957)
tostring shuzi57 ,replace
第三步,生成散点标签
gen label52 = continent + douhao + shuzi52
gen label57 = continent + douhao + shuzi57
- 3 生成作图的X轴数据
gen x1 = 1
gen x2 = 2
- 4 作图
#d ;
gr twoway pcspike y1952 x1 y1957 x2 if y1957 > y1952 ,lcolor("8 187 59")||
pcspike y1952 x1 y1957 x2 if y1957 < y1952,lcolor("248 118 109") ||
scatter y1952 x1 , ms(i) mlabpos(9) mlabel(label52) ||
scatter y1957 x2 , ms(i) mlabpos(3) mlabel(label57) || ,
xlabel(1 2) xscale(range(0.5 2.5)) xtitle("")
ylabel(0 5000 10000) yscale(range(0 13000)) ytitle("Mean GdpPerCap")
legend(off)
ysize(6)
xline(1 2 ,lcolor(black%50) lpattern(dash))
text(13000 1 "Year1952" , place(w) )
text(13000 2 "Year1957", place(e)) ;
#d cr
Graph.png