【问题标题】:How to solve "Error in seq.int(0, to0 - from, by) : 'to' must be a finite number" in ggplot?如何解决ggplot中的“​​seq.int(0,to0 - from,by)中的错误:'to'必须是有限数”?
【发布时间】:2021-05-06 22:39:05
【问题描述】:

我的数据如下所示:

> head(mydata, 24)
     time     exp2
1  Jan-99 24977.25
2  Feb-99 21186.07
3  Mar-99 41245.92
4  Apr-99    47.57
5  May-99 25254.42
6  Jun-99   164.95
7  Jul-99  9629.81
8  Aug-99   164.95
9  Sep-99 47091.12
10 Oct-99     0.09
11 Nov-99  3458.64
12 Dec-99    24.00
13 Jan-00 28636.17
14 Feb-00 23850.65
15 Mar-00 23680.16
16 Apr-00   513.82
17 May-00   252.17
18 Jun-00   178.10
19 Jul-00 20293.85
20 Aug-00   474.16
21 Sep-00   164.95
22 Oct-00   164.95
23 Nov-00   164.95
24 Dec-00    96.10
> 

我想在 ggplot 中创建一个简单的图表,使用:

newdata2 <- mydata %>%
  mutate(date = as.Date(time, format = "%m/%y"))

ggplot(newdata2, aes(x = time, y = exp2)) +
      geom_line(color = "darkorchid4")

错误是:

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

运行时:

mydata$time <- as.Date(mydata$time, format = "%m/%y")
ggplot(mydata, aes(x = time, y = exp2)) +
      geom_line(color = "darkorchid4")

我收到此错误:

Error in seq.int(0, to0 - from, by) : 'to' must be a finite number

我不明白为什么会出现这个错误。 你能帮帮我吗?

【问题讨论】:

  • 你检查newdata2的输出了吗? date 列由什么组成?

标签: r ggplot2


【解决方案1】:

连接group = 1 中设置的点aes

ggplot(df, aes(x = time, y = exp2, group = 1)) +
    geom_line(color = "darkorchid4")

如果time 是类字符,则需要先将其转换为日期对象,然后才能更改格式:

format(as.Date(paste0(df$time, "-01"), "%b-%y-%d"), "%m/%y")
[1] "01/99" "02/99" "03/99" "04/99" "05/99" "06/99" "07/99" "08/99" "09/99" "10/99" "11/99"
[12] "12/99" "01/00" "02/00" "03/00" "04/00" "05/00" "06/00" "07/00" "08/00" "09/00" "10/00"
[23] "11/00" "12/00"

可以在mutate 函数中使用,例如:

df %>%
  mutate(date = format(as.Date(paste0(time, "-01"), "%b-%y-%d"), "%m/%y"))

更新

以上代码会将您的值转换为character 对象。由于您想使用 scale_x_date 绘制和调整 x 轴,因此它需要是 Date 对象。为了让您了解这里的可能性,这是一个可重现的示例:

library(ggplot2)
library(dplyr)

df %>%
  mutate(date = as.Date(paste0(time, "-01"), "%b-%y-%d")) %>% 
  ggplot(aes(x = date, y = exp2, group = 1)) +
  geom_line(color = "darkorchid4") + 
  geom_col(width = 2, fill = "red") + 
  scale_x_date(date_breaks = "2 months",
               date_labels = "%b-%y") + 
  theme(axis.text.x = element_text(angle = 90))

数据

df <- structure(list(time = c("Jan-99", "Feb-99", "Mar-99", "Apr-99", 
"May-99", "Jun-99", "Jul-99", "Aug-99", "Sep-99", "Oct-99", "Nov-99", 
"Dec-99", "Jan-00", "Feb-00", "Mar-00", "Apr-00", "May-00", "Jun-00", 
"Jul-00", "Aug-00", "Sep-00", "Oct-00", "Nov-00", "Dec-00"), 
    exp2 = c(24977.25, 21186.07, 41245.92, 47.57, 25254.42, 164.95, 
    9629.81, 164.95, 47091.12, 0.09, 3458.64, 24, 28636.17, 23850.65, 
    23680.16, 513.82, 252.17, 178.1, 20293.85, 474.16, 164.95, 
    164.95, 164.95, 96.1)), row.names = c(NA, -24L), class = "data.frame")

【讨论】:

  • 非常感谢您的回复。不幸的是,它不起作用。我试过:newdata &lt;- mydata %&gt;% mutate(date = format(as.Date(paste0(time, "-01"), "%b-%y-%d"), "%m/%y")) ,然后是ggplot(newdata2, aes(x = date, y = exp2, group=1)) + geom_line(color = "darkorchid4"),但我得到了这个错误:```错误:mapped_discrete对象只能从数字向量创建运行rlang::last_error()以查看错误发生的位置。```
  • @Marian 您能否提供更多有关您帖子中的时间序列是如何创建的详细信息?您发布的系列的值是 40000,但您的图表没有。也许绘制了某种移动平均线?
  • 好吧,因为 Excel 中的图表从 2010 年 1 月开始......我的问题是使用 ggplot 在 R 中创建一个简单的图表非常复杂,当日期(时间序列按月计算) .例如,当我的日期时间列仅按年计算时,ggplot 可以正常工作 - 但不能使用每月数据...
  • 我们可以在房间里聊天吗?
【解决方案2】:

我想创造这样的东西。这个图表是在 Excel 中创建的,看起来很棒,但我在 R 中努力创建它,这被证明是非常困难的。

之后,运行以下代码:

newdata <- mydata %>%
mutate(date = format(as.Date(paste0(time, "-01"), "%b-%y-%d"), "%m/%y"))

ggplot(newdata, aes(x = date, y = exp2, group=1)) +
      geom_line(color = "darkorchid4") + xlim = c('01/10', '12/29')

R 创造了这样的东西:

另外,由于时间序列是按月计算的,我想使用 ggplot+xlim 绘制图表(从某个“月-年”到某个“月-年”)。

【讨论】:

  • 您应该为此使用scale_x_date。您还可以使用此图层设置标签格式和间隔。否则,您可以在绘图之前过滤数据。您在 excel 中发布的情节看起来也有条形图。所以我认为除了geom_line,您还需要添加?geom_col 层。目前还不清楚平滑线是如何计算的,但我猜它是某种平滑的移动平均线。
  • 我也尝试过使用 scale_x_date 但无法正常工作。您能否附上完整的代码,也许我遗漏了一些步骤?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-23
  • 2022-11-04
相关资源
最近更新 更多