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 \"
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"))
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))