2013-06-17 3 views
45

이 플롯에 대한 범례를 수동으로 설정하는 방법을 알 수 없습니다. 제가 정말로 원했던 것은 3 가지 색을 사용하고 각각 옆에 이름이있는 오른쪽의 단순한 전설입니다.복잡한 플롯에 대한 수동 범례 만들기

enter image description here

현재의 코드는 다음과 같습니다

a <-c("S1","S2","S3","S4","S5","S6","S7","S8","S9") #names 
b <-c(0.23,0.26,0.55,0.56,0.36,0.23,0.18,0.06,0.04) #mean t0 
c <-c(0.64,0.6,0.81,1.4,0.89,0.55,0.48,0.22,0.09) #mean t1 
d <-c(0.20,0.23,0.52,0.53,0.33,0.20,0.15,0.04,0.03) #SD low t0 
e <-c(0.26,0.29,0.58,.59,0.39,0.26,0.21,0.08,0.05) #SD high t0 
f <-c(0.67,0.63,0.86,1.44,0.93,0.59,0.51,0.25,0.10) #SD high t1 
g <-c(0.61,0.57,0.78,1.36,0.85,0.53,0.45,0.19,0.08) #SD low t1 
h <-c(0.41,0.34,0.26,0.84,0.53,0.32,0.30,0.16,0.05) #absolute change 

data <- data.frame(a,b,c,d,e,f,g,h) 

ggplot(data=data,aes(a)) + 
    geom_bar(stat="identity", aes(y=h),fill="#62c76b",colour="#333333")+ #green 
    geom_line(aes(y=b,group=1),size=1.0,colour="#f04546") + #red 
    geom_point(aes(y=b),size=3, colour="#f04546") +   #red 
    geom_errorbar(aes(ymin=d, ymax=e), colour="#f04546", width=0.1, size=.8) + 
    geom_line(aes(y=c,group=1),size=1.0,colour="#3591d1") + #blue 
    geom_point(aes(y=c),size=3, colour="#3591d1") +   #blue 
    geom_errorbar(aes(ymin=f, ymax=g), colour="#3591d1", width=0.1, size=.8) + 
    ylab("Symptom severity") + xlab("PHQ-9 symptoms") + 
    ylim(0,1.6) + 
    theme_bw() + 
    theme(axis.title.x = element_text(size = 15, vjust=-.2)) + 
    theme(axis.title.y = element_text(size = 15, vjust=0.3)) 
+3

매우 도움이 ? – Torvon

답변

63

당신은 전설을 생산하는 (AES는 문 내의 색) 미학에 속성을 매핑해야합니다.

cols <- c("LINE1"="#f04546","LINE2"="#3591d1","BAR"="#62c76b") 
ggplot(data=data,aes(x=a)) + 
    geom_bar(stat="identity", aes(y=h, fill = "BAR"),colour="#333333")+ #green 
    geom_line(aes(y=b,group=1, colour="LINE1"),size=1.0) + #red 
    geom_point(aes(y=b, colour="LINE1"),size=3) +   #red 
    geom_errorbar(aes(ymin=d, ymax=e, colour="LINE1"), width=0.1, size=.8) + 
    geom_line(aes(y=c,group=1,colour="LINE2"),size=1.0) + #blue 
    geom_point(aes(y=c,colour="LINE2"),size=3) +   #blue 
    geom_errorbar(aes(ymin=f, ymax=g,colour="LINE2"), width=0.1, size=.8) + 
    scale_colour_manual(name="Error Bars",values=cols) + scale_fill_manual(name="Bar",values=cols) + 
    ylab("Symptom severity") + xlab("PHQ-9 symptoms") + 
    ylim(0,1.6) + 
    theme_bw() + 
    theme(axis.title.x = element_text(size = 15, vjust=-.2)) + 
    theme(axis.title.y = element_text(size = 15, vjust=0.3)) 

enter image description here

내가 롤랜드는 어디에서 오는 이해하지만, 이것은 단지 3 속성, 그리고 합병증이 중첩 바, 오차 막대에서 발생하기 때문에이 그것과 같은 다양한 형식의 데이터를 떠나 합리적인 될 수있다 입니다. 복잡성이 다소 줄어들 수 있습니다 using geom_pointrange.


