Legend ordering in ggplot2

前端 未结 2 1536
别跟我提以往
别跟我提以往 2020-12-18 14:16

I created the following graph in R using ggplot2. As you can see, the order in the legend is not exactly what it should be: I would like to have \"

相关标签:
2条回答
  • 2020-12-18 14:25

    It's changing names into a factor variable and using alphabetical order by default

    use

    df$names <- factor(df$names, levels = c("1 year","4 years","15 years","25 years","40 years"))
    
    0 讨论(0)
  • 2020-12-18 14:29

    You could use fct_inorder from the forcats package to turn the names vector into an ordered factor in your desired "1 year, 4 years", etc sequence:

    library(forcats)
    names = fct_inorder(c(names1,names2,names3,names4,names5))
    
    0 讨论(0)
提交回复
热议问题