前言
介绍一个R包UpSetR,专门用来集合可视化,当多集合的韦恩图不容易看的时候,就是它大展身手的时候了。
一、R包及数据
#安装及加载R包 #install.packages("UpSetR") library(UpSetR) #载入数据集 data <- read.csv("upSet.csv",header=TRUE) #先大致浏览一下该数据集,数据集太长,就只看前几列 head(data[,1:6],6) #View(data) #弹出窗口,可查看数据
二、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")
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(movies, boxplot.summary = c("AvgRating", "ReleaseDate"))
3.3 添加密度曲线图
因默认属性图中没有密度曲线,需要自定义plot函数
#自定义密度曲线 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(0, 0, 0, 0), "cm"), legend.key.size = unit(0.4, "cm"))) }
upset(data, main.bar.color = "black", mb.ratio = c(0.5, 0.5), 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 = 50, plots = list(list(plot = histogram, x = "ReleaseDate", queries = F), list(plot = scatter_plot, x = "ReleaseDate", y = "AvgRating", queries = T), list(plot = another.plot, x = "AvgRating", y = "ReleaseDate", queries = F)), ncols = 3))
参考
以上就是R语言UpSet包实现集合可视化示例详解的详细内容,更多关于R语言UpSet包集合可视化的资料请关注好代码网其它相关文章!