2016-12-13 10 views
0

제 데이터는 다음과 같습니다. 내가 좋아하는 데이터를 정렬 용융 기능을 사용이박스 플롯을 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 

네 전설 * (초지, 관목 패치, 비 식물 지역 및 산림 지역과 내가 my ggplot로 내 ggplot을 가지고 *. 각 범례 즉 범주에 다섯 개 변수가 있습니다

나는 전설 각각의 변수에 정렬 방식처럼 말아. 내가 순서를 변경하려면 어떻게합니까? 나는 비 식물 우선 지역, 다음 초지, 관목 pathches 마지막 숲 지역에서하고 싶습니다.

+3

당신은 그룹이 유사한 TOL 데이터 $ 이름 = 인자 (데이터 $ 이름, 레벨 = 레벨을 주문 순서를 변경해야합니다 (데이터 $ 이름) [c (1,4,3,2)]) – sb0709

답변

1

factor을 사용하여 명시 적으로 levels 인수의 순서를 설정할 수 있습니다. 기준으로

:

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

<code>iris$Species</code>, default order

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>, reordered

+1

@Beesow,이게 전혀 도움이 되나요? – r2evans