I am trying to make a Sankey graph using the ggalluvial package in R.
I have the following data:
ID Cluster3(values ranging from 1 to 3) Cluster 6 (values ranging from 1 to 6). x and y coordinates (as this is a spatial dataset)
I merely want to plot a graph showing the relationship between the two (i.e. how is cluster 3 being split into 6 clusters).
I keep getting the following error message:
Error: Continuous value supplied to discrete scale
In addition
1: In to_lodes_form(data = data, axes = axis_ind, discern = params$discern) :
Some strata appear at multiple axes.
2: In to_lodes_form(data = data, axes = axis_ind, discern = params$discern) :
Some strata appear at multiple axes.
3: In to_lodes_form(data = data, axes = axis_ind, discern = params$discern) :
Some strata appear at multiple axes.
I suspect this is because of the ID values which are continious from 1 to 13336.
I tried using the x and y coordinates as "markers/identifiers" but here too, I get the same error.
I first tried using the networkd3 package in R, but the iGraph package within it doesn't seem to work. So, as an alternative I tried with ggalluvial using the code below
library(ggalluvial)
library(raster)
library(dplyr)
library(tidyverse)
Clust3 <- raster(paste0("//.....3Cluster.tif"))
Clus6 <- raster(paste0("//.....6Cluster.tif"))
stack_sdf3 = rasterToPoints(Clust3, spatial = T)
stack_df3 = as.data.frame(stack_sdf3)
stack_df3$ID <- sprintf("%03d", 1:nrow(stack_df3))
stack_sdf6 = rasterToPoints(Clus6, spatial = T)
stack_df6 = as.data.frame(stack_sdf6)
stack_df6$ID <- sprintf("%03d", 1:nrow(stack_df6))
# Edit the column names for better understanding
colnames(stack_df3) = c("Cluster_Number3","x","y","ID")
colnames(stack_df6) = c("Cluster_Number6","x","y","ID")
df3 <- stack_df3[, c("ID", "Cluster_Number3","x","y")]
df6 <- stack_df6[, c("ID", "Cluster_Number6","x","y")]
data <- df3 %>% right_join(df6, by=c("x", "y"))
data2<- select(data,"ID.x", "Cluster_Number3", "Cluster_Number6")
ggplot(data = data2,
aes(axis1 = Cluster_Number3, axis2 = Cluster_Number6, y = ID.x)) +
geom_alluvium(aes(fill = Cluster_Number3)) +
geom_stratum() +
geom_text(stat = "stratum",
aes(label = after_stat(stratum))) +
scale_x_discrete(limits = c("Main Cluster", "Diverging clusters"),
expand = c(0.15, 0.05)) +
scale_fill_viridis_d()+
theme_void()
Could you please help me with this, as I am a newbie in R.
Given example data in the format you stated (ignoring the
xandyvalues/columns because they're irrelevant here)...Using {ggalluvial}...
Using {networkD3}...