2014-09-18 3 views
2

두 그룹 간의 차이점을 geom_point 및 errorbars로 증명하고 geom_line로 연결하여 어떤 그룹이 연결되어 있는지 쉽게 파악할 수 있습니다. 범례에는 2 개의 패널이 자동으로 포함됩니다 (하나는 점, 다른 하나는 선). 나는 "범례 결합"과 "중복 범례"및 기타 많은 것들을 검색하여 1-2 시간 정도 가깝지만 문제를 해결할 수없는 것으로 보입니다. 포인트 및 라인 유형을 모두 보여주는 단일 범례를 원합니다. 그러나 전설을 삭제하고 포인트를 표시하는 것으로 해결할 것입니다. 코드가 실행되는 한, 나는 초보자입니다. 따라서보다 효율적인 방법을 발견하면 배울 수있어서 기쁩니다. 도움을 주셔서 감사합니다.ggplot geom_point 및 geom_line 플롯으로 생성 된 중복 범례 결합

dat <- structure(list(pop = structure(c(1L, 1L, 2L, 2L, 3L, 3L), .Label = c("A", 
"B", "C"), class = "factor"), foundlost = structure(c(1L, 2L, 
1L, 2L, 1L, 2L), .Label = c("Found", "Lost"), class = "factor"), 
mean = c(3.939, -7.153, 3.001, -4.089, 4.247, -5.168), sd = c(4.394, 
9.08, 4.627, 6.644, 5.346, 6.592), Freq = c(118L, 65L, 109L, 
80L, 202L, 166L), error = c(0.792806386453774, 2.20738080265164, 
0.868629033901147, 1.4559039398977, 0.737227217957054, 1.00279288913615 
), Edn = c(3.14619361354623, -9.36038080265164, 2.13237096609885, 
-5.5449039398977, 3.50977278204295, -6.17079288913615), Eup = c(4.73180638645377, 
-4.94561919734836, 3.86962903390115, -2.6330960601023, 4.98422721795705, 
-4.16520711086385)), .Names = c("pop", "foundlost", "mean", 
"sd", "Freq", "error", "Edn", "Eup"), class = "data.frame", row.names = c(NA, -6L)) 
dat 

attach(dat) 
require(ggplot2) 
pd <- position_dodge(.2) 


mainplot <- ggplot(data=dat, aes(x=foundlost, y=mean, ymin=Edn, ymax=Eup, linetype=NULL, group=pop)) + 
geom_point(aes(shape=pop), size=4, position=pd)+ 
geom_errorbar(width=0.2, size=1.25, position=pd)+ 
geom_line(position=pd, size=1.25, aes(group=pop, linetype=pop)) 

phase2 <- mainplot + 
scale_shape_discrete(name="", labels= c("ONE", "TWO", "TREE"))+ 
scale_x_discrete(expand=c(0.1,0.1)) + 
labs(title="Mean standardized differences", 
     y="Deviation from population mean", 
     x="") 

phase2 + 
theme(plot.title=element_text(size=rel(1.5)), 
panel.background=element_blank(), 
axis.line=element_line(1.5), 
panel.grid=element_blank(), 
axis.text=element_text(colour='black', size=14), 
axis.title.y=element_text(colour='black', size=14), 
axis.ticks=element_line(colour='black', size=1), 
legend.position=c(.2,.25), 

complete=FALSE) 

최종 지점에 대한 편집 : 나는 이것이 내 AES를 설정 한 방식() 또는 무언가와 일치하지 않는 사실과 관련이있다 나의 독서에서 실현하지만, 모든 것이 내가 ' 시도한 것이 지금까지 실패 했으므로 ggroup에 대한 내 쿼리.

답변

1

전설에는 여러 가지 일이있었습니다.

먼저 전체 음모에 linetype = NULL 인수로 인해 두 개의 전설이 나타났습니다 aes. 제거하면 처음에는 두 개의 전설을 없앨 수 있습니다. 그런 다음 scale_shape_discrete을 사용하여 두 번째 범례가 다시 추가됩니다. 이 방법으로 범례 레이블을 변경하고 하나의 범례 만 사용하려면 scale_shape_discretescale_linetype_discrete을 모두 사용해야합니다. 이것에 대한 좋은 예가 페이지의 Cookbook for R website입니다.

전설 레이블을 변경하는 가장 쉬운 방법 중 하나는 플롯하기 전에 데이터 세트에서 인자 pop의 레벨을 설정하는 것입니다. 당신이 결합 된 범례를 사용하는 경우

levels(dat$pop) = c("ONE", "TWO", "THREE") 

, 당신은 내가 아래에 보여 당신이 guide_legendkeywidthguides에서 할 수있는,하지만 더 선 종류를 표시하기 위해 전설의 폭을 변경할 수 있습니다.

ggplot(data=dat, aes(x=foundlost, y=mean, ymin=Edn, ymax=Eup, group=pop)) + 
    geom_point(aes(shape=pop), size=4, position=pd) + 
    geom_errorbar(width=0.2, size=1.25, position=pd) + 
    geom_line(position=pd, size=1, aes(group=pop, linetype=pop)) + 
    scale_x_discrete(expand=c(0.1,0.1)) + 
    labs(title="Mean standardized differences", 
     y="Deviation from population mean", 
     x="") + 
    theme(plot.title=element_text(size=rel(1.5)), 
     panel.background=element_blank(), 
     axis.line=element_line(1.5), 
     panel.grid=element_blank(), 
     axis.text=element_text(colour='black', size=14), 
     axis.title.y=element_text(colour='black', size=14), 
     axis.ticks=element_line(colour='black', size=1), 
     legend.position=c(.2,.25)) + 
    guides(shape = guide_legend(keywidth = 2)) 

는 대신 linetype 범례를 제거 geom_line 층을 추가하는 경우 show.legend = FALSE를 사용한다.

ggplot(data=dat, aes(x=foundlost, y=mean, ymin=Edn, ymax=Eup, group=pop)) + 
    geom_point(aes(shape=pop), size=4, position=pd) + 
    geom_errorbar(width=0.2, size=1.25, position=pd) + 
    geom_line(position=pd, size=1, aes(group=pop, linetype=pop), show_guide = FALSE) 
+0

빠른 응답을 보내 주셔서 감사합니다. 모든 것이 거의 나를 거기에 데려다 줄 것으로 보이지만 내가 게시 한대로 모두 실행하면 닫히지 않은 괄호가 있습니다. "C (0.2, 0.25) + guide (shape = guide_legend (keywidth = 2)) 오류 : 2 진수 연산자에 숫자가 아닌 인수"오류가 발생합니다. 나는 그걸로 놀았고, ggplot을 실행하고 안내 만하는 경우, 그 부분은 작동하지만, 주제를 조정하려 할 때, 그것은 어색해 보이는 것 같습니다. 다시 한번 감사드립니다. 너희들은 거대한 봉사를한다. –

+0

@ A.Birdman 아아, 예, 편집 할 때 괄호를 사용하지 않았습니다. 추가됨. – aosmith

+0

나는 좀 더 놀았으며 ggplot과 themes 사이의 guides() 호출을 배치하면 게임이 잘 돌아 간다는 것을 알게되었습니다. –

관련 문제