Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot: axis don't intersect at origin

Tags:

r

ggplot2

I am trying to plot the following vector using ggplot:

library(ggplot2)
vec  =c(44.55 ,47.81 ,40.28 ,44.32 ,53.57 ,45.68 ,52.02 ,44.27 ,33.44 ,41.16)
by = c("1994-04-30", "1994-05-31", "1994-06-30", "1994-07-31", "1994-08-31", "1994-09-30", "1994-10-31", "1994-11-30", "1994-12-31", "1995-01-31")
vec.zoo = zoo(vec, order.by = as.Date(by))


g <-ggplot(vec.zoo) +
geom_line (aes(x=index(vec.zoo), y=coredata(vec.zoo)), color = "cadetblue4", size = 0.6) + 
theme(axis.text.x = element_text(angle = 45, hjust = 1)) + 
xlab("Time") + 
ylab("Hit Ratio") + 
scale_y_continuous(limits = c(0, 100))
scale_x_date(limits = c(start(vec.zoo), end(vec.zoo)))
g

Although I set the limits of the axis, they still don't intersect at origin. I would like to set the intersection at x= 0 and y = start(vec).

Here is the result I obtain:

enter image description here

like image 868
Mayou Avatar asked Nov 18 '13 14:11

Mayou


1 Answers

You may use the expand argument in your scale calls. Setting expand to zero, removes the default, small gap between data and axes (see here)

g <-ggplot(vec.zoo) +
  geom_line (aes(x=index(vec.zoo), y=coredata(vec.zoo)), color = "cadetblue4", size = 0.6) + 
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) + 
  xlab("Time") + 
  ylab("Hit Ratio") + 
  scale_y_continuous(limits = c(0, 100), expand = c(0, 0)) +
scale_x_date(limits = c(start(vec.zoo), end(vec.zoo)), expand = c(0, 0))
g
like image 120
Henrik Avatar answered Nov 12 '22 03:11

Henrik