2013-06-13 2 views
3

에 대한 사용자 지정 범례를 추가하는 방법을 제대로 geom_hline 설명하지 않습니다 전설주의 사항 :아래의 데이터를 사용하여 geom_hline

df<-data.frame(
    points=c(.153,.144,.126,.035, .037, .039, .010,.015,.07), 
    days=gl(3,1,9,labels=c("Sun","Mon","Tues")), 
    lang=c("en","en","en","pt","pt","pt","ko","ko","ko")) 

ggplot(data=df[df$lang=="en",])+ 
    geom_point(aes(x=days,y=points),size=5,colour='cyan',show_guide=F)+ 
    geom_point(aes(x=days,y=points,colour=days),size=4,show_guide=F)+ 
    facet_wrap(~lang,ncol=1,scales="free")+ 
    xlab("")+ 
    ylab("")+ 
    scale_y_continuous(labels = percent_format())+ 
    theme(legend.position="right", 
      legend.title = element_blank(), 
      strip.text.x = element_text(size = 13, colour = 'black', angle = 0), 
      axis.text.x=element_text(angle=0, hjust=.5, vjust=0), 
      legend.position = 'none', 
      panel.background = element_rect(fill = "#545454"), 
      panel.grid.major = element_line(colour = "#757575"), 
      panel.grid.minor = element_line(colour = "#757575"))+ 
    geom_hline(yintercept=.136,color='cyan',size=2, show_guide=T) 

enter image description here

이를 설명하는 사용자 정의 전설을 만들 수있는 몇 가지 방법이 있나요을 geom_hline? [전설에 "전설"이라는 이름을 "avg"라는 레이블이 붙어 있습니다.]

이렇게 검색하면 예 : ggplot legend showing transparency and fill color입니다. 그들은 시도한 scale_fill_manual을 사용했지만 위에 표시된 범례를 향상시킬 수 없었습니다.

답변

4

이 조금 어색하지만이 작동하는 것 같다 :

hline <- data.frame(yint = 0.136,lt = 'Avg') 

ggplot(data=df[df$lang=="en",])+ 
    geom_point(aes(x=days,y=points),size=5,colour='cyan')+ 
    geom_point(aes(x=days,y=points,colour=days),size=4)+ 
    facet_wrap(~lang,ncol=1,scales="free")+ 
    xlab("")+ 
    ylab("")+ 
    scale_y_continuous(labels = percent_format())+ 
    theme(legend.position="right", 
      legend.title = element_blank(), 
      strip.text.x = element_text(size = 13, colour = 'black', angle = 0), 
      axis.text.x=element_text(angle=0, hjust=.5, vjust=0), 
      legend.position = 'none', 
      panel.background = element_rect(fill = "#545454"), 
      panel.grid.major = element_line(colour = "#757575"), 
      panel.grid.minor = element_line(colour = "#757575"))+ 
    geom_hline(data = hline,aes(yintercept=yint,linetype = lt),color = "cyan",size=2,show_guide = TRUE) + 
    scale_colour_discrete(guide = "none") + 
    scale_linetype_manual(name = 'Legend',values = 1,guide = "legend") 

enter image description here

아,하지만 당신은 전설의 이름되지 않는 이유 거기에 legend.title = element_blank()을 포함 시켰습니다. 그 이름을 포함하도록 제거하십시오.

관련 문제