【问题标题】:Sankey diagram in time桑基图时间
【发布时间】:2022-01-06 12:00:57
【问题描述】:

我想根据变量time 绘制桑基图。基本上,我希望在 y 轴上有 time,在 x 轴上按 date 分组 temperatureshumidity。如您所见,时间显示不正确。为什么?另外,如何更改humidity 列的大小以适合该框?

示例代码:

library(ggplot2)
library(ggalluvial)

df$temperature<-round(df$temperature, digits = 0)
df$humidity<-round(df$humidity, digits = 2)
ggplot(df, aes(axis1 = temperature, axis2 = humidity, y = time)) +
  geom_alluvium(aes(fill = date)) +
  scale_x_discrete(limits = c("temperature", "humidity"))+
  geom_stratum() + 
  geom_text(stat = "stratum", aes(label = after_stat(stratum))) +
  theme_bw()

样本数据:

df<-structure(list(time = structure(c(63285, 63885, 64485, 65085, 
65685, 66285, 66885, 67485, 68085, 68685, 69285, 69885, 70485, 
71085, 71685, 72285, 72885, 73485, 74085, 74685, 75285, 75885, 
76485, 77085, 77685, 78285, 78885, 79485, 80085, 80685, 81285, 
81885, 82485, 83085, 83685, 84285, 84885, 85485, 86085, 285, 
885, 1485, 2085, 2685, 3285, 3885, 4485, 5085, 5685, 6285), class = c("hms", 
"difftime"), units = "secs"), temperature = c(6.31, 14.81, 17.13, 
16.56, 16.44, 16.44, 16.56, 16.69, 16.88, 17, 17.13, 17.19, 17.31, 
17.5, 17.81, 18.06, 18.19, 18.25, 18.25, 18.19, 18.06, 17.94, 
17.81, 17.75, 17.63, 17.5, 17.44, 17.38, 17.25, 17.19, 17.13, 
17.06, 17, 17, 16.88, 16.88, 16.88, 16.81, 16.81, 16.81, 16.81, 
16.88, 16.88, 16.88, 16.88, 16.88, 16.81, 16.81, 16.81, 16.75
), humidity = c(0.674, 0.643, 0.472, 0.503, 0.515, 0.523, 0.53, 
0.539, 0.541, 0.546, 0.548, 0.549, 0.555, 0.561, 0.578, 0.577, 
0.575, 0.579, 0.596, 0.597, 0.599, 0.604, 0.612, 0.616, 0.619, 
0.623, 0.637, 0.638, 0.639, 0.639, 0.643, 0.646, 0.649, 0.653, 
0.654, 0.66, 0.664, 0.668, 0.672, 0.676, 0.691, 0.693, 0.693, 
0.694, 0.693, 0.694, 0.698, 0.691, 0.694, 0.697), date = c("04/01/2022", 
"04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022", 
"04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022", 
"04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022", 
"04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022", 
"04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022", 
"04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022", 
"04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022", 
"04/01/2022", "04/01/2022", "04/01/2022", "05/01/2022", "05/01/2022", 
"05/01/2022", "05/01/2022", "05/01/2022", "05/01/2022", "05/01/2022", 
"05/01/2022", "05/01/2022", "05/01/2022", "05/01/2022")), row.names = c(NA, 
-50L), class = "data.frame")

【问题讨论】:

  • 请编辑您的问题,以包括在此使用的库
  • @jpsmith 谢谢完成

标签: r ggplot2


【解决方案1】:

根据我对提到的 y 轴的了解,您正在尝试实现这样的目标(这就是我所取得的成就)...也许可以查看其他 libraries

library(ggplot2)
library(lubridate)
library(ggalluvial)

df$temperature<-round(df$temperature, digits = 0)
df$humidity<-round(df$humidity, digits = 2)
df$dtime <- lubridate::dmy_hms(paste(df$date, df$time))

ggplot(df, aes(axis1 = temperature, axis2 = humidity, y = dtime, color = date)) +
    geom_alluvium(aes(fill = dtime), alpha = 1, size = 1) +
    geom_stratum() + 
    scale_y_datetime() +
    geom_text(stat = "stratum", aes(label = after_stat(stratum))) +
    theme_bw() + 
    theme(axis.title.x = element_blank(),
          axis.text.x=element_blank(),
          axis.ticks.x=element_blank(),
          axis.title.y = element_blank(),
          axis.text.y=element_blank(),
          axis.ticks.y=element_blank())

您可以向堆栈添加松散的文本以识别它们,或者也可以对 x 轴进行自定义标签。

附带说明:将颜色参数移动到 geom_alluvium() 的 aes 调用中并不能按预期 100% 工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-06
    • 2016-04-06
    • 2018-02-13
    • 2010-12-09
    • 2018-07-18
    • 2012-04-15
    相关资源
    最近更新 更多