Rstudent
Rstudent

Reputation: 885

Sankey diagram in time

I'd like to draw a Sankey diagram based on the variable time. Basically, I'd like to have time on the y axis and temperatures and humidity grouped by date on the x axis. As you can see, the time is not displayed correctly. Why? Also, how can I change the size of the humidity column to fit in the box?

Sample code:

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()

Sample data:

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")

enter image description here

Upvotes: 0

Views: 481

Answers (1)

DPH
DPH

Reputation: 4344

From what I understand of the y-axis mentioned, you are trying to achive something like this (this is how far I got)... maybe check out other 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())

enter image description here

you could add loose text to the stacks to identify them or work on custom labling of the x axis as well.

on a side note: moving the color argument in to the aes call of geom_alluvium() does not work 100% as expected

Upvotes: 1

Related Questions