打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
韦恩图进阶!upset plot 03
     Try to learn everything about something! 

书接上回!!

前面介绍了upsetR包的查询参数,今天继续介绍组合图形

  • 组合图形

    • attribute.plots

组合图形

组合图形分为两种,一种是函数自带的参数,使用起来非常简单,用于显示不同交集的属性情况。

还是使用昨天的df数据。

upset(df,order.by = "freq",main.bar.color = "skyblue",sets.bar.color = "grey70",nintersects=20,
      
      ## 组合图形
      boxplot.summary = c("col2","value")
      )
plot of chunk unnamed-chunk-9

竟然很炫酷有木有??:laugh

那这个箱线图有啥意思啊?其实也很简单,它展示的是交集的某个属性的分布情况。

就比如col2的第一个条形,展示的就是c,d,e,f,g5个集合的交集(共163个元素),在col2的分布情况。

attribute.plots

另外一种添加图形的方式是通过attribute.plots参数实现。

attribute.plots包含以下参数:

  • gridrows:设定添加的图形大小
  • plots:列表形式的一系列参数,主要包括plotxyqueries
    • plot:返回ggplot对象的函数
    • x:横坐标
    • y:纵坐标
    • queries:TRUE或者FALSE,是否使用查询到的数据覆盖图形
  • ncols:图形排布为几列

下面是自带的2个组合图形,直方图和散点图:

upset(df, order.by = "freq",main.bar.color = "skyblue",sets.bar.color = "grey70",
      
      ## 添加图形
      attribute.plots = list(
        gridrows = 50,
        plots = list(
          list(plot = histogram,x="col1",queries=F),
          list(plot = scatter_plot,x="col2",y="value",queries=F)
        ),
        ncol = 2
      )
      )
plot of chunk unnamed-chunk-10

由于数据问题,这个图看不出来有什么特别作用,所以下面还是使用自带数据集进行演示。

自带数据集movies,是一个电影类型数据,共有3883行(3881部电影),21列。第1列是电影名字,还有上映时间(ReleaseDate)、评分(AvgRatings)、观看数(Watches)。其余列是由0-1矩阵表示的电影类型。

movies <- read.csv(system.file("extdata""movies.csv", package = "UpSetR"), 
    header = T, sep = ";")

str(movies)
## 'data.frame': 3883 obs. of  21 variables:
##  $ Name       : chr  "Toy Story (1995)" "Jumanji (1995)" "Grumpier Old Men (1995)" "Waiting to Exhale (1995)" ...
##  $ ReleaseDate: int  1995 1995 1995 1995 1995 1995 1995 1995 1995 1995 ...
##  $ Action     : int  0 0 0 0 0 1 0 0 1 1 ...
##  $ Adventure  : int  0 1 0 0 0 0 0 1 0 1 ...
##  $ Children   : int  1 1 0 0 0 0 0 1 0 0 ...
##  $ Comedy     : int  1 0 1 1 1 0 1 0 0 0 ...
##  $ Crime      : int  0 0 0 0 0 1 0 0 0 0 ...
##  $ Documentary: int  0 0 0 0 0 0 0 0 0 0 ...
##  $ Drama      : int  0 0 0 1 0 0 0 0 0 0 ...
##  $ Fantasy    : int  0 1 0 0 0 0 0 0 0 0 ...
##  $ Noir       : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ Horror     : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ Musical    : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ Mystery    : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ Romance    : int  0 0 1 0 0 0 1 0 0 0 ...
##  $ SciFi      : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ Thriller   : int  0 0 0 0 0 1 0 0 0 1 ...
##  $ War        : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ Western    : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ AvgRating  : num  4.15 3.2 3.02 2.73 3.01 3.88 3.41 3.01 2.66 3.54 ...
##  $ Watches    : int  2077 701 478 170 296 940 458 68 102 888 ...

下面就是画图,主要是使用attribute.plots参数添加更多图形。

前面说过,组合的图形是支持query的,可以根据查询到的数据画图。

upset(movies, main.bar.color = "black"
      
      ## 添加查询条件
      queries = list(
        list(query = intersects, params = list("Drama"), color = "red", active = F),
        list(query = intersects, params = list("Action""Drama"), active = T),
        list(query = intersects, params = list("Drama""Comedy""Action"), color = "orange", active = T)), 
      
      ## 组合图形
      attribute.plots = list(
        gridrows = 45
        plots = list(
          list(plot = scatter_plot, x = "ReleaseDate", y = "AvgRating", queries = T), # 根据查询结果画图
          list(plot = scatter_plot, x = "AvgRating", y = "Watches", queries = F)), 
        ncols = 2), 
      query.legend = "bottom")
plot of chunk unnamed-chunk-12

这个图包含了非常多信息:

  • 首先是upset plot主体,展示了电影类型最多的前5个以及它们的数量,然后是彼此之间的交集及数量、元素个数;
  • 通过查询,突出3个交集;
  • 最下面的散点图,第一个展示了上映日期和评分的关系,可以看到3个交集被赋予不同的颜色,这和ggplot中的颜色映射非常像!第2个展示了评分和观看人数的关系。

这就是queriesattribute.plots的基本用法。

下面再展示一个ggplot2的用法。

# 首先定义2个函数,可以返回ggplot对象
library(ggplot2)
library(plyr)
## ------------------------------------------------------------------------------
## You have loaded plyr after dplyr - this is likely to cause problems.
## If you need functions from both plyr and dplyr, please load plyr first, then dplyr:
## library(plyr); library(dplyr)
## ------------------------------------------------------------------------------
## 
## 载入程辑包:'plyr'
## The following objects are masked from 'package:dplyr':
## 
##     arrange, count, desc, failwith, id, mutate, rename, summarise,
##     summarize

myplot <- function(mydata, x, y) {
    plot <- (ggplot(data = mydata, aes_string(x = x, y = y, colour = "color")) + 
        geom_point() + scale_color_identity() + theme(plot.margin = unit(c(0
        000), "cm")))
}

another.plot <- function(data, x, y) {
    data$decades <- round_any(as.integer(unlist(data[y])), 10, ceiling)
    data <- data[which(data$decades >= 1970), ]
    myplot <- (ggplot(data, aes_string(x = x)) + geom_density(aes(fill = factor(decades)), 
        alpha = 0.4) + theme(plot.margin = unit(c(0000), "cm"), legend.key.size = unit(0.4
        "cm")))
}

然后就是画图:

upset(movies, main.bar.color = "black"
      queries = list(
        list(query = intersects,params = list("Drama"), color = "red", active = F),
        list(query = intersects,params = list("Action""Drama"), active = T), 
        list(query = intersects,params = list("Drama""Comedy""Action"), color = "orange", active = T)), 
      attribute.plots = list(
        gridrows = 45
        plots = list(
          list(plot = myplot, x = "ReleaseDate",y = "AvgRating", queries = T), 
          list(plot = another.plot, x = "AvgRating", y = "ReleaseDate", queries = F)), 
        ncols = 2)
      )
plot of chunk unnamed-chunk-14

关于组合图形还有另一种形式:metadata,明天继续!

以上就是今天的内容,希望对你有帮助哦!欢迎点赞、在看、关注、转发

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
练习QPainter,运行时看不到图形,只有一个灰色窗口
Matlab中使用Plot函数动态画图方法总结
Rplot画图【转】
数据正态性检验画图的4种方法
【R分享|实战】维恩图(Venn)&集合图(Upset)的选择与绘制
使用mne进行脑电信号分析
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服