您好,登錄后才能下訂單哦!
這篇文章主要介紹“R語言UpSet包怎么實現集合可視化”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“R語言UpSet包怎么實現集合可視化”文章能幫助大家解決問題。
#安裝及加載R包 #install.packages("UpSetR") library(UpSetR) #載入數據集 data <- read.csv("upSet.csv",header=TRUE) #先大致瀏覽一下該數據集,數據集太長,就只看前幾列 head(data[,1:6],6) #View(data) #彈出窗口,可查看數據
使用UpsetR包里面的upset()函數繪制集合可視化圖形。
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)
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")
添加屬性圖,內置有柱形圖、散點圖、熱圖等
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圖例在最上方
每次最多添加兩個箱線圖
upset(movies, boxplot.summary = c("AvgRating", "ReleaseDate"))
因默認屬性圖中沒有密度曲線,需要自定義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包怎么實現集合可視化”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。