0

I would like to align the strata boxes to the borders of the graph in ggalluvial plots, and cannot find the way to customize my plot accordingly.

I could not find the way to adjust their position using hjust, it only moves the texts.

Here is my code :

library(dplyr)
library(ggplot2)
library(ggalluvial)

alluv2 <- nplus %>%
  group_by(dpth_loc.factor, N_post_CNA.factor) %>%
  summarise(obs=n(), .groups="keep") %>%
  ggplot(aes(y=obs,
             axis1=dpth_loc.factor,
             axis2=N_post_CNA.factor)) +
  geom_alluvium(aes(fill=N_post_CNA.factor))+
  geom_stratum(width=1/4, alpha =0.8)+
  geom_text(stat = "stratum",
            aes(label = after_stat(stratum))) +
  scale_x_discrete(limits = c("Depth Location", "N post NAC Status"),
                   expand = c(0.15, 0.09)) +
  scale_fill_manual(values  = c("#d4d5b3", "#60436e")) +
  theme_minimal()+
  theme(legend.position="none",
        axis.text.x = element_text(size = 12, face = "bold"), 
        axis.title.y = element_blank())

and the resulting graph :

alluvial plot

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
caroma
  • 1
  • 1
  • You can try `scale_x_discrete(limits = c("Depth Location", "N post NAC Status"), expand = c(0,0))`. If you edit your question to include a sample data set for reproducibility, it will be easier for people to help answer. – Seth Jun 08 '23 at 13:02
  • 1
    Thnak you very much ! This gives me the result I expected :-) – caroma Jun 10 '23 at 14:56

0 Answers0