R语言ggplot2一幅好看的频率分布直方图实例
2023-04-18 16:25:43 时间
推文内容来自于链接
https://www.andrewheiss.com/blog/2021/12/18/bayesian-propensity-scores-weights/
这个博文里的内容还挺多的,我们只关注其中关于频率分布直方图的实现代码。
读取数据集
nets_with_weights<-read.csv("nets_with_weights.csv")
准备作图配色
isfahan <- MetBrewer::met.brewer("Isfahan1")
length(isfahan)
isfahan[1]
这里用到的配色包是 https://github.com/BlakeRMills/MetBrewer 这个用到的都是博物馆里的油画的配色,挺有意思的,大家可以试试
使用ggplot2作图
这里频率分布直方图用到的是geom_histogram()
函数,这里的代码多了一个weight
参数,暂时没有想明白这个参数起到什么作用
还遇到一个新函数colorspace::lighten()
操作颜色,看帮助文档是是颜色更亮。做一个散点图试试效果
library(ggplot2)
library(patchwork)
p1<-ggplot()+
geom_point(aes(x=1,y=1),size=50,color="darkgreen")
p2<-ggplot()+
geom_point(aes(x=1,y=1),size=50,
color=colorspace::lighten("darkgreen",0.9))
p1+p2
频率分布直方图
ggplot() +
geom_histogram(data = filter(nets_with_weights, net_num == 1),
bins = 50,
aes(x = propensity, weight = iptw),
fill = colorspace::lighten(isfahan[2], 0.35),
color="white")
如果要倒过来加一个负号就可以了
ggplot() +
geom_histogram(data = filter(nets_with_weights, net_num == 1),
bins = 50,
aes(x = propensity, weight = iptw),
fill = colorspace::lighten(isfahan[2], 0.35),
color="white")+
geom_histogram(data = filter(nets_with_weights, net_num == 0),
bins = 50, aes(x = propensity, weight = iptw,
y = -..count..),
fill = colorspace::lighten(isfahan[6], 0.35),
color="white")
添加文本注释
ggplot() +
geom_histogram(data = filter(nets_with_weights, net_num == 1),
bins = 50,
aes(x = propensity, weight = iptw),
fill = colorspace::lighten(isfahan[2], 0.35),
color="white")+
geom_histogram(data = filter(nets_with_weights, net_num == 0),
bins = 50, aes(x = propensity, weight = iptw,
y = -..count..),
fill = colorspace::lighten(isfahan[6], 0.35),
color="white")+
geom_histogram(data = filter(nets_with_weights, net_num == 1),
bins = 50, aes(x = propensity),
fill = isfahan[2],color="white") +
geom_histogram(data = filter(nets_with_weights, net_num == 0),
bins = 50, aes(x = propensity, y = -..count..),
fill = isfahan[6],
color="white")+
annotate(geom = "label",
x = 0.8, y = 70,
label = "Treated (actual)",
fill = isfahan[2],
color = "white", hjust = 1) +
annotate(geom = "label", x = 0.8,
y = 90, label = "Treated (IPTW pseudo-population)",
fill = colorspace::lighten(isfahan[2], 0.35),
color = "white", hjust = 1) +
annotate(geom = "label", x = 0.8, y = -60,
label = "Untreated (actual)",
fill = isfahan[6],
color = "white", hjust = 1) +
annotate(geom = "label",
x = 0.8, y = -80,
label = "Untreated (IPTW pseudo-population)",
fill = colorspace::lighten(isfahan[6], 0.35),
color = "white", hjust = 1)
对细节的一些调整
ggplot() +
geom_histogram(data = filter(nets_with_weights, net_num == 1),
bins = 50,
aes(x = propensity, weight = iptw),
fill = colorspace::lighten(isfahan[2], 0.35),
color="white")+
geom_histogram(data = filter(nets_with_weights, net_num == 0),
bins = 50, aes(x = propensity, weight = iptw,
y = -..count..),
fill = colorspace::lighten(isfahan[6], 0.35),
color="white")+
geom_histogram(data = filter(nets_with_weights, net_num == 1),
bins = 50, aes(x = propensity),
fill = isfahan[2],color="white") +
geom_histogram(data = filter(nets_with_weights, net_num == 0),
bins = 50, aes(x = propensity, y = -..count..),
fill = isfahan[6],
color="white")+
annotate(geom = "label",
x = 0.8, y = 70,
label = "Treated (actual)",
fill = isfahan[2],
color = "white", hjust = 1) +
annotate(geom = "label", x = 0.8,
y = 90, label = "Treated (IPTW pseudo-population)",
fill = colorspace::lighten(isfahan[2], 0.35),
color = "white", hjust = 1) +
annotate(geom = "label", x = 0.8, y = -60,
label = "Untreated (actual)",
fill = isfahan[6],
color = "white", hjust = 1) +
annotate(geom = "label",
x = 0.8, y = -80,
label = "Untreated (IPTW pseudo-population)",
fill = colorspace::lighten(isfahan[6], 0.35),
color = "white", hjust = 1) +
geom_hline(yintercept = 0, color = "white", size = 0.25) +
scale_y_continuous(label = abs) +
coord_cartesian(xlim = c(0.1, 0.8), ylim = c(-80, 100)) +
labs(x = "Propensity", y = "Count")+
theme_minimal() +
theme(panel.grid.minor = element_blank(),
plot.background = element_rect(fill = "white", color = NA),
plot.title = element_text(face = "bold"),
axis.title = element_text(face = "bold"),
strip.text = element_text(face = "bold", size = rel(0.8), hjust = 0),
strip.background = element_rect(fill = "grey80", color = NA),
legend.title = element_text(face = "bold"))
示例数据和代码大家可以自己到推文开头提到的链接去下载
相关文章
- 【技术种草】cdn+轻量服务器+hugo=让博客“云原生”一下
- CLB运维&运营最佳实践 ---访问日志大洞察
- vnc方式登陆服务器
- 轻松学排序算法:眼睛直观感受几种常用排序算法
- 十二个经典的大数据项目
- 为什么使用 CDN 内容分发网络?
- 大数据——大数据默认端口号列表
- Weld 1.1.5.Final,JSR-299 的框架
- JavaFX 2012:彻底开源
- 提升as3程序性能的十大要点
- 通过凸面几何学进行独立于边际的在线多类学习
- 利用行动影响的规律性和部分已知的模型进行离线强化学习
- ModelLight:基于模型的交通信号控制的元强化学习
- 浅谈Visual Source Safe项目分支
- 基于先验知识的递归卡尔曼滤波的代理人联合状态和输入估计
- 结合网络结构和非线性恢复来提高声誉评估的性能
- 最佳实践丨云开发CloudBase多环境管理实践
- TimeVAE:用于生成多变量时间序列的变异自动编码器
- 具有线性阈值激活的神经网络:结构和算法
- 内网渗透之横向移动 -- 从域外向域内进行密码喷洒攻击