【问题标题】:Plot time series data (wide format)绘制时间序列数据(宽格式)
【发布时间】:2017-09-27 15:07:07
【问题描述】:

我在 R Studio 中有一个合并的 dataframe,如下所示:

head(my_df)
     Year        Month.x      val1       Month.y        val2
     2005-01     January     22.43099    January        26.3339814993271
     2005-02     February    26.62969    February       30.8841743766816
     2005-03     March       31.67926    March          27.9245803297443
     2005-04     April       23.65202    April          30.9088206490251
     2005-05     May         25.39969    May            26.494307897712
     2005-06     June        20.30036    June           18.9395527997218

year 列包含 2005 年 1 月至 2015 年 12 月的数据。数据框中的 val2 列中有一个 "NA",但我修复了它:

my_df[is.na(my_df)] <- ""

我需要在 R 中绘制这个时间序列数据(val1val2year)。最初,我试图使用 R 基础图形绘制 val1year

plot(my_df$Year, my_df$val1, type = "b", col = "blue", xlim=c(2005, 2015),
     lwd=1, pch = 1, cex = 0.2, xlab="Year", ylab="Value")

但我看到一个带有此警告的空白图表。

Warning message:
In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion

谁能帮我找出哪里出了问题?无论如何我该如何解决这个问题。

EDIT1:

正如@Santosh 所建议的,dput(my_df) 在这里:

structure(list(Year = c("2005-01", "2005-02", "2005-03", "2005-04", 
"2005-05", "2005-06", "2005-07", "2005-08", "2005-09", "2005-10", 
"2005-11", "2005-12", "2006-01", "2006-02", "2006-03", "2006-04", 
"2006-05", "2006-06", "2006-07", "2006-08", "2006-09", "2006-10", 
"2006-11", "2006-12", "2007-01", "2007-02", "2007-03", "2007-04", 
............
"2014-11", "2014-12", "2015-01", "2015-02", "2015-03", "2015-04", 
"2015-05", "2015-06", "2015-07", "2015-08", "2015-09", "2015-10", 
"2015-11", "2015-12"),  Month.x = c("January", "February", "March", 
"April", "May", "June", "July", "August", "September", "October",
.............
"February", "March", "April", "May", "June", "July", "August", 
"September", "October", "November", "December"), val1 = c(22.4309863561828, 
26.629689578869, 31.6792564287634, 23.6520192347222, 25.3996868508065, 
20.3003602638889, 20.2621707795699, 22.3685403172043, 30.7087719888889,
..........
13.8973171652778, 11.3131837150538, 13.2869582405914, 16.4443315347222, 
17.5448029758065, 22.8475848819444, 15.2890522220727), Month.y = c("January", 
"February", "March", "April", "May", "June", "July", "August",
............
 "July", "August", "September", "October", "November", "December"
), val2 = c("26.3339814993271", "30.8841743766816", "27.9245803297443", 
"30.9088206490251", "26.494307897712", "18.9395527997218", "21.9441695597826",
..........
"18.3722117002688", "17.8116471652778", "19.684253344086", "25.0107780152778", 
"20.6051117175464")), .Names = c("Year", "Month.x", "val1", 
"Month.y", "val2"), row.names = c(NA, -132L), class = "data.frame"

EDIT2:

plot 函数中删除xlim(2005, 2015) 也会产生错误:

Error in plot.window(...) : need finite 'xlim' values
In addition: Warning messages:
1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf

【问题讨论】:

  • 它可能将"" 转换回它无法绘制的NA。您可能需要估算缺失值,或将其删除。
  • 也许你可以在绘图时尝试na.rm = TRUE。您可以输入输入数据框的 dput 吗?
  • 您可能也有 xlim 的问题。根据 R 对第一列的看法,(char, date, POSIXct) 它可能不承认 2005, 2015 是数字的。确保基础数据的所有单元和结构都一致,并且如果您依赖于加载适当包的解释。对此的一个简单测试是删除 xlim 并查看它是否绘制(甚至错误),然后努力使轴和年份列一致。
  • @Santosh:我尝试了 na.rm=TRUE,但最终出现了另一个错误:na.rm 不是图形参数。请参阅 dput() 的更新。
  • @sconfluentus:我尝试删除 xlim() 但错误仍然存​​在。请查看编辑。

标签: r ggplot2


【解决方案1】:

以下是如何过滤掉NA 案例并绘制数据的解决方案:

  1. 选择您将使用的列
  2. Year字符串转换为日期
  3. 将数据从宽格式转换为长格式
  4. 过滤掉NA案例
  5. 使用ggplot2 绘图

tidyverse 包在这里加载你需要的所有包(例如dplyrggplot2

library(tidyverse)

my_df %>%
    select(Year, val1, val2) %>%
    mutate(Year = as.Date(paste0(Year, "-01"))) %>%
    gather(val, value, -Year) %>%
    filter(complete.cases(.)) %>%
    ggplot(aes(Year, value, color = val)) +
        geom_line()

【讨论】:

  • 谢谢。这里的“val”、“value”是什么?就我而言,这是否意味着“val1”、“val2”?我需要将“val2”的缺失值保留为空白。不知何故,它对我不起作用。
  • @JibranKhan 我将val1val2 都命名为val
  • 好的,谢谢。我试过。收到此警告:geom_path:每个组仅包含一个观察值。需要调整群体审美吗?图表看起来很奇怪。
  • 您能用您提供的数据对此进行测试吗?它仍然产生错误吗?
  • @JibranKhan 类似这样的:scale_colour_manual(name = "", values = c("blue", "red"))
猜你喜欢
  • 2021-11-20
  • 2020-04-09
  • 2010-11-13
  • 2015-09-19
  • 2013-12-25
  • 2011-09-30
  • 1970-01-01
相关资源
最近更新 更多