2013-11-22 2 views
3

아래 그림에서 위치 3의 x 축에 빈 공간을 생성하고 싶습니다. 다른 말로하면 점을 wt>=3wt>=3이 임의의 선택된 값만큼 오른쪽으로 시프트 될 때의 축척입니다. 그것은 의미가 있습니까?ggplot2 : x 축에 빈 공간 만들기

ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() 

enter image description here

나는 단순히 내 데이터를 수정하고 mpgwt>=3의 각 값에 0.5를 추가하지만 x 축으로 문제가 해결되지 않을 수 있습니다.

나는 아래의 그래프에서 수직선이 데이터와 겹치지 않으므로 모든 데이터 (및 x 축)가 두께만큼 왼쪽으로 이동해야한다는 질문을 다시 할 수도 있습니다. 수직선의

ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() + geom_vline(xintercept=3, size=30) 

enter image description here

나는 약 facet_wrap 또는 viewport을 생각했다. 어쩌면 mpg의 각 값에 상수를 추가하고 wt>=3을 입력 한 다음 x 축의 값을 수동으로 설정하는 것과 같습니다. @ 프랭크, 그리고 테스트와 마찬가지로

+0

참조 [이] (http://stackoverflow.com/questions/7194688/using-ggplot2-can-i-insert-a-break-in-the-axis) 관련 질문 축에 틈을 삽입 할 때. – Jota

+0

내가 얻은 가장 가까운 것은 plotrix 패키지를 사용하고 있었다.'gap.plot (mtcars $ wt, mtcars $ mpg, gap = c (2.99999,3.000001), gap.axis = "x")'. 나는 격차 크기를 바꾸는 데에 일하지 않았다. – Jota

+0

또는 ggplot2 : 'mtcars $ foo <-ifelse (mtcars $ wt> = 3, c ("over 3"), c ("under 3"))와 같은 방법을 시도해 볼 수 있습니다. 'mtcars $ foo <-factor (mtcars $ foo, levels = c ("3 이하", "3 이상")) ggplot (mtcars, aes (x = wt, y = mpg)) + geom_point() + facet_wrap ~ foo, 비늘 = "free_x")'. 그래도 여전히 약간의 작업이 필요합니다. – Jota

답변

2

당신이 찾고있는 것이 무엇인지 잘 모르겠지만 이상한 축을 내 자신의 음모로보고 혼란스러워합니다.

mtcars$wt2 <- with(mtcars, ifelse(wt > 3, wt+1, wt)) 

ggplot(mtcars, aes(x = wt2, y = mpg)) + 
    geom_point() + 
    annotate("rect", xmin = 3, xmax = 4, ymin = 0, ymax = 35, alpha = 0.2) + 
    scale_x_continuous(breaks = round(mtcars$wt2), label = round(mtcars$wt)) 

enter image description here

1

...

x <- mtcars 
x$group <- "" 
x[x$wt<3,]$group <- "1.LIGHT" 
x[x$wt>=3,]$group <- "2.HEAVY" 

library(ggplot2) 
library(grid) #for unit(...) 
ggplot(x, aes(x=wt,y=mpg)) + 
    geom_point() + 
    facet_grid(~group,scales="free",space="free") + 
    theme(panel.margin = unit(2, "lines")) 

이 생산 :

enter image description here

1

어떻게 이런 일에 대해?

mtcars <- transform(mtcars, split = ifelse(wt >= 3, "b", "a")) 

ggplot(mtcars, aes(x = wt, y = mpg)) + 
     geom_point() + 
     facet_wrap(~split, scales = "free_x")