admin管理员组文章数量:1406734
I am trying to make an alluvial plot for changes in species composition. Lets say, that some species occurs on sites currently. but under slimate change, some species will remain, some will disapear and novel species will come.
I would like to show this transition using alluvial plot. But, I can't properly claim it to be properly plot? Here is my working example:
library(ggalluvial)
# try a simple test: still not working, needs further simlification !
df_test <- data.frame(site = c(1,1,1,2,2,1,2,2,2,2),
site_ch = c("a","a","a","b","b","a","b","b","b","b"),
acc = c("piab",
"abies",
"fasy",
"piab",
"fasy",
"frax",
"piab",
"fasy",
"frax",
"abies"
),
scenario = rep(c("current", 'rcp25'), each = 5)) %>%
mutate(acc = factor(acc),
site_ch = factor(site_ch),
scenario = factor(scenario))
# Ensure each species appears across both scenarios per site
df_alluvial <- df_test %>%
count(site, scenario, acc) %>% # Count occurrences of each species per site per scenario
complete(site, scenario, acc, fill = list(n = 0)) %>% # Ensure all combinations exist
rename(freq = n) %>% # Rename count column for clarity
arrange(site, acc, scenario) #%>%
#dplyr::filter(freq !=0)
# Ensure scenario is a factor in correct order
df_alluvial <- df_alluvial %>%
mutate(scenario = factor(scenario, levels = c("current", "rcp25")),
acc = factor(acc),
site = factor(site)) %>%
group_by(scenario, acc) %>%
dplyr::summarise(sum_n = sum(freq, na.rm = T))
# Create an alluvial plot
ggplot(df_alluvial,
aes(x = scenario, stratum = acc, alluvium = sum_n, y = sum_n, fill = acc)) +
geom_alluvium(alpha = 0.6, aes(fill = acc)) + # Connects species across scenarios
geom_stratum() + # Adds category blocks
theme_minimal() +
labs(title = "Species Presence Across Scenarios",
x = "Scenario",
y = "Frequency",
fill = "Species") +
scale_fill_brewer(palette = "Set2")
I get an error:
Error in `geom_alluvium()`:
! Problem while computing stat.
ℹ Error occurred in the 1st layer.
Caused by error in `setup_data()`:
! Data is not in a recognized alluvial form (see `help('alluvial-data')` for details).
Run `rlang::last_trace()` to see where the error occurred.
This is how my output should look like (a bit more elegant of course! ):
I am trying to make an alluvial plot for changes in species composition. Lets say, that some species occurs on sites currently. but under slimate change, some species will remain, some will disapear and novel species will come.
I would like to show this transition using alluvial plot. But, I can't properly claim it to be properly plot? Here is my working example:
library(ggalluvial)
# try a simple test: still not working, needs further simlification !
df_test <- data.frame(site = c(1,1,1,2,2,1,2,2,2,2),
site_ch = c("a","a","a","b","b","a","b","b","b","b"),
acc = c("piab",
"abies",
"fasy",
"piab",
"fasy",
"frax",
"piab",
"fasy",
"frax",
"abies"
),
scenario = rep(c("current", 'rcp25'), each = 5)) %>%
mutate(acc = factor(acc),
site_ch = factor(site_ch),
scenario = factor(scenario))
# Ensure each species appears across both scenarios per site
df_alluvial <- df_test %>%
count(site, scenario, acc) %>% # Count occurrences of each species per site per scenario
complete(site, scenario, acc, fill = list(n = 0)) %>% # Ensure all combinations exist
rename(freq = n) %>% # Rename count column for clarity
arrange(site, acc, scenario) #%>%
#dplyr::filter(freq !=0)
# Ensure scenario is a factor in correct order
df_alluvial <- df_alluvial %>%
mutate(scenario = factor(scenario, levels = c("current", "rcp25")),
acc = factor(acc),
site = factor(site)) %>%
group_by(scenario, acc) %>%
dplyr::summarise(sum_n = sum(freq, na.rm = T))
# Create an alluvial plot
ggplot(df_alluvial,
aes(x = scenario, stratum = acc, alluvium = sum_n, y = sum_n, fill = acc)) +
geom_alluvium(alpha = 0.6, aes(fill = acc)) + # Connects species across scenarios
geom_stratum() + # Adds category blocks
theme_minimal() +
labs(title = "Species Presence Across Scenarios",
x = "Scenario",
y = "Frequency",
fill = "Species") +
scale_fill_brewer(palette = "Set2")
I get an error:
Error in `geom_alluvium()`:
! Problem while computing stat.
ℹ Error occurred in the 1st layer.
Caused by error in `setup_data()`:
! Data is not in a recognized alluvial form (see `help('alluvial-data')` for details).
Run `rlang::last_trace()` to see where the error occurred.
This is how my output should look like (a bit more elegant of course! ):
Share Improve this question edited Mar 5 at 5:27 Jan 10.2k6 gold badges21 silver badges33 bronze badges asked Mar 4 at 20:37 mayccamaycca 4,1226 gold badges40 silver badges74 bronze badges1 Answer
Reset to default 1In your final ggplot, in the aes()
, alluvium = acc
replace alluvium = sum_n
;
Use geom_flow()
instead of geom_alluvium()
ggplot(df_alluvial,
aes(x = scenario,
y = sum_n,
stratum = acc,
alluvium = acc)) +
geom_flow() +
geom_stratum() +
theme_minimal() +
labs(title = "Species Presence Across Scenarios",
x = "Scenario",
y = "Frequency",
fill = "Species") +
scale_fill_brewer(palette = "Set2")
本文标签: rMake an alluvial plotStack Overflow
版权声明:本文标题:r - Make an alluvial plot - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745030260a2638451.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论