[英]Set colors & title in an alluvial plot
I have a simple data.frame, made into a simple alluvial map using the alluvial package. 我有一个简单的data.frame,使用冲积包制作一个简单的冲积地图。 How can I edit the plot?
我该如何编辑情节? My questions, in order of importance, are:
我的问题按重要性排列:
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. 警告:ggalluvial可能更容易但不幸的是我无法在工作中安装它,因此解决方案需要使用base r,ggplot或冲积包。
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"))
I've been using the color code below until I figure out how to map the colors to the "Admitted To" group 我一直在使用下面的颜色代码,直到我弄清楚如何将颜色映射到“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)
And my alluvial plot code: 我的冲积地图代码:
alluvial(df[,1:2],
freq = 8,
blocks = T,
col = cols)
I've tried adding title = "SampleTitleHere"
into my code but it just plots another column. 我已经尝试将
title = "SampleTitleHere"
添加到我的代码中,但它只是绘制了另一列。 I haven't found much documentation on this package. 我没有找到关于这个包的很多文档。
Thanks for using the alluvial package. 感谢您使用冲积包。 Addressing your questions one by one:
逐一解决您的问题:
1 Change the color scheme so that flows coming from the same "Admitted.To" unit are the same color.
1更改颜色方案,使来自相同“Admitted.To”单元的流体颜色相同。
Eg like that 就像这样
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
添加标题
Perhaps we will add a title
or main
argument. 也许我们会添加
title
或main
参数。 Meanwhile use mtext()
to add a "margin text" on top 同时使用
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)
If your title spans more than one line or you'd rather have larger margin or space between the title and the plot you can (a) make the margin larger by using a larger number of the third element of a vector you pass to mar
argument; 如果您的标题跨越多行或者您希望在标题和绘图之间有更大的边距或空格,您可以(a)通过使用更大数量的向量传递给
mar
参数的向量来使边距更大; (b) mess with line
argument to mtext()
to adjust how far away from the plot the title should appear. (b)
mtext()
line
参数,以调整标题应该出现的距离。
- Save this plot so I can later plot it into a grid with a few ggplots
保存这个图,这样我以后可以将它绘制成一个带有几个ggplots的网格
I dont have a quick answer for that. 我没有快速回答这个问题。 If you need to mix it with other ggplot-based figures using
ggalluvial
would be a better choice. 如果您需要将其与其他基于ggplot的数据混合使用
ggalluvial
将是一个更好的选择。 See below how you might get it to work. 请参阅下文,了解如何使其发挥作用。
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.
警告:ggalluvial可能更容易但不幸的是我无法在工作中安装它,因此解决方案需要使用base r,ggplot或冲积包。
You should be able to install and use any R package (like ggalluvial
) even if you are not an administrator of your system. 即使您不是系统的管理员,您也应该能够安装和使用任何R包(如
ggalluvial
)。 You only need to install them somewhere you have a permission to write files. 您只需要在有权写入文件的地方安装它们。 This can be even the folder you keep your analysis.
这甚至可以是您保存分析的文件夹。 See eg 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 , or Google for "R user library tree".
参见例如https://csg.sph.umich.edu/docs/R/localpackages.html或http://www.stat.osu.edu/computer-support/mathstatistics-packages/installing-r-libraries-locally-您的主目录 ,或Google的“R用户库树”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.