R語(yǔ)言UpSet包實(shí)現(xiàn)集合可視化示例詳解
前言
介紹一個(gè)R包UpSetR,專門用來(lái)集合可視化,當(dāng)多集合的韋恩圖不容易看的時(shí)候,就是它大展身手的時(shí)候了。
一、R包及數(shù)據(jù)
#安裝及加載R包 #install.packages("UpSetR") library(UpSetR) #載入數(shù)據(jù)集 data <- read.csv("upSet.csv",header=TRUE) #先大致瀏覽一下該數(shù)據(jù)集,數(shù)據(jù)集太長(zhǎng),就只看前幾列 head(data[,1:6],6) #View(data) #彈出窗口,可查看數(shù)據(jù)
二、upset()函數(shù)
使用UpsetR包里面的upset()函數(shù)繪制集合可視化圖形。
1)基本參數(shù)
upset(data, sets = c("Action", "Adventure", "Comedy", "Drama", "Fantasy" , "Children","Crime"),#查看特定的幾個(gè)集合 mb.ratio = c(0.55, 0.45),#控制上方條形圖以及下方點(diǎn)圖的比例 order.by = "freq", #如何排序,這里freq表示從大到小排序展示 keep.order = TRUE, #keep.order按照sets參數(shù)的順序排序 number.angles = 30, #調(diào)整柱形圖上數(shù)字角度 point.size = 2, line.size = 1, #點(diǎn)和線的大小 mainbar.y.label = "Genre Intersections", sets.x.label = "Movies Per Genre", #坐標(biāo)軸名稱 text.scale = c(1.3, 1.3, 1, 1, 1.5, 1)) #六個(gè)數(shù)字,分別控制c(intersection size title, intersection size tick labels, set size title, set size tick labels, set names, numbers above bars)
2)queries參數(shù)
queries參數(shù)分為四個(gè)部分:query, param, color, active;
query: 指定哪個(gè)query,UpSetR有內(nèi)置,也可以自定義;
param: list, query作用于哪個(gè)交集
color:每個(gè)query都是一個(gè)list,里面可以設(shè)置顏色,沒(méi)設(shè)置的話將調(diào)用包里默認(rèn)的調(diào)色板;
active:被指定的條形圖:TRUE顯示顏色,F(xiàn)ALSE在條形圖頂端顯示三角形;
upset(data, main.bar.color = "black", queries = list(list(query = intersects, ? #UpSetR 內(nèi)置的intersects query params = list("Drama"), ##指定作用的交集 color = "red", ##設(shè)置顏色,未設(shè)置會(huì)調(diào)用默認(rèn)調(diào)色板 active = F, ? # TRUE:條形圖被顏色覆蓋,F(xiàn)ALSE:條形圖頂端顯示三角形 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參數(shù)
添加屬性圖,內(nèi)置有柱形圖、散點(diǎn)圖、熱圖等
3.1 添加柱形圖和散點(diǎn)圖
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, #散點(diǎn)圖 x = "ReleaseDate", y = "AvgRating", #橫縱軸的變量 queries = T), #T 則顯示出上面queries定義的顏色 list(plot = histogram, x = "ReleaseDate", queries = F)), ncols = 2), # 添加的圖分兩列 query.legend = "top") #query圖例在最上方
3.2 添加箱線圖
每次最多添加兩個(gè)箱線圖
upset(movies, boxplot.summary = c("AvgRating", "ReleaseDate"))
3.3 添加密度曲線圖
因默認(rèn)屬性圖中沒(méi)有密度曲線,需要自定義plot函數(shù)
#自定義密度曲線 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語(yǔ)言UpSet包實(shí)現(xiàn)集合可視化示例詳解的詳細(xì)內(nèi)容,更多關(guān)于R語(yǔ)言UpSet包集合可視化的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
使用R語(yǔ)言繪制3D數(shù)據(jù)可視化scatter散點(diǎn)圖實(shí)現(xiàn)步驟
這篇文章主要為大家介紹了使用R語(yǔ)言繪制3D數(shù)據(jù)可視化scatter散點(diǎn)圖的實(shí)現(xiàn)步驟,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02R語(yǔ)言關(guān)于“包”的知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家分享的是一篇關(guān)于R語(yǔ)言“包”的知識(shí)點(diǎn)總結(jié)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-03-03R語(yǔ)言中因子相關(guān)知識(shí)點(diǎn)詳解
在本篇內(nèi)容里小編給大家總結(jié)了關(guān)于R語(yǔ)言中因子的相關(guān)知識(shí)點(diǎn)以及相關(guān)實(shí)例內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-04-04R語(yǔ)言中c()函數(shù)與paste()函數(shù)的區(qū)別說(shuō)明
這篇文章主要介紹了R語(yǔ)言中c()函數(shù)與paste()函數(shù)的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04