【问题标题】:Use ggplot2 to plot time series data使用 ggplot2 绘制时间序列数据
【发布时间】:2017-03-04 12:20:47
【问题描述】:

我正在做一个需要使用 ggplot2 来绘制时间序列数据的项目。这是我正在使用的数据集:

这就是我现在所做的:

library(ggplot2)
library(lubridate)
eur$Date <- as.Date(eur$Date)
ggplot(eur, aes(Date, EUR)) + geom_line()

我得到了这个非常奇怪的情节。有人可以帮我解决问题吗?

【问题讨论】:

  • 请分享您的数据框eur的一些数据。
  • 我上传了eur的截图。你可以点击它。
  • 看起来您的EUR 列是一个因素。检查str(eur) Date 属于Date 类,EUR 属于numeric 类型,而不是一个因素。

标签: r ggplot2 time-series lubridate


【解决方案1】:

编辑 - 如果您的数据属于时间序列(ts)类,那么您可以使用 ggfortify 的自动绘图功能,它会为您处理转换。

library(ggfortify) 
library (ggplot2)
ggfortify::autoplot(df) 

如果您的数据不是时间序列,那么您的日期列可能是因子或字符。

如果是字符转换用 as.Date(foo)

如果它是一个因素,那么请参见下文

   #create the data 
data <- data.frame(
i = c(6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L),
Date = c("2000-01-03", "2000-01-04", "2000-01-05", "2000-01-06",
"2000-01-07", "2000-01-10", "2000-01-11", "2000-01-12",
"2000-01-13", "2000-01-14", "2000-01-18", "2000-01-19"),
EUR = c(1.0155, 1.0309, 1.0335, 1.0324, 1.0294, 1.0252, 1.0322,
1.0281, 1.027, 1.0128, 1.0121, 1.0115)
)

#check the data
str(data)

# convert date column to Date as it is currently a **factor**
data$Date <- as.Date(as.character(data$Date))

#basic plot
library(ggplot2)
ggplot(data,aes(Date, EUR)) + geom_line() + geom_point()

## plot can be tidied up further using lubridate/scales packages 

#sessionInfo() 
# R version 3.4.0 (2017-04-21)
# Platform: x86_64-w64-mingw32/x64 (64-bit)
# Running under: Windows 7 x64 (build 7601) Service Pack 1
# 
# Matrix products: default
# 
# locale:
#   [1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252   
# [3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C                           
# [5] LC_TIME=English_United Kingdom.1252    
# 
# attached base packages:
#   [1] stats     graphics  grDevices utils     datasets  methods   base     
# 
# other attached packages:
#   [1] ggplot2_2.2.1
# 
# loaded via a namespace (and not attached):
#   [1] labeling_0.3     colorspace_1.3-2 scales_0.4.1     compiler_3.4.0   lazyeval_0.2.0  
# [6] plyr_1.8.4       tools_3.4.0      gtable_0.2.0     tibble_1.3.3     Rcpp_0.12.11    
# [11] grid_3.4.0       rlang_0.1.1      munsell_0.4.3   
# > 

【讨论】:

    【解决方案2】:

    你需要扩展你的数据。您能否详细说明您对数据外观的解释。这种格式可能会对您有所帮助。不过我需要更好地理解数据

    library(ggplot2)
    library(lubridate)  
    ggplot( aes(Date, EUR)) + geom_line() +
    scale_x_date(format = "%b-%Y") + xlab("") + ylab("")
    

    【讨论】:

    • 数据为每日汇率。我刚试过你的代码,但 R 向我展示了这个“错误:ggplot2 不知道如何处理 uneval 类的数据”。
    【解决方案3】:

    @Niranja Gd 的回答不起作用,因为formatscale_x_date 中没有参数。

    以下方法应该可以完成这项工作:

    df$Date <- as.Date(df$Date)
    
    library(ggplot2)
    
    ggplot(df, aes(Date, EUR)) + geom_line() +
      scale_x_date(date_labels = "%d-%b")
    

    【讨论】:

    • 谢谢!我刚刚尝试了代码,但我得到了一个同样问题的情节。我认为您的代码应该可以工作,但我不确定系统是否有问题或我的代码是否有其他问题。
    猜你喜欢
    • 2021-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-30
    • 2018-07-05
    • 2021-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多