2013-03-12 3 views
4

다음 그래프는 거의 완성되었지만 하나의 문제점이 있습니다.ggplot2는 두 개의 범례를 그립니다.

그래프의 범례는 두 번 그려집니다. 여기

데이터입니다 :

structure(list(Period = c("January 1997 - August 2003", "September 2003 - Jun 2005", 
"Jul 2005 - Dec 2009", "January 1997 - August 2003", "September 2003 - Jun 2005", 
"Jul 2005 - Dec 2009"), Time.Period = structure(c(1L, 3L, 2L, 
1L, 3L, 2L), .Label = c("Jan 1997 - Aug 2003", "Jul 2005 - Dec 2009", 
"Sep 2003 - Jun 2005"), class = "factor"), Variable = structure(c(2L, 
2L, 2L, 1L, 1L, 1L), .Label = c("Significant", "Zscore"), class = "factor"), 
Score = c(8.798129, 4.267268, 7.280275, 1.64, 1.64, 1.64)), .Names = c("Period", 
"Time.Period", "Variable", "Score"), class = "data.frame", row.names = c(NA, 
-6L)) 

ggplot(glomor, aes(x=Time.Period, y=Score, group=Variable, shape=Variable, color=Variable)) + 
geom_point() + 
guides(fill=FALSE) + 
scale_x_discrete(limits=c("Jan 1997 - Aug 2003","Sep 2003 - Jun 2005","Jul 2005 - Dec 2009"), expand=c(.08,0)) + 
    geom_line(aes(linetype=Variable), size=1.5) + 
    geom_point(size=4.2) + 
    scale_linetype_manual(values=c(1,3)) + 
    scale_color_manual(values=c("black", "grey40"), name="", labels=c("Signficant Z-Score", "Moran's I Z-Score")) + 
    scale_fill_discrete(name="", label=c("Signficant Z-Score", "Moran's I Z-Score")) + 
    theme_classic()+ 
    ylim(0,10) + 
    xlab("Time Periods") + 
    ylab("Moran's I Z-Score") + 
    theme(axis.title.x=element_text(size=14)) + 
    theme(axis.title.y=element_text(size=14)) + 
    theme(legend.position=c(.75, .85)) + 
    theme(legend.background = element_rect(fill="white")) + 
    theme(legend.key = element_blank()) 

사람이 알고 있나요 의 ggplot2 두 전설을 생산하는 이유는 무엇입니까?

+0

'dput (your_data)'를 사용하여 출력을 붙여주세요. – Arun

+1

의 'scale_color'에서 축척 이름을'' "'로 설정하지만 색상 및 모양 축척이 하나로 합쳐 지도록 축척 이름을 동일하게 설정해야합니다. – baptiste

+0

또는'guides (guide = "none")도 작동 할 것이라고 생각합니까? –

답변

15

모양, 색상 및 선 종류가 Variable 인 세 가지 미학이 있습니다. 범례는 제목과 레이블이 같은 경우 함께 축소됩니다. 당신은 색깔에 대해 공란으로 제목을 정하고 그것을 커스텀 라벨 ("중요 Z 점수"와 "모란의 I Z 점수")로 지정했습니다. 선이 모두 함께 쓰러 지도록하려면 선 종류와 모양을 변경해야합니다.

변경

scale_linetype_manual(values=c(1,3)) + 

scale_linetype_manual(values=c(1,3), name="", labels=c("Signficant Z-Score", "Moran's I Z-Score")) + 

과 당신이 실제로 어디 채우기 미적를 사용하지 않기 때문에 (당신은 또한 scale_fill_discrete 제거 할 수

scale_shape_discrete(name="", label=c("Signficant Z-Score", "Moran's I Z-Score")) + 

추가 할 수 있습니다.)

이것은 주어진다 enter image description here

+0

정말 고마워요. 이제 그 대답을 볼 수 있습니다, 그것은 정말 직관적으로 의미가 있습니다. – user1738753

관련 문제