如何从ggplot2中的geom_line中删除空白?

时间:2017-09-01 09:01:56

标签: r 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()

2 个答案:

答案 0 :(得分:3)

@ Z.Lin建议的解决方案正常工作。 如果将expand = c(0,0)添加到scale_x_continuous(),则会删除这两个间隙。 set.seed(1) singJanS&lt; - data.frame(sn = 1:740,diff = rnorm(740)/ 3) p&lt; -ggplot(singJanS)+ geom_line(aes(x = sn,y = diff)) p&lt; -p + geom_hline(yintercept = seq(-0.8,0.8,by = 0.4),linetype = 2,color =&#34; grey&#34;)+   geom_vline(xintercept = seq(15,744,by = 24),linetype = 6,color =&#34; red&#34;)+   geom_vline(xintercept = seq(23,744,by = 24),linetype = 6,color =&#34; blue&#34;)+   ylab(Delta~T~&#39;(&#39;〜度~C~&#39;)&#39;)+ xlab(&#34;&#34;)+   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

答案 1 :(得分:0)

尝试添加代码

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