如何解决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/(我试图制作一个reprex,但有一些我不明白的错误)
当我使用 r 中的原始数据运行原始代码时,我仍然遇到相同的错误,我不知道该怎么做或如何修复它
提前致谢
解决方法
网站上的代码没有按预期运行,因为它读入了一个名为 milestones.csv
的文件,您无权访问该文件。但是,如果您确实在 df
中创建数据集,则网站上的所有内容都会按预期运行。下面的代码运行产生预期的情节。您可以发布尝试运行此代码时遇到的错误吗?
df <- tibble::tribble(
~month,~year,~milestone,~status,6,2017,"Milestone 1","Complete",7,"Milestone 2"," Complete",10,"Milestone 3",12,"Milestone 4",1,2018,"Milestone 5","Milestone 6",2,"Milestone 7",5,"Milestone 8","Milestone 9","On Target","Milestone 10",9,"Milestone 11","At Risk",11,"Milestone 12","Milestone 13","Milestone 14","Milestone 15",4,2019,"Milestone 16","Critical","Milestone 17","Milestone 18","Milestone 19","Milestone 20","Milestone 21","Milestone 22",)
df$date <- with(df,ymd(sprintf('%04d%02d%02d',year,month,1)))
df <- df[with(df,order(date)),]
head(df)
status_levels <- c("Complete","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),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,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"),color='black')
# Show text for each milestone
timeline_plot<-timeline_plot+geom_text(aes(y=text_position,label=milestone),size=2.5)
timeline_plot
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。