2016-12-12 7 views
0

나는 ggplot과 나란히 두 데이터 세트를 가지고 있으므로 y-axis을 공유합니다.ggplot의 스티치면

ggplot 님의 facet_wrap을 사용하는 것이 좋겠지 만, stitch을 함께 사용하는 방법을 찾아야합니다.

df.1 <- data.frame(x=c(-0.678071905112638,1.32192809488736,-0.678071905112638,1.32192809488736,-0.678071905112638,1.32192809488736), 
        y=c(62.8805462356349,73.027603062927,88.4090942806369,87.6879626013305,55.9895740872068,93.5396099910227), 
        side=1,stringsAsFactors = F) 

df.2 <- data.frame(x=c(1.32192809488736,3.32192809488736,1.32192809488736,1.32192809488736), 
        y=c(73.027603062927,7.33717302418609,87.6879626013305,93.5396099910227), 
        side=2,stringsAsFactors = F) 

df <- rbind(df.1,df.2) 
df$side <- factor(df$side,levels=c(1,2)) 

require(ggplot2) 
ggplot(df,aes(x=x,y=y))+geom_point()+facet_wrap(~side,ncol=2,scales="free")+stat_smooth(method="lm",formula=y~x,colour="black")+theme(strip.text.y=element_text()) 

enter image description here

어떻게 내가 바로면의 y 축으로 제거하고 마치 하나의 그림으로 표시되도록면 사이의 공간을 제거합니까 : 이것은 내가 지금까지 가지고 무엇인가? 또한 Y 축 좌표도 동일해야합니다.

facets을 사용하는 이유는 분명히 각각 df에 작품을 적용하고 있기 때문입니다.

답변

1

당신은 공간을 제거하는 측면 사이의 마진을 조정할 수 있습니다. 또한 scales="free_x"을 사용하면 x 축 배율 만 사용 가능하고 y 축 배율은 동일합니다. y 축 스케일은 각면에서 동일하기 때문에, 규모가 오른쪽면에 반복되지 않을 것이다 :

theme_set(cowplot::theme_cowplot()) 

ggplot(df,aes(x=x,y=y))+geom_point() + 
    facet_wrap(~side, ncol=2, scales="free_x") + 
    stat_smooth(method="lm", formula=y~x, colour="black") + 
    theme(panel.spacing = unit(-1.25, "lines")) 

enter image description here

그러나 당신은 또한 색상을 사용하여 별도의 라인을 facetting를 방지하고 얻을 수 미적. 이 방법은 하나의 패널에 모든 것을두고, 그래서 당신은 측면 사이의 X-저울을 안감에 대해 걱정할 필요가 없습니다 :

ggplot(df,aes(x=x, y=y)) + 
    geom_point() + 
    stat_smooth(method="lm",formula=y~x, aes(colour=side)) 

enter image description here

1

y 제한을 ylim으로 설정하십시오. 간격을 panel.spacing.x으로 조정하십시오. 원하지 않는 레이블을 axis.text.y으로, 눈금 표시를 axis.ticks으로 제거하십시오.

ggplot(df,aes(x=x,y=y))+geom_point()+ ylim(0, 120) + 
     facet_wrap(~side,ncol=2,scales="free")+ 
     stat_smooth(method="lm",formula=y~x,colour="black")+ 
     theme(strip.text.y=element_text(), axis.text.y = element_blank(), 
     axis.ticks = element_blank(), panel.spacing.x=unit(0, "lines")) 

enter image description here