2017-02-17 1 views
1

나는 아래 그림과 같이 파란색 -> 녹색 -> 빨강에서 r-> g-> b로 색칠 된 선의 순서를 변경하고 싶습니다. 전설의 명령과 동일합니다. 전설의 순서를 변경하는 데 필요한 몇 가지 자습서를 찾았으나이 경우 순서를 유지하려고합니다 (1, 2 및 3이므로). 여기 ggplot2 : '색상'의 순서 변경

Figure

데이터도 생성 할 수있는 코드이다.

population_num <- 100 
population <- tibble(
    gender = as.numeric(rbinom(population_num, 1, 0.5)), 
    age=rnorm(population_num, mean=50, sd=20), 
    score=rnorm(population_num, mean=80, sd=30), 
    setid=sample(c(1,2,3), size=population_num, replace=T) 
    ) 
temp <- population %>% 
    group_by(setid) %>% 
    do(model1 = tidy(lm(score ~ age, data = .)), 
    model2 = tidy(lm(score ~ age + gender, data = .))) %>% 
    gather(model_name, model, -setid) %>%      
    unnest() %>%            
    filter(term == "age")          

interval1 <- -qnorm((1-0.9)/2) 

ggplot(temp, aes(colour = as.factor(setid))) + 
    geom_hline(yintercept = 0, colour = gray(1/2), lty = 2) + 
    geom_linerange(aes(x = model_name, ymin = estimate - std.error*interval1, 
        ymax = estimate + std.error*interval1), 
       lwd = 1, position = position_dodge(width = 1/2)) + 
scale_x_discrete(limits=c("model2", "model1"), labels=c("M2", "M1")) + 
    coord_flip() 

는 (이 질문은 한 번 일본어 유래에 요구하고 있지만 답변을 얻을 수 없었다.)

+0

패키지에서'tidy' 함수이 경고를 생성합니까? 나는 이미 그것을 보았을 것이라고 확신하지만,'tidyverse'는 결과가 없습니다. – GGamba

+0

@G 감바는 '빗자비'여야합니다. – thepule

+4

'position'을'position_dodge (width = -1/2)'로 변경하십시오. – alistaire

답변

1

당신은 부정적인에 position_dodgewidth 매개 변수를 변경할 수 있습니다. 잘

Warning message: 
position_dodge requires non-overlapping x intervals 

하지만 플롯 :

ggplot(temp, aes(colour = as.factor(setid))) + 
    geom_hline(yintercept = 0, colour = gray(1/2), lty = 2) + 
    geom_linerange(aes(x = model_name, ymin = estimate - std.error*interval1, 
         ymax = estimate + std.error*interval1), 
        lwd = 1, position = position_dodge(width = -1/2)) + 
    scale_x_discrete(limits=c("model2", "model1"), labels=c("M2", "M1")) + 
    coord_flip()