介绍一个R包UpSetR,专门用来集合可视化,当多集合的韦恩图不容易看的时候,就是它大展身手的时候了。
一 载入R包 数据
#安装及加载R包 #install.packages("UpSetR") library(UpSetR) #载入数据集 data <- read.csv("upSet.csv",header=TRUE) #先大致浏览一下该数据集,数据集太长,就只看前几列 head(data,6)
二 upSet集合可视化
使用UpsetR包里面的upset()函数绘制集合可视化图形。
1. 基本参数
upset(data, sets = c("Action", "Adventure", "Comedy", "Drama", "Fantasy" , "Children","Crime"),#查看特定的几个集合 mb.ratio = c(0.55, 0.45),#控制上方条形图以及下方点图的比例 order.by = "freq", #如何排序,这里freq表示从大到小排序展示 keep.order = TRUE, #keep.order按照sets参数的顺序排序 number.angles = 30, #调整柱形图上数字角度 point.size = 2, line.size = 1, #点和线的大小 mainbar.y.label = "Genre Intersections", sets.x.label = "Movies Per Genre", #坐标轴名称 text.scale = c(1.3, 1.3, 1, 1, 1.5, 1)) #六个数字,分别控制c(intersection size title, intersection size tick labels, set size title, set size tick labels, set names, numbers above bars)
2. queries参数
queries参数分为四个部分:query, param, color, active;
query: 指定哪个query,UpSetR有内置,也可以自定义;
param: list, query作用于哪个交集
color:每个query都是一个list,里面可以设置颜色,没设置的话将调用包里默认的调色板;
active:被指定的条形图:TRUE显示颜色,FALSE在条形图顶端显示三角形;
upset(data, main.bar.color = "black", queries = list(list(query = intersects, #UpSetR 内置的intersects query params = list("Drama"), ##指定作用的交集 color = "red", ##设置颜色,未设置会调用默认调色板 active = F, # TRUE:条形图被颜色覆盖,FALSE:条形图顶端显示三角形 query.name = "Drama"), # 添加query图例 list(query = intersects, params = list("Action", "Drama"), active =T, query.name = "Emotional action"), list(query = intersects, params = list("Drama", "Comedy","Action"), color = "orange", active = T)), query.legend = "top")
可以看到 list("Drama") ,list("Action", "Drama") 和 list("Drama", "Comedy", "Action") 均根据 color 设置 显示了不同的颜色。
3. attribute.plots参数
添加属性图,内置有柱形图、散点图、热图等
3.1 添加柱形图和散点图
upset(data, main.bar.color = "black", queries = list(list(query = intersects, params = list("Drama"), color = "red", active = F, query.name = "Drama"), list(query = intersects, params = list("Action", "Drama"), active =T,query.name = "Emotional action"), 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), #T 则显示出上面queries定义的颜色 list(plot = histogram, x = "ReleaseDate", queries = F)), ncols = 2), # 添加的图分两列 query.legend = "top") #query图例在最上方
3.2 添加箱线图
每次最多添加两个箱线图
upset(data, boxplot.summary = c("AvgRating", "ReleaseDate"))