r - seq.int(r1$mon, 12 (to0$year - r1$year) + to0$mon, by) 中的错误:from 必须是有限数
问题描述
我是 R studio 的初学者,每次使用自己的数据运行此特定代码时都会遇到错误。我已经按照示例中设置 csv 的方式格式化了自己的数据。
代码取自https://benalexkeen.com/creating-a-timeline-graphic-using-r-and-ggplot2/(我试图做一个代表,但有我不明白的错误)
当我使用 r 中的原始数据运行原始代码时,我仍然会遇到相同的错误,我不确定该怎么做或如何修复它
提前致谢
解决方案
网站上的代码没有按预期运行,因为它读取了一个名为 的文件milestones.csv
,而您将无权访问该文件。但是,如果您确实在 中制作数据集df
,则网站上的所有内容都会按预期运行。下面的代码运行产生预期的情节。您可以发布尝试运行此代码时遇到的错误吗?
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
推荐阅读
- html - 如何修复我的 React Bootstrap 导航栏?(导航栏品牌折叠)已更新
- azure-active-directory - Azure AD SCIM 集成会删除直接在 Snowflake 中创建的预先存在的用户吗?
- nativescript - Nativescript + Vuejs - 当应用程序在后台时显示对话框
- javascript - 使用“pkg”打包 node.js 应用程序后,如何在 Node.js 中运行 python 脚本。
- html - 在ajax成功时更改html标签样式属性
- kubernetes - pod_mutation_hook 函数不适用于使用 KubernetesExecutor 在 kubernetes 中运行的气流
- selenium - 控制文件名下载硒
- java - 根据各种条件分配纹理的最高效代码方式?
- angular - 如何通过时刻获取给定年份的给定月份的最后一天
- wordpress - 使用 Lottie 和 wordpress 在悬停时触发动画