ggupset -- ggplot2版本的upset plot

栏目: R语言 · 发布时间: 5年前

内容简介:在《这包已经在CRAN上,所以可以用最简单的方式安装:

在《 一图告诉你venn plot和upset plot的关系 》一文中,我们应该很清楚这两者的关系,upset plot是更清晰的呈现方式,而且能够支持无数多个分类,在《 转UpSet图为ggplot? 》一文中,又介绍了一个转化UpSetR输出为ggplot2便于嵌图和拼图的方法,但这个需要一个补丁,然后我提交的这个补丁,一直没有被作者接收。而且毕竟UpSetR是用grid写的,像grid这种高级货,玩起来还是有点难度,我一直在想应该有一个 ggplot2 版本的upset plot,最近就让我在gayhub上发现了。

这包已经在CRAN上,所以可以用最简单的方式安装:

install.packages("ggupset")
library(tidyverse)
library(ggupset)

tidy_movies %>%
  distinct(title, year, length, .keep_all=TRUE) %>%
  ggplot(aes(x=Genres)) +
    geom_bar() +
    scale_x_upset(n_intersections = 20)

ggupset -- ggplot2版本的upset plot

它的做法是把 x-axis 给改了,不过我发现还有一个不太兼容的地方,你不能对输出使用 theme ,像上面的图,你如果 +theme_bw() 就会报错。但好在你可以在 scale_x_upset 前面加 theme ,也还OK。

比如你想应用 theme_bw ,则必须是:

tidy_movies %>%
  distinct(title, year, length, .keep_all=TRUE) %>%
  ggplot(aes(x=Genres)) +
    geom_bar() +
    theme_bw() + #加在最后则不行
    scale_x_upset(n_intersections = 20)

这样等同于说下面那部分,你没法用 theme 去控制,所以作者又提供了 theme_combmatrix 来控制下面那部分。

tidy_movies %>%
  distinct(title, year, length, .keep_all=TRUE) %>%
  ggplot(aes(x=Genres)) +
    geom_bar() +
    scale_x_upset(order_by = "degree") +
    theme_combmatrix(combmatrix.panel.point.color.fill = "green",
                     combmatrix.panel.line.size = 0,
                     combmatrix.label.make_space = FALSE)

ggupset -- ggplot2版本的upset plot

用ggplot2的好处

grid 就是封装, ggplot2 虽然是基于 grid ,但为什么大家这么爱 ggplot2 ,因为它的设计是抽象,所有的东西是乐高块,我们可以自己拼,像上面所提到的,ggupset实现的是另一种x-axis,那么各种x轴是分类型变量的图,就可以应用这样的x坐标,于是有了自由的选项,高级的图应运而生。

tidy_movies %>%
  distinct(title, year, length, .keep_all=TRUE) %>%
  ggplot(aes(x=Genres, y=year)) +
    geom_violin() +
    scale_x_upset(order_by = "freq", n_intersections = 12)

ggupset -- ggplot2版本的upset plot

df_complex_conditions %>%
  mutate(Label = pmap(list(KO, DrugA, Timepoint), function(KO, DrugA, Timepoint){
    c(if(KO) "KO" else "WT", if(DrugA == "Yes") "Drug", paste0(Timepoint, "h"))
  })) %>%
  ggplot(aes(x=Label, y=response)) +
    geom_boxplot() +
    geom_jitter(aes(color=KO), width=0.1) +
    geom_smooth(method = "lm", aes(group = paste0(KO, "-", DrugA))) +
    scale_x_upset(order_by = "degree",
                  sets = c("KO", "WT", "Drug", "8h", "24h", "48h"),
                  position="top", name = "") +
    theme_combmatrix(combmatrix.label.text = element_text(size=12),
                     combmatrix.label.extra_spacing = 5)

ggupset -- ggplot2版本的upset plot

avg_rating <- tidy_movies %>%
  mutate(Genres_collapsed = sapply(Genres, function(x) paste0(sort(x), collapse="-"))) %>%
  mutate(Genres_collapsed = fct_lump(fct_infreq(as.factor(Genres_collapsed)), n=12)) %>%
  group_by(stars, Genres_collapsed) %>%
  summarize(percent_rating = sum(votes * percent_rating)) %>%
  group_by(Genres_collapsed) %>%
  mutate(percent_rating = percent_rating / sum(percent_rating)) %>%
  arrange(Genres_collapsed) %>%
  ggplot(avg_rating, aes(x=Genres_collapsed, y=stars, fill=percent_rating)) +
    geom_tile() +
    stat_summary_bin(aes(y=percent_rating * stars), fun.y = sum,  geom="point", 
                     shape="—", color="red", size=6) +
    axis_combmatrix(sep = "-", levels = c("Drama", "Comedy", "Short", 
                    "Documentary", "Action", "Romance", "Animation", "Other")) +
    scale_fill_viridis_c()

ggupset -- ggplot2版本的upset plot


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

JavaScript权威指南(第6版)

JavaScript权威指南(第6版)

David Flanagan / 淘宝前端团队 / 机械工业出版社 / 2012-4-1 / 139.00元

本书是程序员学习核心JavaScript语言和由Web浏览器定义的JavaScript API的指南和综合参考手册。 第6版涵盖HTML 5和ECMAScript 5。很多章节完全重写,以便与时俱进,紧跟当今的最佳Web开发实践。本书新增章节描述了jQuery和服务器端JavaScript。 本书适合那些希望学习Web编程语言的初、中级程序员和希望精通JavaScript的JavaSc......一起来看看 《JavaScript权威指南(第6版)》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具