2016-11-02 5 views
1

시간이 지남에 따라 국가 별 통계가있는 데이터가 있습니다. 국가별로 플롯하기 위해 facet_wrap()을 사용하지만 가장 최근 값 (2015)을 기준으로 플롯을 내림차순으로 정렬하고 싶습니다. transform()을 사용하려고했지만 첫 번째 값 (2005)에만 해당 주문이 있습니다. 나는 forcats::fct_reorder()가 나를 거기에 데려다 줄 수 있다고 생각하지만 나는 facet_wrap()에 대한 어떤 인자로도 삽입에 성공하지 못했다. 이것은 가능한가? 가능한 경우 grid_arrange()as this question suggests을 수행하지 않으려합니다.forcat :: fct_reorder를 사용하여 facet_wrap 내의 플롯을 정렬하십시오.

Country <- c('a', 'b', 'c', 'x', 'y', 'z') 
`2005` <- c(500, 700, 600, 900, 800, 1000) 
`2010` <- c(900, 600, 800, 1000, 500, 700) 
`2015` <- c(1000, 900, 500, 800, 700, 600) 

df1 <- data.frame(Country, `2005`, `2010`, `2015`) 

gather1 <- gather(df1, "Year", myValue, 2:4) 

gg1 <- ggplot() + 
    geom_line(data = gather1, aes(x = Year, y = myValue, group = Country), size = 1.5, color = "orange") + 
    facet_wrap(~ Country, ncol = 3) + 
    theme(aspect.ratio = (35/50)) 
gg1 

답변

2

여기 있습니다.

gather2 <- df1 %>% 
    mutate(Country=fct_reorder(Country, `2015`)) %>% 
    gather("Year", myValue, 2:4) 

gg2 <- ggplot() + 
    geom_line(data = gather2, aes(x = Year, y = myValue, group = Country), size = 1.5, color = "orange") + 
    facet_wrap(~ Country, ncol = 3) + 
    theme(aspect.ratio = (35/50)) 
gg2 

enter image description here

+0

부야! 그게 ... facet_wrap이 아닌 gather() 함수에 추가하는 것입니다. 감사. – mikeLdub