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

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

问题描述

我对 R 很陌生,所以这个问题非常初级,但我无法自己解决.我将非常感谢您的帮助.

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

这是我使用的代码:

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

我收到以下错误消息:

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?

推荐答案

您应该在 aes() 调用中添加 group=1 以使用线连接点.这将通知 geom_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天全站免登陆