2017-12-31 8 views
2

에 따라 플롯을 주문하는하지만 결과는 내가 오름차순으로 fat 변수에 따라 주문하고 싶습니다 매우 지저분하다.

airlines1 <- data.frame(airline = rownames(airlines), fat, row.names = NULL) 
airlines2 <- factor(airlines1$airline, 
        levels = airlines[order(airlines$fat),"fat"]) 
ggplot(airlines2, aes(y = airline, x = fat)) + 
    geom_point(stat = "identity") 

을하지만 두 가지 오류 얻을 :

나는 시도

&

"Error: ggplot2 doesn't know how to deal with data of class factor" 

"Error: Column `fat` not found" 
내가 그것을 어떻게 주문해야을? dplyrggplot2

dput(airlines) 
structure(list(airline = c("Aer Lingus", "Aeroflot*", "Aerolineas Argentinas", 
"Aeromexico*", "Air Canada", "Air France", "Air India*", "Air New Zealand*", 
"Alaska Airlines*", "Alitalia", "All Nippon Airways", "American*", 
"Austrian Airlines", "Avianca", "British Airways*", "Cathay Pacific*", 
"China Airlines", "Condor", "COPA", "Delta/Northwest*", "Egyptair", 
"El Al", "Ethiopian Airlines", "Finnair", "Garuda Indonesia", 
"Gulf Air", "Hawaiian Airlines", "Iberia", "Japan Airlines", 
"Kenya Airways", "KLM*", "Korean Air", "LAN Airlines", "Lufthansa*", 
"Malaysia Airlines", "Pakistan International", "Philippine Airlines", 
"Qantas*", "Royal Air Maroc", "SAS*", "Saudi Arabian", "Singapore Airlines", 
"South African", "Southwest Airlines", "Sri Lankan/AirLanka", 
"SWISS*", "TACA", "TAM", "TAP - Air Portugal", "Thai Airways", 
"Turkish Airlines", "United/Continental*", "US Airways/America West*", 
"Vietnam Airlines", "Virgin Atlantic", "Xiamen Airlines"), avseatkm = c(320906734, 
1197672318, 385803648, 596871813, 1865253802, 3004002661, 869253552, 
710174817, 965346773, 698012498, 1841234177, 5228357340, 358239823, 
396922563, 3179760952, 2582459303, 813216487, 417982610, 550491507, 
6525658894, 557699891, 335448023, 488560643, 506464950, 613356665, 
301379762, 493877795, 1173203126, 1574217531, 277414794, 1874561773, 
1734522605, 1001965891, 3426529504, 1039171244, 348563137, 413007158, 
1917428984, 295705339, 682971852, 859673901, 2376857805, 651502442, 
3276525770, 325582976, 792601299, 259373346, 1509195646, 619130754, 
1702802250, 1946098294, 7139291291, 2455687887, 625084918, 1005248585, 
430462962), inc1 = c(2, 76, 6, 3, 2, 14, 2, 3, 5, 7, 3, 21, 1, 
5, 4, 0, 12, 2, 3, 24, 8, 1, 25, 1, 10, 1, 0, 4, 3, 2, 7, 12, 
3, 6, 3, 8, 7, 1, 5, 5, 7, 2, 2, 1, 2, 2, 3, 8, 0, 8, 8, 19, 
16, 7, 1, 9), fatacc1 = c(0, 14, 0, 1, 0, 4, 1, 0, 0, 2, 1, 5, 
0, 3, 0, 0, 6, 1, 1, 12, 3, 1, 5, 0, 3, 0, 0, 1, 1, 0, 1, 5, 
2, 1, 1, 3, 4, 0, 3, 0, 2, 2, 1, 0, 1, 1, 1, 3, 0, 4, 3, 8, 7, 
3, 0, 1), fat1 = c(0, 128, 0, 64, 0, 79, 329, 0, 0, 50, 1, 101, 
0, 323, 0, 0, 535, 16, 47, 407, 282, 4, 167, 0, 260, 0, 0, 148, 
520, 0, 3, 425, 21, 2, 34, 234, 74, 0, 51, 0, 313, 6, 159, 0, 
14, 229, 3, 98, 0, 308, 64, 319, 224, 171, 0, 82), inc2 = c(0, 
6, 1, 5, 2, 6, 4, 5, 5, 4, 7, 17, 1, 0, 6, 2, 2, 0, 0, 24, 4, 
1, 5, 0, 4, 3, 1, 5, 0, 2, 1, 1, 0, 3, 3, 10, 2, 5, 3, 6, 11, 
2, 1, 8, 4, 3, 1, 7, 0, 2, 8, 14, 11, 1, 0, 2), fatacc2 = c(0, 
1, 0, 0, 0, 2, 1, 1, 1, 0, 0, 3, 0, 0, 0, 0, 1, 0, 0, 2, 1, 0, 
2, 0, 2, 1, 0, 0, 0, 2, 0, 0, 0, 0, 2, 2, 1, 0, 0, 1, 0, 1, 0, 
0, 0, 0, 1, 2, 0, 1, 2, 2, 2, 0, 0, 0), fat2 = c(0, 88, 0, 0, 
0, 337, 158, 7, 88, 0, 0, 416, 0, 0, 0, 0, 225, 0, 0, 51, 14, 
0, 92, 0, 22, 143, 0, 0, 0, 283, 0, 0, 0, 0, 537, 46, 1, 0, 0, 
110, 0, 83, 0, 0, 0, 0, 3, 188, 0, 1, 84, 109, 23, 0, 0, 0), 
    model = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", 
    "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", 
    "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", 
    "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", 
    "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", 
    "51", "52", "53", "54", "55", "56")), .Names = c("airline", 
"avseatkm", "inc1", "fatacc1", "fat1", "inc2", "fatacc2", "fat2", 
"model"), row.names = c(NA, -56L), class = c("tbl_df", "tbl", 
"data.frame")) 

