Labelling and theme of ggalluvial plot in R

百般思念 提交于 2019-12-11 02:34:50

问题


For the past hours I have tried to understand the whole design/theme/labelling concept around ggalluvial, but I failed. Within minutes I was able to produce the kind of graph I want (thanks to the package ggalluvial), but I can't figure how to produce the correct labelling/theme I'd like to have.

This is where I am currently:

This is where I was initially:

To get you to understand where I am, here's a reproducible example

I used the following code:

library(ggalluvial)

ds <- as.data.frame(Titanic)

ggplot(ds,
   aes(weight = Freq, axis1 = Sex, axis2 = Class)) +
geom_alluvium(aes(fill = Sex), width = 1/12) +
geom_stratum(width = 1/4, fill = "black", color = "grey") + 
scale_x_continuous(breaks = 1:2, labels = c("Sex", "Class")) + 
scale_fill_manual(name = "", values=c("#A0A0A0", "#494949")) +              
ggtitle("Titanic Survival") +                            
theme_bw() +                                                          
geom_text(stat = "stratum", color="white",label.strata = TRUE,
        angle=c(90,90,0,0,0,0) , size=6, 
        nudge_y=c(1,2,3,4,5,0)) +
theme(legend.position = "bottom",
    title = element_text(size = 20),
    legend.text = element_text(size = 20),
    axis.text.y = element_blank(),
    axis.text.x = element_text(size=20))

What I would like to change:

  • turn labels on the left vertically
    • done
  • make labels on the right not overlap (not visible with the Titanic data)
    • I tried nudging. But it may be more useful to place these legends outside the graph, maybe like on an axis?
  • remove y-axis labels
    • done
  • Understand with what command I can change each of the text elements size (title, legend, labels, axis labels)
    • dome

I am very happy for any help on this. Thank you.


回答1:


You can try:

ggplot(ds,
       aes(weight = Freq, axis1 = Sex, axis2 = Class)) +
  geom_alluvium(aes(fill = Sex), width = 1/12) +
  geom_stratum(width = 1/16, fill = "black", color = "grey") + 
  scale_x_continuous(breaks = 1:2, labels = c("Sex", "Class")) + 
  scale_fill_manual(name = "", values=c("#A0A0A0", "#494949")) +              
  ggtitle("Titanic Survival") +                            
  theme_bw() +                                                          
  geom_text(stat = "stratum", color="red",label.strata = TRUE, angle=c(0,0,45,45,45,45),size=5) +              
  theme(legend.position = "bottom",
        title = element_text(size = 20),
        legend.text = element_text(size = 20),
        axis.text.y = element_blank(),
        axis.text.x = element_text(size=20))

  • add a vector with the same length of labels like angle=c(0,0,45,45,45,45)
  • add axis.text.y = element_blank() in theme()
  • pending
  • Sizes via theme
    • via theme() such as x-axis labels
    • text in plot via size=5 in geom_text()


来源:https://stackoverflow.com/questions/50720718/labelling-and-theme-of-ggalluvial-plot-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!