원래의 오차 막대 전설의 배경 색상을 변경 플롯 사양에 + theme(legend.key = element_rect(fill = "white",colour = "white"))를 추가합니다. 다른 범례를 병합하려면 일반적으로 모든 요소에 대해 일관된 매핑이 필요하지만 현재는 나를 위해 검은 배경의 아티팩트를 생성하고 있습니다. 나는 guide = guide_legend(fill = NULL,colour = NULL)이 전설에 대해 null로 배경을 설정할 것이라고 생각했지만 그렇지 않았습니다. 아마도 또 다른 질문의 가치가있을 것입니다.

enter image description here


ggplot(data=data,aes(x=a)) + 
    geom_bar(stat="identity", aes(y=h,fill = "BAR", colour="BAR"))+ #green 
    geom_line(aes(y=b,group=1, colour="LINE1"),size=1.0) + #red 
    geom_point(aes(y=b, colour="LINE1", fill="LINE1"),size=3) +   #red 
    geom_errorbar(aes(ymin=d, ymax=e, colour="LINE1"), width=0.1, size=.8) + 
    geom_line(aes(y=c,group=1,colour="LINE2"),size=1.0) + #blue 
    geom_point(aes(y=c,colour="LINE2", fill="LINE2"),size=3) +   #blue 
    geom_errorbar(aes(ymin=f, ymax=g,colour="LINE2"), width=0.1, size=.8) + 
    scale_colour_manual(name="Error Bars",values=cols, guide = guide_legend(fill = NULL,colour = NULL)) + 
    scale_fill_manual(name="Bar",values=cols, guide="none") + 
    ylab("Symptom severity") + xlab("PHQ-9 symptoms") + 
    ylim(0,1.6) + 
    theme_bw() + 
    theme(axis.title.x = element_text(size = 15, vjust=-.2)) + 
    theme(axis.title.y = element_text(size = 15, vjust=0.3)) 

는 전설의 검은 배경을 제거하려면 guide_legendoverride.aes 인수를 사용해야합니다. 이 기능의 목적은 올바르게 할당되지 않은 범례의 특정 부분을 지정하게하는 것입니다. 내가 문서와 튜토리얼없는 경우의 코드를 것 -

ggplot(data=data,aes(x=a)) + 
    geom_bar(stat="identity", aes(y=h,fill = "BAR", colour="BAR"))+ #green 
    geom_line(aes(y=b,group=1, colour="LINE1"),size=1.0) + #red 
    geom_point(aes(y=b, colour="LINE1", fill="LINE1"),size=3) +   #red 
    geom_errorbar(aes(ymin=d, ymax=e, colour="LINE1"), width=0.1, size=.8) + 
    geom_line(aes(y=c,group=1,colour="LINE2"),size=1.0) + #blue 
    geom_point(aes(y=c,colour="LINE2", fill="LINE2"),size=3) +   #blue 
    geom_errorbar(aes(ymin=f, ymax=g,colour="LINE2"), width=0.1, size=.8) + 
    scale_colour_manual(name="Error Bars",values=cols, 
         guide = guide_legend(override.aes=aes(fill=NA))) + 
    scale_fill_manual(name="Bar",values=cols, guide="none") + 
    ylab("Symptom severity") + xlab("PHQ-9 symptoms") + 
    ylim(0,1.6) + 
    theme_bw() + 
    theme(axis.title.x = element_text(size = 15, vjust=-.2)) + 
    theme(axis.title.y = element_text(size = 15, vjust=0.3)) 

enter image description here

+0

Andy, 대단히 감사합니다. (1) 2 줄과 1 줄 범례를 "병합"할 수있는 방법이 있습니까? (2) 수동으로 범례를 변경하는 방법이 있습니까? 예 : LINE1과 LINE2 주변의 회색 테두리의 색상? 나는 ggplot2 문서에서 모든 것을 찾지 못했다. – Torvon

+0

@ Torvon, 범례를 병합하려면 일반적으로 각 기하 구조에 대해 일관된 매핑이 필요합니다. 예를 들어 업데이트 하겠지만 이상한 아티팩트가 생깁니다 (라인에 내부가 채워지지 않았기 때문일 수 있습니다). 그것은 당신의 취향에 맞게 수정 될 수 있는지 알아 보는 것이 다른 질문 일 가치가 있습니다. 나는 다른 전설이 좋은 생각이라고 생각하지 않지만 나는 말할 것이다. 그것은 2 개의 전설 IMO로 꽤 간단합니다. –

관련 문제