0

If you consider this code:

library(ggplot2)
df <- as.data.frame(matrix(rnorm(8),4,2)) 
colnames(df) <- c("x","y") 
df$dates <- as.factor(c("april", "may", "june", "august"))

ggplot(df, aes(x=x, y=y, color=dates)) + geom_point(size=3, shape=20)

How can I sort the legend by the temporal sequence of the months?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
nouse
  • 3,315
  • 2
  • 29
  • 56
  • possible duplicate of [ggplot legends - change labels, order and title](http://stackoverflow.com/questions/12075037/ggplot-legends-change-labels-order-and-title) – Henrik Oct 08 '14 at 13:35
  • hello. why not use the 'levels" argument of the 'factor' function? df$dates <- factor(df$dates, levels=df$dates[c(4,1,3,2)]) – agenis Oct 08 '14 at 13:48

1 Answers1

2

Replace the df$dates<- line with:

m <- c("april", "may", "june", "august")
df$dates <- factor(m, levels = m)
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341