前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >独特的箱型图版式,你学会了吗?

独特的箱型图版式,你学会了吗?

作者头像
作图丫
发布2022-03-29 14:29:14
8610
发布2022-03-29 14:29:14
举报
文章被收录于专栏:作图丫

导语

GUIDE ╲

ggeconodist是开发者受Economist杂志独特风格的启发,开发的一款与普通绘制的箱型图不同风格的R包。

背景介绍

作为大家做生物信息学绘图的重要工具,R包的主要功能不仅仅是展示数据,如何做出更漂亮的图片也是重要的一部分。

今天要给大家介绍的是一种箱型图的个性画法,功能由ggeconodist包提供,开发者受一款杂志Economist中绘图风格的启发,从而开发了这样一款绘图美观的箱型图绘图包。

功能展示

代码语言:javascript
复制
##帮助将econodist图例添加到ggplot2绘图
add_econodist_legend()
##创建一个可以与econodist图表一起使用的图例grob
econodist_legend_grob()
##绘图和统计
geom_econodist() GeomEconodist 
stat_econodist() StatEconodist
##左对齐
left_align()
##更流行的Econodist 风格的ggplot2主题
theme_econodist()

ggeconodist可视化

01

R包的安装

代码语言:javascript
复制
install.packages("ggeconodist", repos = "https://cinc.rud.is")
library(ggeconodist)

02

基本箱型图的绘制

首先让我们来看一下正常用geom_boxplot()绘制的箱型图和geom_econodist()绘制的箱型图有什么区别,上面是普通的箱型图,下方是改变后的图片,相比之下确实美观了很多呀!

代码语言:javascript
复制
library(dplyr)
library(patchwork)
library(hrbrthemes)
library(gapminder)

p <- ggplot(mpg, aes(class, hwy)) + theme_ipsum_rc()
##将geom_boxplot改为geom_econodist即可用ggeconodist绘制箱型图
(p + geom_boxplot()) +
  (p + geom_econodist(width = 0.25)) +
  plot_layout(ncol = 1)

03

颜色设置

旋转一下图片,设置上下边界的颜色看看效果

代码语言:javascript
复制
#将图片转为横向
(p + geom_boxplot() + coord_flip()) +
#参数设置颜色
  (p + geom_econodist(tenth_col = ft_cols$blue, ninetieth_col = ft_cols$red) +
     coord_flip()) +
  plot_layout(ncol = 1)

按照数据中drv特征对箱型图上色

代码语言:javascript
复制
##设置填充颜色 
(p + geom_boxplot(aes(fill = factor(drv)))) +
  (p + geom_econodist(aes(fill = factor(drv)))) +
  plot_layout(ncol = 1)

04

构建数据集,绘制复杂一些的箱型图

代码语言:javascript
复制
gapminder %>%
  filter(year %in% c(1952, 1962, 1972, 1982, 1992, 2002)) %>%
  filter(continent != "Oceania") %>%
  ggplot(aes(x = factor(year), y = lifeExp, fill = continent)) +
  geom_econodist(
    median_point_size = 1.2,
    ##设置上下线颜色
    tenth_col = "#b07aa1",
    ninetieth_col = "#591a4f",
    show.legend = FALSE
  ) +
  ##标尺设置
  ggthemes::scale_fill_tableau(name = NULL) +
  coord_flip() +
  labs(
    x = "Year", title = "Life Expectancy", y = NULL,
    caption = "Example borrowed from @cmdline_tips"
  ) +
  ##分面设置
  facet_wrap(~continent, nrow = 4) +
  theme_ipsum_rc() -> gmgg

grid.newpage()
gmgg %>% 
  add_econodist_legend(
    econodist_legend_grob(
      tenth_col = "#b07aa1",
      ninetieth_col = "#591a4f",
    ), 
    below = "axis-b-1-4", 
    just = "right"
  ) %>% 
  grid.draw()

05

使用自己的数据看看效果

使用R中自带的数据集看看ggeconodist的基本功能吧

代码语言:javascript
复制
##普通boxplot
boxplot(Petal.Width~Species,iris,col="#b07aa1")
##使用ggeconodist绘图
ggplot(iris, aes(Species,Petal.Width,color=Species),color  = "Species")+ 
  geom_econodist(width = 0.5)

绘制复杂一些的箱型图看看!

代码语言:javascript
复制
##绘制两种常见的堆积模式
ggplot(diamonds,aes(cut,price,fill=color))+
  geom_econodist()
ggplot(diamonds,aes(cut,price,fill=color,alpha=1/30))+
  geom_econodist(position="identity")
代码语言:javascript
复制
###ggplot2两种分面板式
ggplot(diamonds,aes(cut,price,fill=color))+
  geom_econodist()+
  facet_grid(.~color) 
ggplot(diamonds,aes(cut,price,fill=color))+
  geom_econodist()+
  facet_wrap(~color)

对箱型图进行进一步的美化

代码语言:javascript
复制
library(ggthemes)
ggplot(diamonds,aes(cut,price,fill=color))+
  geom_econodist()+
  ##图题
  ggtitle("Box Plot")+
  ##添加economist特色主题
  theme_economist()+
  ##设置图例配色
  scale_fill_economist()+
  guides(fill=guide_legend(title=NULL)) 

ggplot(diamonds,aes(cut,price,fill=color))+
  geom_econodist()+
  ggtitle("Box Plot")+
  theme_economist()+
  scale_fill_economist()+
  guides(fill=guide_legend(title=NULL))+
  ##按颜色进行分面
  facet_grid(.~color)

06

Economist中原图绘制

最后让我们一起来看开发者是怎么绘制Economist杂志中给予他灵感来源的箱型图的!

代码语言:javascript
复制
##使用mammogram_costs数据
ggplot(mammogram_costs, aes(x = city)) +
  geom_econodist(
    aes(ymin = tenth, median = median, ymax = ninetieth),
    stat = "identity", show.legend = TRUE
  ) +
  ##标尺设置范围等
  scale_y_continuous(expand = c(0,0), position = "right", limits = range(0, 800)) +
  ###版面设置
  coord_flip() +
  labs(
    x = NULL, y = NULL,
    title = "Mammoscams",
    subtitle = "United States, prices for a mammogram*\nBy metro area, 2016, $",
    caption = "*For three large insurance companies\nSource: Health Care Cost Institute"
  ) +
  theme_econodist() -> gg

grid.newpage()
left_align(gg, c("subtitle", "title", "caption")) %>% 
  add_econodist_legend(econodist_legend_grob(), below = "subtitle") %>% 
  grid.draw()

文章参考:https://github.com/hrbrmstr/ggeconodist

小编总结

一类图形的绘制往往具有多种方法,在本文中介绍的ggeconodist就是一个很好的例子,使用不同的函数,让你的图片在茫茫图海中脱颖而出!大家可以按照自己的喜好选择更为适合自己的函数哟!

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

本文分享自 作图丫 微信公众号,前往查看

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档