2017-09-01 47 views
0

geom_line在線兩側有很大差距,如何消除這個差距!如何從ggplot2中的geom_line中刪除空白?

geom_line

[R腳本被列爲遵循,

p <- ggplot(singJanS)+ geom_line(aes(x=sn,y=diff)) 
p <- p + geom_hline(yintercept=seq(-0.8,0.8,by=0.4), linetype=2, colour="grey") + 
    geom_vline(xintercept=seq(15,744,by=24), linetype=6, colour="red") + 
    geom_vline(xintercept=seq(23,744,by=24), linetype=6, colour="blue") + 
    ylab(Delta~T~' ('~degree~C~')')+xlab("")+ 
    scale_x_continuous(breaks = c(0,120,240,360,480,600,720))+ 
    scale_y_continuous(breaks = c(-0.8,-0.4,0,0.4,0.8)) +theme_bw() 
+0

你有沒有嘗試添加'XLIM(0,750)'你的圖形? –

+0

謝謝!但它是無效的! –

+0

你能提供'dput(singJanS)'的結果嗎?這樣我們就可以重現這個問題嗎? –

回答

0

嘗試添加到您的代碼

+ scale_x_continuous(limits = c(0, 750)) 
+0

謝謝!但它是invaild! –

3

通過@ Z.Lin提出的解決方案正常工作。
如果將expand = c(0, 0)添加到scale_x_continuous(),則會刪除兩個間隔。

set.seed(1) 
singJanS <- data.frame(sn=1:740, diff=rnorm(740)/3) 

p <- ggplot(singJanS)+ geom_line(aes(x=sn,y=diff)) 
p <- p + geom_hline(yintercept=seq(-0.8,0.8,by=0.4), linetype=2, colour="grey") + 
    geom_vline(xintercept=seq(15,744,by=24), linetype=6, colour="red") + 
    geom_vline(xintercept=seq(23,744,by=24), linetype=6, colour="blue") + 
    ylab(Delta~T~' ('~degree~C~')')+xlab("")+ 
    scale_x_continuous(breaks = c(0,120,240,360,480,600,720),expand = c(0, 0))+ 
    scale_y_continuous(breaks = c(-0.8,-0.4,0,0.4,0.8)) +theme_bw() 
p 

enter image description here

+0

這沒關係!非常感謝你! –