其他
ggplot2绘制美美的棒棒糖图
欢迎关注R语言数据分析指南
❝本节继续来分析绘图案例,来绘制一个美美的棒棒糖图,下面通过一个具体的例子来进行展示
❞
安装并加载R包
package.list=c("tidyverse","showtext","scales","ggprism")
for (package in package.list) {
if (!require(package,character.only=T, quietly=T)) {
install.packages(package)
library(package, character.only=T)
}
}
加载数据
sports <- read_tsv("sports.xls")
设置字体
font_add_google(name = "Black Ops One", family = "opsone")
font_add_google(name = "Dosis", family = "dosis")
showtext_auto()
数据清洗
plot_data <- sports %>%
select(exp_men, exp_women, sports) %>%
drop_na() %>%
filter(exp_men!= 0,exp_women!= 0) %>%
group_by(sports) %>%
summarise(mean_exp_men=mean(exp_men),
mean_exp_women=mean(exp_women)) %>%
mutate(mean_exp_diff=mean_exp_men - mean_exp_women) %>%
select(sports,mean_exp_diff) %>%
mutate(sports=fct_reorder(sports, mean_exp_diff),
less=as.factor(mean_exp_diff<0))
数据可视化
ggplot(data=plot_data,
mapping=aes(x=mean_exp_diff,y=sports, colour = less)) +
geom_point(size = 3) +
geom_segment(aes(yend=sports,xend=0),size=1) +
annotate("segment",y=1,yend=17,x=150000,xend=150000,colour="#4b0082",
arrow = arrow(ends="both",angle=90,length=unit(.2,"cm"))) +
annotate("segment",y=18,yend=31,x=-150000,xend=-150000,colour="#008080",
arrow=arrow(ends="both",angle=90,length=unit(.2,"cm"))) +
annotate("text",x=275000,y=9,label="Higher female expenditure",size=8,
colour="#4b0082",family="dosis") +
annotate("text",x=-275000,y=24.5,label="Higher male expenditure",size=8,
colour="#008080",family="dosis") +
scale_x_continuous(labels=unit_format(unit="K",scale=1e-3,sep=""),
limits=c(-600000,600000),expand=c(0,0),guide="prism_offset_minor") +
scale_colour_manual("",values=c("#008080","#4b0082")) +
coord_flip() +
labs(x=NULL,y=NULL)+
theme_prism(base_line_size=0.5)+
theme(plot.margin=unit(c(0.8,0.8,0.5,0.8),"cm"),
axis.line=element_line(color="black"),
axis.text.x=element_text(angle = 90,hjust=1,color="black"),
axis.text.y = element_text(color="black"),
panel.grid.major =element_line(size=0.2,color="#e5e5e5"),
panel.grid.minor=element_blank(),
panel.spacing=unit(0,"lines"),
legend.position="none")
数据获取
本节的内容到此结束,可以看到主要的内容还是数据清洗及图形的细节调整,喜欢的小伙伴欢迎转发此文档附上一句话到朋友圈「30分钟后台截图给我」,即可获取对应的数据及代码,如未及时回复可添加我的微信
欢迎大家扫描下方二位码加入「QQ交流群」,与全国各地上千位小伙伴交流
「关注下方公众号下回更新不迷路」,如需要加入微信交流群可添加小编微信,请备注单位+方向+姓名
往期推荐