ggupset -- ggplot2版本的upset plot

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

内容简介:在《这包已经在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


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

查看所有标签

猜你喜欢:

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

用数据讲故事

用数据讲故事

[美] Cole Nussbaumer Knaflic / 陆 昊、吴梦颖 / 人民邮电出版社 / 2017-8 / 59.00元

本书通过大量案例研究介绍数据可视化的基础知识,以及如何利用数据创造出吸引人的、信息量大的、有说服力的故事,进而达到有效沟通的目的。具体内容包括:如何充分理解上下文,如何选择合适的图表,如何消除杂乱,如何聚焦受众的视线,如何像设计师一样思考,以及如何用数据讲故事。一起来看看 《用数据讲故事》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试