书接上回!!
前面介绍了upsetR
包的查询参数,今天继续介绍组合图形!
组合图形
attribute.plots
组合图形分为两种,一种是函数自带的参数,使用起来非常简单,用于显示不同交集的属性情况。
还是使用昨天的df数据。
upset(df,order.by = "freq",main.bar.color = "skyblue",sets.bar.color = "grey70",nintersects=20,
## 组合图形
boxplot.summary = c("col2","value")
)
竟然很炫酷有木有??:laugh
那这个箱线图有啥意思啊?其实也很简单,它展示的是交集的某个属性的分布情况。
就比如col2的第一个条形,展示的就是c,d,e,f,g5个集合的交集(共163个元素),在col2的分布情况。
另外一种添加图形的方式是通过attribute.plots
参数实现。
attribute.plots
包含以下参数:
下面是自带的2个组合图形,直方图和散点图:
upset(df, order.by = "freq",main.bar.color = "skyblue",sets.bar.color = "grey70",
## 添加图形
attribute.plots = list(
gridrows = 50,
plots = list(
list(plot = histogram,x="col1",queries=F),
list(plot = scatter_plot,x="col2",y="value",queries=F)
),
ncol = 2
)
)
由于数据问题,这个图看不出来有什么特别作用,所以下面还是使用自带数据集进行演示。
“自带数据集
movies
,是一个电影类型数据,共有3883行(3881部电影),21列。第1列是电影名字,还有上映时间(ReleaseDate)、评分(AvgRatings)、观看数(Watches)。其余列是由0-1矩阵表示的电影类型。
movies <- read.csv(system.file("extdata", "movies.csv", package = "UpSetR"),
header = T, sep = ";")
str(movies)
## 'data.frame': 3883 obs. of 21 variables:
## $ Name : chr "Toy Story (1995)" "Jumanji (1995)" "Grumpier Old Men (1995)" "Waiting to Exhale (1995)" ...
## $ ReleaseDate: int 1995 1995 1995 1995 1995 1995 1995 1995 1995 1995 ...
## $ Action : int 0 0 0 0 0 1 0 0 1 1 ...
## $ Adventure : int 0 1 0 0 0 0 0 1 0 1 ...
## $ Children : int 1 1 0 0 0 0 0 1 0 0 ...
## $ Comedy : int 1 0 1 1 1 0 1 0 0 0 ...
## $ Crime : int 0 0 0 0 0 1 0 0 0 0 ...
## $ Documentary: int 0 0 0 0 0 0 0 0 0 0 ...
## $ Drama : int 0 0 0 1 0 0 0 0 0 0 ...
## $ Fantasy : int 0 1 0 0 0 0 0 0 0 0 ...
## $ Noir : int 0 0 0 0 0 0 0 0 0 0 ...
## $ Horror : int 0 0 0 0 0 0 0 0 0 0 ...
## $ Musical : int 0 0 0 0 0 0 0 0 0 0 ...
## $ Mystery : int 0 0 0 0 0 0 0 0 0 0 ...
## $ Romance : int 0 0 1 0 0 0 1 0 0 0 ...
## $ SciFi : int 0 0 0 0 0 0 0 0 0 0 ...
## $ Thriller : int 0 0 0 0 0 1 0 0 0 1 ...
## $ War : int 0 0 0 0 0 0 0 0 0 0 ...
## $ Western : int 0 0 0 0 0 0 0 0 0 0 ...
## $ AvgRating : num 4.15 3.2 3.02 2.73 3.01 3.88 3.41 3.01 2.66 3.54 ...
## $ Watches : int 2077 701 478 170 296 940 458 68 102 888 ...
下面就是画图,主要是使用attribute.plots
参数添加更多图形。
前面说过,组合的图形是支持query的,可以根据查询到的数据画图。
upset(movies, main.bar.color = "black",
## 添加查询条件
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 = 45,
plots = list(
list(plot = scatter_plot, x = "ReleaseDate", y = "AvgRating", queries = T), # 根据查询结果画图
list(plot = scatter_plot, x = "AvgRating", y = "Watches", queries = F)),
ncols = 2),
query.legend = "bottom")
这个图包含了非常多信息:
这就是queries和attribute.plots的基本用法。
下面再展示一个ggplot2的用法。
# 首先定义2个函数,可以返回ggplot对象
library(ggplot2)
library(plyr)
## ------------------------------------------------------------------------------
## You have loaded plyr after dplyr - this is likely to cause problems.
## If you need functions from both plyr and dplyr, please load plyr first, then dplyr:
## library(plyr); library(dplyr)
## ------------------------------------------------------------------------------
##
## 载入程辑包:'plyr'
## The following objects are masked from 'package:dplyr':
##
## arrange, count, desc, failwith, id, mutate, rename, summarise,
## summarize
myplot <- function(mydata, x, y) {
plot <- (ggplot(data = mydata, aes_string(x = x, y = y, colour = "color")) +
geom_point() + scale_color_identity() + theme(plot.margin = unit(c(0,
0, 0, 0), "cm")))
}
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(movies, main.bar.color = "black",
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 = 45,
plots = list(
list(plot = myplot, x = "ReleaseDate",y = "AvgRating", queries = T),
list(plot = another.plot, x = "AvgRating", y = "ReleaseDate", queries = F)),
ncols = 2)
)
关于组合图形还有另一种形式:metadata,明天继续!
以上就是今天的内容,希望对你有帮助哦!欢迎点赞、在看、关注、转发!
联系客服