R+Stata自动调整散点图标签
我们经常在画散点图时需要标示出散点标签,但总是遇到标签重叠的情况,Stata需要手动调整标签位置,很费时间,查了Stata相关命令,目前Stata似乎也没有很好的解决方案,有时间的时候,我会尝试写一个自动调整标签的Stata命令。
幸运的是,R中已经有人开发了ggrepel包,可以自动调整标签位置,所以我们可以使用R和Stata交互命令rcall来实现标签位置的自动调整。
sysuse auto,clear
keep if _n<35
rcall:library(ggplot2)
rcall:library(ggrepel)
rcall:library(ggthemes)
rcall:data <- st.data()
rcall:a <- ggplot(data,aes(price, mpg)) +
geom_point(aes(price, mpg), color="red")+
geom_text_repel(aes(price, mpg, label=make))+
geom_smooth(method="auto",se=FALSE) +
theme_gray(base_size = 16)
rcall:ggsave("/Applications/Stata17/ado/personal/Raymond/scatter.pdf",plot=a)
