ggplot:轴不在原点相交 [英] ggplot: axis don't intersect at origin

查看:502
本文介绍了ggplot:轴不在原点相交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用 ggplot 绘制下面的向量:

 库(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, 11-30,1994-12-31,1995-01-31)
vec.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(命中率) +
scale_y_continuous(limits = c(0,100))
scale_x_date(limits = c(start(vec.zoo),end(vec.zoo)))
g

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



以下是我得到的结果:



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

解决方案

您可以在展开 code> scale 调用。设置展开为零,删除数据和坐标轴之间的默认小间隔(参见 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(命中率)+
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


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:

Any help would be appreciated!! Thanks!!

解决方案

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

这篇关于ggplot:轴不在原点相交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