前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >R语言ggplot2一幅好看的频率分布直方图实例

R语言ggplot2一幅好看的频率分布直方图实例

作者头像
用户7010445
发布2021-12-28 08:20:05
2.6K0
发布2021-12-28 08:20:05
举报
文章被收录于专栏:小明的数据分析笔记本

推文内容来自于链接

https://www.andrewheiss.com/blog/2021/12/18/bayesian-propensity-scores-weights/

这个博文里的内容还挺多的,我们只关注其中关于频率分布直方图的实现代码。

读取数据集

代码语言:javascript
复制
nets_with_weights<-read.csv("nets_with_weights.csv")

准备作图配色

代码语言:javascript
复制
isfahan <- MetBrewer::met.brewer("Isfahan1")
length(isfahan)
isfahan[1]

这里用到的配色包是 https://github.com/BlakeRMills/MetBrewer 这个用到的都是博物馆里的油画的配色,挺有意思的,大家可以试试

使用ggplot2作图

这里频率分布直方图用到的是geom_histogram()函数,这里的代码多了一个weight参数,暂时没有想明白这个参数起到什么作用

还遇到一个新函数colorspace::lighten()操作颜色,看帮助文档是是颜色更亮。做一个散点图试试效果

代码语言:javascript
复制
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

频率分布直方图

代码语言:javascript
复制
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")

如果要倒过来加一个负号就可以了

代码语言:javascript
复制
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")

添加文本注释

代码语言:javascript
复制
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) 

对细节的一些调整

代码语言:javascript
复制
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"))

示例数据和代码大家可以自己到推文开头提到的链接去下载

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-12-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 小明的数据分析笔记本 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 推文内容来自于链接
  • 读取数据集
  • 准备作图配色
  • 使用ggplot2作图
  • 频率分布直方图
  • 如果要倒过来加一个负号就可以了
  • 添加文本注释
  • 对细节的一些调整
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档