这篇文章主要介绍“R语言UpSet包怎么实现集合可视化”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“R语言UpSet包怎么实现集合可视化”文章能帮助大家解决问题。
一、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 queryparams = 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包怎么实现集合可视化”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注编程网行业资讯频道,小编每天都会为大家更新不同的知识点。