2017-11-29 1 views
-1

오름차순으로 x 축을 정렬 할 수 없습니다. 나는 아래에 언급 된 코드를 사용해 보았지만 작동하지 않는다. x 축을 오름차순으로 정렬하려면 어떻게해야합니까?X 축을 R에서 정렬 할 수 없습니다

mydata$Quarter <- reorder(mydata$Quarter, mydata$Fund) 
ggplot(data=mydata1,aes(Quarter))+ 
    geom_point(aes(x = Quarter, y = Ratio, color = Fund))+ 
    theme(axis.text.x = element_text(angle = 65, hjust = 1)) 
+1

당신은'dput (mydata)'를 콘솔에 입력하고 출력을 질문에 추가함으로써 데이터'mydata'를 제공 할 수 있습니까? – brettljausn

+0

> dput (mydata) 구조 (목록 (기금 = 구조 (c (2L, 1L), 레이블 = c ("기금 XI-A, LP", "기회 자금, LP" (1 : 2, .Label = c ("2013-Q3", "2017-Q1"), 클래스 = "인수"), Q1 " "Q12 "), 클래스 ="인자 "), 약속 = 구조 (1 : 2, .Label의 = C를 ("$ 20,000,000 " "$ 35,000,000 "), 클래스 ="인자 "), NAV = 구조 (1 : 2, .Label의 = C를 ("$ 247,764" "$ 4천88만8천84"), 클래스 = "인자") 비율 = C를 (0.01, 1.17)), .Names의 = C를 ("기금" "날짜" – gurtej

+0

무엇 @brettljausn 수단 "분기", "투입", "탐색", "비"), 클래스 = "data.frame"row.names에서의 C = (NA, -2L))는 데이터의 작은 샘플이고 'mydata'. 그래서 우리는 당신의 코드를 실행할 수 있습니다 .. – Dan

답변

0

여기서 핵심은 데이터 포인트를 플롯하기 전에, 당신의 요인 변수 Quarter 순서를 변경하는 방법을 찾는 것입니다. 이 도움이 될 것입니다보다 일반적인 솔루션으로

library(ggplot2) 

# example data 
df = structure(list(Fund = structure(c(2L, 1L), .Label = c(" Fund XI-A, L.P.", "Opportunity Fund, LP"), class = "factor"), 
        Date = structure(1:2, .Label = c("2013-Q3", "2017-Q1"), class = "factor"), 
        Quarter = structure(1:2, .Label = c("Q1", "Q12"), class = "factor"), 
        Commitment = structure(1:2, .Label = c("$20,000,000", "$35,000,000"), class = "factor"), 
        NAV = structure(1:2, .Label = c("$247,764", "$40,888,084"), class = "factor"), 
        Ratio = c(0.01, 1.17)), .Names = c("Fund", "Date", "Quarter", "Commitment", "NAV", "Ratio"), class = "data.frame", row.names = c(NA, -2L)) 

# keep the order of your factor variable as it is 
ggplot(data=df,aes(Quarter))+ 
    geom_point(aes(x = Quarter, y = Ratio, color = Fund))+ 
    theme(axis.text.x = element_text(angle = 65, hjust = 1)) 

# change the order and plot again 
df$Quarter <- reorder(df$Quarter, 2:1) 

ggplot(data=df,aes(Quarter))+ 
    geom_point(aes(x = Quarter, y = Ratio, color = Fund))+ 
    theme(axis.text.x = element_text(angle = 65, hjust = 1)) 

(즉, 당신은 2 인자 수준이 없을 때) 당신이 순서를 df$Quarter <- reorder(df$Quarter, length(df$Quarter):1)를 사용할 수 있습니다.

관련 문제