seq.int(r1$mon, 12 (to0$year - r1$year) + to0$mon, by) 错误:from 必须是有限数

Error in seq.int(r1$mon, 12 (to0$year - r1$year) + to0$mon, by) : from must be a finite number

我是 R studio 的初学者,每次我 运行 使用我自己的数据编写此特定代码时都会出错。我已经按照示例中设置 csv 的方式格式化了自己的数据。

代码取自https://benalexkeen.com/creating-a-timeline-graphic-using-r-and-ggplot2/(我试图做一个reprex,但有错误我不明白)

当我 运行 原始代码和 r 中的原始数据时,我仍然遇到相同的错误,我不确定该怎么做或如何修复它

提前致谢

网站上的代码与预期的不同 运行,因为它读取了一个名为 milestones.csv 的文件,您无权访问该文件。但是,如果您实际上在 df 中制作数据集,则网站上的所有内容都如预期的那样 运行。 运行s 下面的代码产生了预期的情节。你能 post 尝试 运行 这段代码时遇到的错误吗?

df <- tibble::tribble(
  ~month,   ~year,  ~milestone,     ~status,
  6,    2017,   "Milestone 1",  "Complete",
  7,    2017,   "Milestone 2",  "   Complete",
  10,   2017,   "Milestone 3",  "Complete",
  12,   2017,   "Milestone 4",  "   Complete",
  1,    2018,   "Milestone 5",  "Complete",
  1,    2018,   "Milestone 6",  "   Complete",
  2,    2018,   "Milestone 7",  "Complete",
  5,    2018,   "Milestone 8",  "Complete",
  6,    2018,   "Milestone 9",  "On Target",
  6,    2018,   "Milestone 10",  "On Target",
  9,    2018,   "Milestone 11",     "At Risk",
  11,   2018,   "Milestone 12",  "On Target",
  12,   2018,   "Milestone 13",     "On Target",
  12,   2018,   "Milestone 14",  "On Target",
  12,   2018,   "Milestone 15",     "At Risk",
  4,    2019,   "Milestone 16",  "Critical",
  7,    2019,   "Milestone 17",     "On Target",
  7,    2019,   "Milestone 18",  "On Target",
  9,    2019,   "Milestone 19",     "On Target",
  10,   2019,   "Milestone 20",  "At Risk",
  10,   2019,   "Milestone 21",     "On Target",
  12,   2019,   "Milestone 22",  "Critical",
)



df$date <- with(df, ymd(sprintf('%04d%02d%02d', year, month, 1)))
df <- df[with(df, order(date)), ]
head(df)



status_levels <- c("Complete", "On Target", "At Risk", "Critical")
status_colors <- c("#0070C0", "#00B050", "#FFC000", "#C00000")

df$status <- factor(df$status, levels=status_levels, ordered=TRUE)

positions <- c(0.5, -0.5, 1.0, -1.0, 1.5, -1.5)
directions <- c(1, -1)

line_pos <- data.frame(
  "date"=unique(df$date),
  "position"=rep(positions, length.out=length(unique(df$date))),
  "direction"=rep(directions, length.out=length(unique(df$date)))
)

df <- merge(x=df, y=line_pos, by="date", all = TRUE)
df <- df[with(df, order(date, status)), ]

text_offset <- 0.05

df$month_count <- ave(df$date==df$date, df$date, FUN=cumsum)
df$text_position <- (df$month_count * text_offset * df$direction) + df$position

month_buffer <- 2

month_date_range <- seq(min(df$date) - months(month_buffer), max(df$date) + months(month_buffer), by='month')
month_format <- format(month_date_range, '%b')
month_df <- data.frame(month_date_range, month_format)

year_date_range <- seq(min(df$date) - months(month_buffer), max(df$date) + months(month_buffer), by='year')
year_date_range <- as.Date(
  intersect(
    ceiling_date(year_date_range, unit="year"),
    floor_date(year_date_range, unit="year")
  ),  origin = "1970-01-01"
)
year_format <- format(year_date_range, '%Y')
year_df <- data.frame(year_date_range, year_format)

timeline_plot<-ggplot(df,aes(x=date,y=0, col=status, label=milestone))
timeline_plot<-timeline_plot+labs(col="Milestones")
timeline_plot<-timeline_plot+scale_color_manual(values=status_colors, labels=status_levels, drop = FALSE)
timeline_plot<-timeline_plot+theme_classic()

# Plot horizontal black line for timeline
timeline_plot<-timeline_plot+geom_hline(yintercept=0, 
                                        color = "black", size=0.3)

# Plot vertical segment lines for milestones
timeline_plot<-timeline_plot+geom_segment(data=df[df$month_count == 1,], aes(y=position,yend=0,xend=date), color='black', size=0.2)

# Plot scatter points at zero and date
timeline_plot<-timeline_plot+geom_point(aes(y=0), size=3)

# Don't show axes, appropriately position legend
timeline_plot<-timeline_plot+theme(axis.line.y=element_blank(),
                                   axis.text.y=element_blank(),
                                   axis.title.x=element_blank(),
                                   axis.title.y=element_blank(),
                                   axis.ticks.y=element_blank(),
                                   axis.text.x =element_blank(),
                                   axis.ticks.x =element_blank(),
                                   axis.line.x =element_blank(),
                                   legend.position = "bottom"
)

# Show text for each month
timeline_plot<-timeline_plot+geom_text(data=month_df, aes(x=month_date_range,y=-0.1,label=month_format),size=2.5,vjust=0.5, color='black', angle=90)
# Show year text
timeline_plot<-timeline_plot+geom_text(data=year_df, aes(x=year_date_range,y=-0.2,label=year_format, fontface="bold"),size=2.5, color='black')
# Show text for each milestone
timeline_plot<-timeline_plot+geom_text(aes(y=text_position,label=milestone),size=2.5)
timeline_plot