corybrunson / ggalluvial

ggplot2 extension for alluvial plots
http://corybrunson.github.io/ggalluvial/
GNU General Public License v3.0
497 stars 34 forks source link

rotating labels in the axes doesn't seem to work? #119

Closed plijnzaad closed 10 months ago

plijnzaad commented 1 year ago

I'm trying to rotate the labels in the axes in order to cram more information in it (e.g. the percentages of the counts per stratum in that axis). I intend to rename the actual data for this (surely there must be a more elegant way? I.e. lookup or calculate the percentage string on the fly??) but there is not enough room, so rotating the text could solve that. The code I have is:

g <- ggplot(as.data.frame(d), # wide format
       aes(y = n, axis1 = k2, axis2 = k3, axis3=k4)) +
    geom_alluvium(
                  aes(fill= .data[[ expected_k ]] ), # color by the genotype in a particular axis
                  width = 1/12,
                  show.legend=TRUE) +
    scale_fill_manual(values=alluv_colrs) +
    geom_stratum(width = 1/12, 
                 alpha=0.7,
                 fill = "white", color='grey40') +
  geom_label(stat = "stratum",
             aes(label = after_stat(stratum)),
             size=3,
             angle=90, # <-- this should work?
             color='black',
             alpha=0, 
             label.size=NA
             ) +
  scale_x_continuous(breaks = 1:length(kks), labels = kks) +
  ggtitle(title) + theme_pubr()
show(g)

which doesn't seem to work, it gives this:

image

I would like the first axis to have something like '0 (49.4%)', '1 (47.7%)', 'dbl (2.6%)' and 'unk (0.3%)' plotted vertically (OK, those last two won't fit) and likewise for the other axes. What am I doing wrong? This is R version 4.2.3 on Mac, ggalluvial version 0.12.5.

Kind regards,

Philip

corybrunson commented 1 year ago

Hi @plijnzaad, have you tried using geom_text() rather than geom_label()? The documentation (help(geom_label)) indicates that the angle aesthetic is only supported by the 'text' geom.

plijnzaad commented 1 year ago

yes, geom_text works, many thanks! Is there a way to make the rotation angle of 'unk' and 'dbl' different from the rest? I can't get my head around this, to be honest :-(

corybrunson commented 1 year ago

OK, cool. To rotate different elements differently, i think you'll need to either (a) create a variable in the data set indicating which ones to rotate differently and pass that variable to angle in aes() or (b) create a standalone vector of angles and pass it to angle outside aes(). See the geom_text and related ggplot2 documentation for any more details on that!

corybrunson commented 10 months ago

@plijnzaad i assume that this is resolved, at least insofar as i can help, but please do reopen this issue if i is not!