簡體   English   中英

如何在R中重新排列箱形圖

[英]How to re-arrange the boxplots in r

我的數據看起來像這樣。 我使用了融化功能來安排這樣的數據

                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表示為 我的ggplot

我不喜歡圖例在每個變量中的排序方式。 如何更改訂單? 我首先要有非植物區,然后是草地,灌木林,最后是森林區。

您可以使用factor ,顯式設置levels參數的順序。

作為基准:

library(ggplot2)
ggplot(iris, aes(Species, Sepal.Length)) + geom_boxplot()

<code> iris $ Species </ code>,默認順序

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()

<code> iris $ Species </ code>,已重新排序

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM