2013-11-22 38 views
3

在下面的圖中,我想在位置3的x軸上創建一個空的空間。換句話說,我希望wt>=3wt>=3被右移一些任意選擇的值時軸比例。是否有意義?ggplot2:在x軸上創建一個空的空間

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

enter image description here

我可以簡單地修改我的數據,並添加0.5至mpg其中wt>=3每個值,但它不與x軸解決問題。

我可能會改說我的問題,說在下面的圖表上,我希望垂直線不重疊數據,因此所有數據(以及x軸)都應該向左移動厚度的垂直線。

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

enter image description here

我在想facet_wrapviewport。也許像爲每個值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(「under 3」,「over 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") 
相關問題