R Change Display Order of ggplot2 Plot Legend (Example) | Sort Items Manually Using factor Function

preview_player
Показать описание
R code of this video:

y = 1:5,
group = LETTERS[1:5])

library("ggplot2")

ggp <- ggplot(data, aes(x, y, color = group)) + # Basic ggplot2 plot
geom_point()
ggp # Print plot

data_new <- data # Replicate data

data_new$group <- factor(data_new$group, # Relevel group factor
levels = c("B", "D", "A", "E", "C"))

ggp_new <- ggplot(data_new, aes(x, y, color = group)) + # Recreate plot
geom_point()
ggp_new # Draw updated plot

Follow me on Social Media:
Рекомендации по теме
Комментарии
Автор

Thank you so so much, I was racking my brains to find out a solution and your video was the only thing that got in my brain

gabrieldorozo
Автор

I can't "relevel group factor" for bar plot. The codes used here for point graph does not work for bar plot.

mohammadrahmatullah
visit shbcf.ru