df_ex <- read.table("df_example.txt", header=TRUE, sep=" ")
where df_example.txt has the contents of:
name_age age distance
Stu_27 27 6.28
Stu_27 27 5.66
Stu_27 27 5.25
Ben_22 22 3.85
Ben_22 22 4.01
Ben_22 22 4.22
Liz_40 40 3.99
Liz_40 40 4.21
Liz_40 40 4.65
then
ggplot(df_ex, aes(x = distance, y = name_age)) + geom_density_ridges(scale = 1.5, fill="lightblue", rel_min_height=0.00001)
How can I order the y-axis by age while still showing the y-axis labels with "name_age" format? The order from bottom to top should be Ben_22 Stu_27 Liz_40
I tried to apply a factor following the ggplot documentation, however when I performed the command
ggplot(df_ex, aes(x = distance, y = factor(name_age, levels=age))) + geom_density_ridges(scale = 1.5, fill="lightblue", rel_min_height=0.00001)
I received an error
Error in `levels<-`(`*tmp*`, value = as.character(levels)) :
factor level [2] is duplicated