enter image description here

+1

오류 메시지의 텍스트를 기반으로 디버깅 프로세스의 일부로 스스로에게 물어볼 수있는 간단한 두 가지 : (1)'head (airlines1) '을보십시오. 변수는 '뚱뚱한가?'입니까? 그렇지 않다면 왜 안 되겠습니까? (2)'airlines2'는 데이터 프레임입니까? 왜냐하면'ggplot'은 데이터 프레임을 기대하기 때문입니다. – joran

+1

언제나처럼 ...요인 수준을 주문하십시오. –

답변

2

:

내가 분석하고 데이터입니다

library(ggplot2) 
library(dplyr) 

airlines %>% 
    select(airline, fat1, fat2) %>% 
    mutate(fat = fat1 + fat2) %>% 
    ggplot(aes(fat, reorder(airline, fat))) + 
    geom_point(stat = "identity") + 
    labs(y = "airline", x = "fatalities") 

enter image description here

당신은 순서가 반대 할 경우, 당신이 할 수있는 수정 fat-fat에 :

airlines %>% 
    select(airline, fat1, fat2) %>% 
    mutate(fat = fat1 + fat2) %>% 
    ggplot(aes(fat, reorder(airline, -fat))) + 
    geom_point(stat = "identity") + 
    labs(y = "airline", x = "fatalities") 

enter image description here

+0

mtcars의 코드가이 데이터와 작동하지 않아서 내 데이터 세트의 dput으로 게시물을 업데이트했습니다. – Kas

+1

알겠습니다. 내 대답을 수정했습니다. – tyluRp

+0

'aes()'를 호출 할 때이 모든 작업을 수행 할 수있는 동안'dplyr'을 사용하여 기본 데이터를 조작하면 어떤 이점이 있는지 이해할 수있게 도와 주시겠습니까? – Uwe

0

dplyr를로드하거나 기본 airlines data.frame을 조작 할 이유가 없다. reorder()

library(ggplot2) 
ggplot(airlines, aes(y = reorder(airline, fat1 + fat2), x = fat1 + fat2)) + 
    geom_point() + xlab("Fatalities") + ylab(NULL) 

통화가 요인 수준이 fat1 + fat2의 값을 증가시켜 정렬 위치를 고려하는 airline 강제 변환 :를 온 - 더 - 플라이이 aes()에 호출에 간결하게 수행 할 수 있습니다.

enter image description here

요인을 취급, 나는 해들리 위컴의 forcats 패키지는 매우 유용하게 찾을 수 있습니다.

ggplot(airlines, aes(y = forcats::fct_reorder(airline, fat1 + fat2, .desc = TRUE), 
        x = fat1 + fat2)) + 
    geom_point() + xlab("Fatalities") + ylab(NULL) 

enter image description here

내가 개인적으로 -1에 의해 기본 R의 reorder() 함수의 X 매개 변수를 곱하면하는 것보다이 더 투명 찾아 다음 fct_reorder()는 명시 적 요인 수준의 순서를 반대로하는 데 사용할 수있는 .desc 매개 변수가 예 : reorder(airline, -(fat1 + fat2)). 그러나 마일리지는 다를 수 있습니다.