在冲积地块中设置颜色和标题
Set colors & title in an alluvial plot
我有一个简单的 data.frame,使用冲积包制作成简单的冲积图。如何编辑情节?我的问题按重要性排序是:
- 更改配色方案,使流量来自同一个
"Admitted To"单位颜色相同
- 添加标题
- 保存此图,以便稍后我可以将其绘制成带有几个 ggplot 的网格
警告:ggalluvial 可能更容易,但不幸的是我无法在工作中安装它,因此解决方案需要使用 base r、ggplot 或 alluvial 包。
library(alluvial)
df <- structure(list(Admitted.To =
c("UnitC", "UnitC", "UnitC", "UnitC", "UnitD", "UnitD",
"UnitD", "UnitD", "UnitE", "UnitE", "UnitE", "UnitF",
"UnitB", "UnitB", "UnitB", "UnitB", "UnitB", "UnitG",
"UnitH", "UnitA", "UnitA", "UnitA", "UnitA", "UnitA"),
Discharged.From = c("UnitC", "UnitD", "UnitE", "UnitA",
"UnitC", "UnitD", "UnitE", "UnitA",
"UnitD", "UnitE", "UnitA", "UnitF",
"UnitD", "UnitI", "UnitE", "UnitB",
"UnitA", "UnitG", "UnitH", "UnitC",
"UnitD", "UnitI", "UnitE", "UnitA"),
n = c(136, 2, 1, 2, 1, 162, 2, 3, 1, 213, 1, 3, 5, 1, 7,
22, 23, 1, 32, 10, 9, 39, 9, 607)),
.Names = c("Admitted.To", "Discharged.From", "n"),
row.names = c(NA, -24L),
class = c("tbl_df", "tbl", "data.frame"))
我一直在使用下面的颜色代码,直到我弄清楚如何将颜色映射到 "Admitted To" 组
set.seed(8) # for nice colors
cols <- hsv(h = sample(1:8/10), s = sample(3:8)/8, v = sample(3:8)/8)
还有我的冲积地块代码:
alluvial(df[,1:2],
freq = 8,
blocks = T,
col = cols)
我尝试将 title = "SampleTitleHere"
添加到我的代码中,但它只是绘制了另一列。我没有找到很多关于这个包的文档。
感谢您使用冲积包。一一解答您的问题:
1 Change the color scheme so that flows coming from the same "Admitted.To" unit are the same color.
例如像那样
pal <- RColorBrewer::brewer.pal(8, "Set1") # colors to use
alluvial(
f[,1:2],
freq = 8,
blocks = T,
col = k[ match(f$Admitted.To, unique(f$Admitted.To)) ]
)
- Add a title
也许我们会添加一个 title
或 main
参数。同时使用 mtext()
在顶部
添加一个 "margin text"
pal <- RColorBrewer::brewer.pal(8, "Set1")
alluvial(
f[,1:2],
freq = 8,
blocks = T,
col = k[ match(f$Admitted.To, unique(f$Admitted.To)) ]
)
mtext("A title", 3, line=3, font=2)
如果您的标题跨越不止一行,或者您希望标题和情节之间有更大的边距或 space,您可以 (a) 通过使用更多的第三行来增大边距传递给 mar
参数的向量元素; (b) 乱用 line
到 mtext()
的参数来调整标题应该出现在离情节多远的地方。
- Save this plot so I can later plot it into a grid with a few ggplots
我没有快速的答案。如果您需要将它与其他 ggplot-based 数字混合使用 ggalluvial
将是更好的选择。请参阅下文如何让它发挥作用。
Caveat: ggalluvial might be easier but unfortunately I can't install it at work, so the solution needs to use base r, ggplot, or the alluvial package.
即使您不是系统管理员,您也应该能够安装和使用任何 R 包(如 ggalluvial
)。您只需要将它们安装在您有权写入文件的位置。这甚至可以是您保存分析的文件夹。参见例如https://csg.sph.umich.edu/docs/R/localpackages.html or http://www.stat.osu.edu/computer-support/mathstatistics-packages/installing-r-libraries-locally-your-home-directory,或 Google 表示 "R user library tree"。
我有一个简单的 data.frame,使用冲积包制作成简单的冲积图。如何编辑情节?我的问题按重要性排序是:
- 更改配色方案,使流量来自同一个 "Admitted To"单位颜色相同
- 添加标题
- 保存此图,以便稍后我可以将其绘制成带有几个 ggplot 的网格
警告:ggalluvial 可能更容易,但不幸的是我无法在工作中安装它,因此解决方案需要使用 base r、ggplot 或 alluvial 包。
library(alluvial)
df <- structure(list(Admitted.To =
c("UnitC", "UnitC", "UnitC", "UnitC", "UnitD", "UnitD",
"UnitD", "UnitD", "UnitE", "UnitE", "UnitE", "UnitF",
"UnitB", "UnitB", "UnitB", "UnitB", "UnitB", "UnitG",
"UnitH", "UnitA", "UnitA", "UnitA", "UnitA", "UnitA"),
Discharged.From = c("UnitC", "UnitD", "UnitE", "UnitA",
"UnitC", "UnitD", "UnitE", "UnitA",
"UnitD", "UnitE", "UnitA", "UnitF",
"UnitD", "UnitI", "UnitE", "UnitB",
"UnitA", "UnitG", "UnitH", "UnitC",
"UnitD", "UnitI", "UnitE", "UnitA"),
n = c(136, 2, 1, 2, 1, 162, 2, 3, 1, 213, 1, 3, 5, 1, 7,
22, 23, 1, 32, 10, 9, 39, 9, 607)),
.Names = c("Admitted.To", "Discharged.From", "n"),
row.names = c(NA, -24L),
class = c("tbl_df", "tbl", "data.frame"))
我一直在使用下面的颜色代码,直到我弄清楚如何将颜色映射到 "Admitted To" 组
set.seed(8) # for nice colors
cols <- hsv(h = sample(1:8/10), s = sample(3:8)/8, v = sample(3:8)/8)
还有我的冲积地块代码:
alluvial(df[,1:2],
freq = 8,
blocks = T,
col = cols)
我尝试将 title = "SampleTitleHere"
添加到我的代码中,但它只是绘制了另一列。我没有找到很多关于这个包的文档。
感谢您使用冲积包。一一解答您的问题:
1 Change the color scheme so that flows coming from the same "Admitted.To" unit are the same color.
例如像那样
pal <- RColorBrewer::brewer.pal(8, "Set1") # colors to use
alluvial(
f[,1:2],
freq = 8,
blocks = T,
col = k[ match(f$Admitted.To, unique(f$Admitted.To)) ]
)
- Add a title
也许我们会添加一个 title
或 main
参数。同时使用 mtext()
在顶部
pal <- RColorBrewer::brewer.pal(8, "Set1")
alluvial(
f[,1:2],
freq = 8,
blocks = T,
col = k[ match(f$Admitted.To, unique(f$Admitted.To)) ]
)
mtext("A title", 3, line=3, font=2)
如果您的标题跨越不止一行,或者您希望标题和情节之间有更大的边距或 space,您可以 (a) 通过使用更多的第三行来增大边距传递给 mar
参数的向量元素; (b) 乱用 line
到 mtext()
的参数来调整标题应该出现在离情节多远的地方。
- Save this plot so I can later plot it into a grid with a few ggplots
我没有快速的答案。如果您需要将它与其他 ggplot-based 数字混合使用 ggalluvial
将是更好的选择。请参阅下文如何让它发挥作用。
Caveat: ggalluvial might be easier but unfortunately I can't install it at work, so the solution needs to use base r, ggplot, or the alluvial package.
即使您不是系统管理员,您也应该能够安装和使用任何 R 包(如 ggalluvial
)。您只需要将它们安装在您有权写入文件的位置。这甚至可以是您保存分析的文件夹。参见例如https://csg.sph.umich.edu/docs/R/localpackages.html or http://www.stat.osu.edu/computer-support/mathstatistics-packages/installing-r-libraries-locally-your-home-directory,或 Google 表示 "R user library tree"。