按因子排序ggplot图例(此处:月份)

时间:2014-10-08 13:33:26

标签: r ggplot2 r-factor

如果您考虑以下代码:

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)

如何按照月份的时间顺序对图例进行排序?

1 个答案:

答案 0 :(得分:2)

df$dates<-行替换为:

m <- c("april", "may", "june", "august")
df$dates <- factor(m, levels = m)