2

I'm trying to fix this issue for quite some time now and no code seems to work here. I'm not that experienced in R, so maybe my problem is easy to fix. The following code shall present an Alluvial plot which links Bee nests to certain Trees. The only thing that won't work is the fontsize of the Species on both sites, so that they will not overlap.

I´ve tried ggplot + theme(text=element_text(size=...); also theme(axis.text.x=element_text(size=...) or add size=... in every command shown below. Is the fontsize connected to the "stratum" stat?

ggplot(data = trees,
aes(axis1 = trees$Bee_species, axis2 = trees$Tree_species, y = trees$obs)) +
geom_alluvium(aes(fill = trees$Bee_species)) +
geom_stratum(width=0.50) +
geom_text(stat = "stratum", aes(label = after_stat(stratum))) +
scale_x_discrete(limits = c("Bee_species", "Tree_species"),
expand = c(0.40, 0.40))+
theme_void()+
theme(legend.position = "none") +
scale_fill_manual(values = c("purple", "green", "blue", "red", "turquoise", "yellow"))

Here the table:

dput(trees)
structure(list(Bee_species = c("Apis_mellifera", "Lestrimelitta_sp", 
"Lestrimelitta_sp", "Partamona_orizabaensis", "Partamona_orizabaensis", 
"Partamona_orizabaensis", "Partamona_orizabaensis", "Partamona_orizabaensis", 
"Partamona_orizabaensis", "Partamona_orizabaensis", "Partamona_orizabaensis", 
"Partamona_orizabaensis", "Partamona_orizabaensis", "Partamona_orizabaensis", 
"Partamona_orizabaensis", "Partamona_orizabaensis", "Partamona_orizabaensis", 
"Partamona_orizabaensis", "Partamona_orizabaensis", "Scaptotrigona_subobscuripennis", 
"Scaptotrigona_subobscuripennis", "Scaptotrigona_subobscuripennis", 
"Scaptotrigona_subobscuripennis", "Scaptotrigona_subobscuripennis", 
"Scaptotrigona_subobscuripennis", "Scaptotrigona_subobscuripennis", 
"Scaptotrigona_subobscuripennis", "Tetragonisca_angustula", "Tetragonisca_angustula", 
"Tetragonisca_angustula", "Tetragonisca_angustula", "Tetragonisca_angustula", 
"Tetragonisca_angustula", "Tetragonisca_angustula", "Trigona_corvina", 
"Trigona_corvina", "Trigona_corvina", "Trigona_corvina", "Trigona_corvina"
), Tree_species = c("tronco_muerto", "Ficus_jimenesii", "Spathodea_campanulata", 
"Bambusa_vulgaris", "Cedrela_odorata", "Cojoba_arborea", "Cordia_eriostigma", 
"Cupania_glabra", "Cupressus_lusitanica", "Elaeis_guineensis", 
"Eucalyptus_sp", "Ficus_jimenesii", "Jacaranda_mimosifolia", 
"Lagerstroemia_speciosa", "Phellodendron_sp", "Roystonea_regia", 
"Spathodea_campanulata", "Tabebuia_rosea", "tronco_muerto", "Casuarina_equisetifolia", 
"Cordia_eriostigma", "Cupressus_lusitanica", "Erythrina_poeppigiana", 
"Ficus_costaricana", "Ficus_jimenesii", "Mangifera_indica", "tronco_muerto", 
"Conifera", "Cupressus_lusitanica", "Ficus_costaricana", "Ficus_elastica", 
"Ficus_jimenesii", "Spathodea_campanulata", "Tapirira_mexicana", 
"Bambusa_vulgaris", "Citharexylum_sp", "Cupressus_sp", "Ficus_costaricana", 
"Ficus_jimenesii"), obs = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 4L, 
1L, 1L, 8L, 1L, 1L, 3L, 2L, 5L, 2L, 3L, 1L, 1L, 2L, 1L, 2L, 3L, 
1L, 1L, 1L, 1L, 1L, 1L, 5L, 1L, 1L, 1L, 1L, 1L, 1L, 2L)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -39L), groups = structure(list(
    Bee_species = c("Apis_mellifera", "Lestrimelitta_sp", "Partamona_orizabaensis", 
    "Scaptotrigona_subobscuripennis", "Tetragonisca_angustula", 
    "Trigona_corvina"), .rows = structure(list(1L, 2:3, 4:19, 
        20:27, 28:34, 35:39), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -6L), .drop = TRUE))
M--
  • 25,431
  • 8
  • 61
  • 93
LostInR
  • 21
  • 2
  • 1
    Hi @LostInR, Could you please share your data using `dput(trees)`? So we can help you better. – Quinten Mar 29 '22 at 17:51
  • Also, when using `ggplot2` you usually don't need to specify the dataset with `trees$` each time you list a variable - the `data = trees` argument takes care of that. – Andrea M Mar 29 '22 at 17:57

2 Answers2

1

Not sure if this is what you're after, but using size argument within geom_text we can change the font size.

library(ggplot2)
library(ggalluvial)

ggplot(data = trees,
       aes(axis1 = trees$Bee_species, axis2 = trees$Tree_species, y = trees$obs)) +
  geom_alluvium(aes(fill = trees$Bee_species)) +
  geom_stratum(width=0.50) +
  geom_text(stat = "stratum", aes(label = after_stat(stratum)), size= 2.5) +
  scale_x_discrete(limits = c("Bee_species", "Tree_species"),
                   expand = c(0.40, 0.40))+
  theme_void()+
  theme(legend.position = "none") +
  scale_fill_manual(values = c("purple", "green", "blue", "red", "turquoise", "yellow"))

a

M--
  • 25,431
  • 8
  • 61
  • 93
  • 1
    Thanks you! now changing the size worked! Its hard to make this plot look nice since there are so many "1" values :). But its definately more pleasing to look at. – LostInR Mar 29 '22 at 19:03
1

Also you could use geom_label() insted of geom_text() to adjust font size.

Sample code:

library(ggplot2)
library(ggalluvial)
library(ggthemes)


ggplot(trees,aes(axis1 = Bee_species, axis2 = Tree_species, y = obs)) +
        geom_alluvium(aes(fill = Bee_species, color = Tree_species), width = 6/16, knot.pos = 0.3, alpha = 0.5) +
        geom_stratum(width = 6/16, , alpha = 0.5,  color = "black") +
        geom_label(stat = "stratum", aes(label=after_stat(stratum)), fontface = "bold", size = 3) +
  scale_x_discrete(limits = c("Bee species", "Tree species"),expand = c(0.01, 0.01))+
  scale_y_continuous(breaks = NULL)+
  scale_fill_viridis_d() +
  scale_color_viridis_d() +
  theme_hc() +
  theme(axis.text.x = element_text(hjust = 1, face="bold", size=12, color="black"), 
        axis.title.x = element_text(face="bold", size=16, color="black"),
        axis.text.y = element_text(face="bold", size=12, color="black"),
        axis.title.y = element_blank(),
        strip.text = element_text(size=10, face="bold"),
        plot.title = element_text(size=20, face="bold"),
        legend.position = "none")

Plot: enter image description here

Rfanatic
  • 2,224
  • 1
  • 5
  • 21