Add line break to axis labels and ticks in ggplot

后端 未结 5 900
难免孤独
难免孤独 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条回答
  •  Happy的楠姐
    2020-11-29 03:34

    If you don't want a break at each space, you could alternatively use the \n (new line) within the call to scale_x_continuous:

    my.labels <- c("Ambystoma\nmexicanum",
                   "Daubentonia madagascariensis", 
                   "Psychrolutes marcidus") # first create labels, add \n where appropriate.
    
    myplot + 
        scale_x_discrete(labels= my.labels)
    

    Note that only the first name (Ambystoma mexicanum) will break using the new line command (\n).

提交回复
热议问题