ggplot:轴不与原点相交

时间:2013-11-18 14:22:09

标签: r ggplot2

我正在尝试使用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

虽然我设置了轴的极限,但它们仍然不在原点相交。我想在x= 0y = start(vec)设置交叉点。

以下是我获得的结果:

enter image description here

任何帮助将不胜感激!谢谢!

1 个答案:

答案 0 :(得分:9)

您可以在expand来电中使用scale参数。将expand设置为零,删除数据和轴之间的默认小间隙(请参阅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