R 中的桑基图:如何更改与每个节点相关的各个部分的高度(Y)?
Sankey diagram in R: How to change the height (Y) of individual sections related to each node?
问题
如何更改 Sankey diagram 的每个 section/node 的高度?我想创建类似下面的图像 1 的内容,其中 'gender' 部分较小,然后 'cause' 部分较大,然后 'age' 部分再次较小:
我的输出更像图 2,其中每个部分(燃料、部门、最终用途、转换设备)具有相同的高度:
代码:
library(ggplot2)
library(ggalluvial)
library(RColorBrewer)
dfs <- dftest[ , c("Hospital", "Paciente", "Terapia", "Unit")]
alpha <- 1
getPalette <- colorRampPalette(brewer.pal(12, "Set3"))
colourCount <- length(unique(dfs$Hospital))
ggplot(dfs,
aes(axis1 = Hospital, axis2 = Paciente, axis3=Terapia)) +
geom_alluvium(aes(fill = Hospital),
width = 1/12, alpha = alpha, knot.pos = 0.5) +
geom_stratum(width = 1/20) +
scale_x_continuous(breaks = 1:3, labels = c("Hospital", "Patient", "Therapy")) +
scale_fill_manual(values = getPalette(colourCount)) +
ggtitle("Teste") +
theme_minimal() +
theme( legend.position = "none", panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.text.y = element_blank(),
axis.text.x = element_text(size = 12, face = "bold"))
我有 3 个 sections/nodes、中心(24 个不同)、患者(750 个不同)和疗法(10 个不同)。所以,我想我可以创建一个类似于图 1 的桑基图。您可以在下面找到一个合成数据集的 dput(dfs)
,因为我的数据集太大(750 人)无法包含在这里。
dput(dfs)
structure(list(Hospital = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("1",
"2", "3", "4", "5"), class = "factor"), Paciente = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L,
3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L,
5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L
), .Label = c("21", "22", "23", "24", "25", "26", "27"), class = "factor"),
Terapia = structure(c(2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
3L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L,
1L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
3L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L), .Label = c("Adalimumab",
"Etanercept", "Infliximab", "Rituximab"), class = "factor"),
Unit = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), class = "data.frame", row.names = c(NA,
-65L))
谁能指点一下?
我认为 ggalluvial
包的 geom 不是为自由浮动部分设计的。然而,正如它的创建者在包小插图中指出的那样,ggforce
包有类似的东西,如果下面的外观是你想要的:
使用的代码:
library(ggforce)
# transform dataframe into appropriate format
dfs2 <- gather_set_data(dfs, 1:3)
# define axis-width / sep parameters once here, to be used by
# each geom layer in the plot
aw <- 0.1
sp <- 0.1
ggplot(dfs2,
aes(x = x, id = id, split = y, value = Unit)) +
geom_parallel_sets(aes(fill = Hospital), alpha = 0.3,
axis.width = aw, sep = sp) +
geom_parallel_sets_axes(axis.width = aw, sep = sp) +
geom_parallel_sets_labels(colour = "white",
angle = 0, size = 3,
axis.width = aw, sep = sp) +
theme_minimal()
下面是一些不同参数值的演示:
问题
如何更改 Sankey diagram 的每个 section/node 的高度?我想创建类似下面的图像 1 的内容,其中 'gender' 部分较小,然后 'cause' 部分较大,然后 'age' 部分再次较小:
我的输出更像图 2,其中每个部分(燃料、部门、最终用途、转换设备)具有相同的高度:
代码:
library(ggplot2)
library(ggalluvial)
library(RColorBrewer)
dfs <- dftest[ , c("Hospital", "Paciente", "Terapia", "Unit")]
alpha <- 1
getPalette <- colorRampPalette(brewer.pal(12, "Set3"))
colourCount <- length(unique(dfs$Hospital))
ggplot(dfs,
aes(axis1 = Hospital, axis2 = Paciente, axis3=Terapia)) +
geom_alluvium(aes(fill = Hospital),
width = 1/12, alpha = alpha, knot.pos = 0.5) +
geom_stratum(width = 1/20) +
scale_x_continuous(breaks = 1:3, labels = c("Hospital", "Patient", "Therapy")) +
scale_fill_manual(values = getPalette(colourCount)) +
ggtitle("Teste") +
theme_minimal() +
theme( legend.position = "none", panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.text.y = element_blank(),
axis.text.x = element_text(size = 12, face = "bold"))
我有 3 个 sections/nodes、中心(24 个不同)、患者(750 个不同)和疗法(10 个不同)。所以,我想我可以创建一个类似于图 1 的桑基图。您可以在下面找到一个合成数据集的 dput(dfs)
,因为我的数据集太大(750 人)无法包含在这里。
dput(dfs)
structure(list(Hospital = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("1",
"2", "3", "4", "5"), class = "factor"), Paciente = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L,
3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L,
5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L
), .Label = c("21", "22", "23", "24", "25", "26", "27"), class = "factor"),
Terapia = structure(c(2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
3L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L,
1L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
3L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L), .Label = c("Adalimumab",
"Etanercept", "Infliximab", "Rituximab"), class = "factor"),
Unit = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), class = "data.frame", row.names = c(NA,
-65L))
谁能指点一下?
我认为 ggalluvial
包的 geom 不是为自由浮动部分设计的。然而,正如它的创建者在包小插图中指出的那样,ggforce
包有类似的东西,如果下面的外观是你想要的:
使用的代码:
library(ggforce)
# transform dataframe into appropriate format
dfs2 <- gather_set_data(dfs, 1:3)
# define axis-width / sep parameters once here, to be used by
# each geom layer in the plot
aw <- 0.1
sp <- 0.1
ggplot(dfs2,
aes(x = x, id = id, split = y, value = Unit)) +
geom_parallel_sets(aes(fill = Hospital), alpha = 0.3,
axis.width = aw, sep = sp) +
geom_parallel_sets_axes(axis.width = aw, sep = sp) +
geom_parallel_sets_labels(colour = "white",
angle = 0, size = 3,
axis.width = aw, sep = sp) +
theme_minimal()
下面是一些不同参数值的演示: