用ggplot2创建线图,使用时间段作为x变量 [英] Create line graph with ggplot2, using time periods as x-variable

查看:180
本文介绍了用ggplot2创建线图,使用时间段作为x变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对R非常陌生,所以这个问题是非常基本的,但我自己无法解决。我非常感谢你的帮助。



这是一种我想使用的数据框:

 期间价值Cut.off 
1998年1月1日 - 2002年8月8.798129 1.64
2002年9月 - 2006年6月4.267268 1.64
2006年7月3日 - 2009年12月7.280275 1.64
  

> require(ggplot2)
bq < - ggplot(data = glomor,aes(x = as.character(Period),y = Value))+ geom_point()+ ylim(0,10)

bq <-bq + scale_x_discrete(limits = c(1998年1月 - 2002年8月,2002年9月 - 2006年6月,2006年7月 - 2009年12月))

bq + geom_line()

我收到以下错误消息:

  geom_path:每个组只包含一个观察值。你需要调整团体审美吗? 

如何更改代码,以便点连线?

解决方案

您应该在 group = 1 > aes()用线调用锥点。这将通知 geom_line()所有点属于一个级别,并且它们应该连接。

  ggplot(data = glomor,aes(x = as.character(Period),y = Value,group = 1))+ 
geom_point()+ ylim (0,10)+ geom_line()


I am very new to R, and so this question is extremely elementary, but I can't solve it myself. I would very much appreciate your help.

This is a sort of dataframe I want to use:

     Period                           Value   Cut.off
1   January 1998 - August 2002      8.798129    1.64
2   September 2002 - Jun 2006       4.267268    1.64
3   Jul 2006 - Dec 2009             7.280275    1.64

This the code I am using:

require(ggplot2)
bq <- ggplot(data=glomor, aes(x=as.character(Period),y=Value))+geom_point()+ylim(0,10)

bq <- bq + scale_x_discrete(limits=c("January 1998 - August 2002","September 2002 - Jun 2006","Jul 2006 - Dec 2009"))

bq + geom_line()

I receive the following error message:

geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?

How do I need to change the code, so that the points will be connected by a line?

解决方案

You should add group=1 in your aes() call to conect points with line. This will inform geom_line() that all your points belong to one level and they should be connected.

ggplot(data=glomor, aes(x=as.character(Period),y=Value,group=1))+
   geom_point()+ylim(0,10) + geom_line()

这篇关于用ggplot2创建线图,使用时间段作为x变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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