H.W ZHu
H.W ZHu

Reputation: 1

How to remove blank from geom_line in ggplot2?

geom_line has big gap in line two sides, how to remove this gap!

geom_line

R scripts is listed as follow,

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()

Upvotes: 0

Views: 568

Answers (2)

Marco Sandri
Marco Sandri

Reputation: 24252

The solution suggested by @Z.Lin works correctly.
The two gaps are removed if you add expand = c(0, 0) to 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

Upvotes: 3

R18
R18

Reputation: 1560

Try adding to your code

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

Upvotes: 0

Related Questions