我的数据是这样的。我使用melt函数将数据排列如下
Legend variable value
1 Grassland NDVI 0.139
2 Grassland NDVI 0.285
3 Grassland NDVI 0.134
4 Grassland NDVI 0.243
5 Grassland NDVI 0.113
6 Grassland NDVI 0.144
7 Grassland NDVI 0.212
8 Grassland NDVI 0.249
9 Grassland NDVI 0.231
10 Grassland NDVI 0.192
11 Grassland NDVI 0.159
12 Grassland NDVI 0.146
13 Grassland NDVI 0.177
14 Grassland NDVI 0.287
15 Grassland NDVI 0.240
16 Grassland NDVI 0.285有四个传说*(草原、灌木斑块、非植被区和林区),每个传说中有五个变量,即类别*。我得到了我的ggplot

我不喜欢图例在每个变量中排序的方式。如何更改顺序?我希望首先是非植被区,然后是草地,灌木丛小路,最后是林区。
发布于 2016-12-14 10:33:48
您可以使用factor,显式设置levels参数的顺序。
作为基准:
library(ggplot2)
ggplot(iris, aes(Species, Sepal.Length)) + geom_boxplot()

df <- iris
levels(df$Species)
# [1] "setosa" "versicolor" "virginica"
df$Species <- factor(df$Species, levels = levels(df$Species)[c(3,1,2)])
ggplot(df, aes(Species, Sepal.Length)) + geom_boxplot()

https://stackoverflow.com/questions/41132511
复制相似问题