我正在尝试为我的数据创建桑基图。
对于每种治疗,都会对个体进行一段时间的跟踪。我希望有一个节点“治疗”(具有不同治疗名称的分类变量)随时间重复,并且 x 轴占时间。有任何想法吗?我真的很感谢任何帮助。
到目前为止,这是我尝试过的:
### install and load packages
install.packages("ggplot2")
install.packages("readxl")
install.packages("ggforce")
# load packages
library(ggplot2)
library(readxl)
library(ggforce)
### read dataset
dataset_new <- read_excel("Made_up_dataset_new.xlsx")
df_new <- as.data.frame(dataset_new)
df_new$Unit <- 1
df_sankey <- df_new[c("Therapy", "Frequency", "Continuous_time","Unit")]
# transform dataframe into appropriate format
df_sankey <- gather_set_data(df_sankey, 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(df_sankey,
aes(x = x, id = id, split = y, …Run Code Online (Sandbox Code Playgroud) 我想根据 2018-19 年的生产水平订购我的数据集。因此,首先是公司代码 3 (5000),然后是公司代码 1 (2000),最后是公司代码 2 (1000)。
我有一个这样的数据集:
| 公司代码 | 财务年 | 生产 |
|---|---|---|
| 1 | 2018-19 | 2000年 |
| 1 | 2019-20 | 2500 |
| 1 | 2020-21 | 3000 |
| 1 | 2018-21 | 7500 |
| 2 | 2018-19 | 1000 |
| 2 | 2019-20 | 1500 |
| 2 | 2020-21 | 1000 |
| 2 | 2020-21 | 3500 |
| 3 | 2018-19 | 5000 |
| 3 | 2019-20 | 5500 |
| 3 | 2020-21 | 4000 |
| 3 | 2018-21 | 14500 |
我想最终得到:
| 公司代码 | 财务年 | 生产 |
|---|---|---|
| 3 | 2018-19 | 5000 |
| 3 | 2019-20 | 5500 |
| 3 | 2020-21 | 4000 |
| 3 | 2018-21 | 14500 |
| 1 | 2018-19 | 2000年 |
| 1 | 2019-20 | 2500 |
| 1 | 2020-21 | 3000 |
| 1 | 2018-21 | 7500 |
| 2 | 2018-19 | 1000 … |