Add line break to axis labels and ticks in ggplot

后端 未结 5 909
难免孤独
难免孤独 2020-11-29 03:10

I\'m looking for a way to use long variable names on the x axis of a plot. Of course I could use a smaller font or rotate them a little but I would like keep them vertical a

5条回答
  •  星月不相逢
    2020-11-29 03:38

    You can add your own formatter ( see scales package for more examples). Here I replace any space in your x labels by a new line.

    addline_format <- function(x,...){
        gsub('\\s','\n',x)
    }
    
    myplot + 
        scale_x_discrete(breaks=unique(df_M$variable), 
        labels=addline_format(c("Ambystoma mexicanum", 
                            "Daubentonia madagascariensis", "Psychrolutes marcidus")))
    

    enter image description here

提交回复
热议问题