gpt4 book ai didi

r - UpSetR 中的堆叠条形图

转载 作者:行者123 更新时间:2023-12-04 11:48:27 28 4
gpt4 key购买 nike

我一直在寻找一种在 upsetR 中具有堆叠条形图的方法图形。
我下载了电影数据集(来自 here )并添加了一个只有两个值“M”和“C”的列。
下面是有关我如何加载数据并添加“x”列的信息。

编辑:

m <- read.csv(system.file("extdata", "movies.csv", package = "UpSetR"), 
header = T, sep = ";")
nrow(m)
[1] 3883
x<-c(rep("M", 3000), rep("C", 883))
m<-cbind(m, x)
unique(m$x)
[1] M C

这是数据框的结构:
str(m)
'data.frame': 3883 obs. of 22 variables:
$ Name : Factor w/ 3883 levels "$1,000,000 Duck (1971)",..: 3577 1858 1483 3718 1175 1559 3010 3548 3363 1420 ...
$ 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 ...
$ x : Factor w/ 2 levels "M","C": 1 1 1 1 1 1 1 1 1 1 ...

现在我尝试实现堆积条形图如下:
upset(m,
queries = list(
list(query = elements,
params = list("x", "M"), color = "#e69f00", active = T),
list(query = elements,
params = list("x", "C"), color = "#cc79a7", active = T)))

结果如下所示:

enter image description here

正如您所看到的,比例是错误的,因为每个条形中应该只有“M”或“C”两种颜色(因子)。
这个问题似乎不是一个微不足道的问题,正如 here 也指出的.
有没有人知道如何在 UpsetR 中实现这一点?
非常感谢

最佳答案

这是一种使用堆叠条形图创建翻转图的方法,但使用我的 ComplexUpset 而不是 UpSetR:
stacked bars complex upset

library(ComplexUpset)
movies = as.data.frame(ggplot2movies::movies)
genres = colnames(movies)[18:24]

# for simplicity of examples, only use the complete data points
movies[movies$mpaa == '', 'mpaa'] = NA
movies = na.omit(movies)


upset(
movies,
genres,
base_annotations=list(
'Intersection size'=intersection_size(
counts=FALSE,
mapping=aes(fill=mpaa)
)
),
width_ratio=0.1
)
请在 documentation 中查看更多示例.
GitHub 上提供了安装说明: krassowski/complex-upset (还有与 UpSetR 和其他软件包的比较)。

关于r - UpSetR 中的堆叠条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54770795/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com