使用ggplot在R中创建日期时间序列图的问题 [英] Problems creating datetime series graph in R using ggplot

查看:648
本文介绍了使用ggplot在R中创建日期时间序列图的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建具有以下特征的图形:

I am trying to create a graph with the following characteristics:

  • x轴:时间和日期
  • y轴:数据

在这里您可以下载我的数据框: https://my.cloudme.com/josechka/data

here you can download my dataframe: https://my.cloudme.com/josechka/data

我尝试使用以下方法生成图形:

I try to produce the graph using:

p <- ggplot(data,aes(x = Date, y = Var,group = 1)) 
        + geom_line() 
        + scale_x_date(labels = date_format("%m/%d/%Y")) 
        + scale_y_continuous(limits = c(0, 70000))    
p    

我得到结果:

Error: Invalid input: date_trans works with objects of class Date only

我在R和ggplot中是一个新手.我在做什么错了?

I am quite new in R and ggplot. What am I doing wrong?

推荐答案

根据建议,您必须将Date列设置为Date对象的格式.

As suggested you have to format the Date column into a Date object.

data$Date<-as.Date(data$Date, format="%d/%m/%Y")

现在,您可以使用脚本来创建绘图了:

Now you can use your script in order to create the plot:

library("ggplo2") 
library("scales")
p <- ggplot(data,aes(x = Date, y = Var,group = 1)) 
        + geom_line() 
        + scale_x_date(labels = date_format("%m/%d/%Y")) 
        + scale_y_continuous(limits = c(0, 70000))    
p

这是结果图:

这篇关于使用ggplot在R中创建日期时间序列图的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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