我想将本教程中介绍的时间轴转换为水平版本,其中将同一时间点的标签直接放置在彼此下方且不重叠:
http://benalexkeen.com/creating-a-timeline-graphic-using-r-and-ggplot2/
我已经稍微修改了网站上的代码,以手动将其调整为我的数据,但是代码非常相似。在水平时间轴上工作得很好。
我尝试在
中使用vjustimeline_plot<- timeline_plot+geom_text(aes(x=text_position,label=events),size=2.5 , vjus=c(1:lenght(tl$events))
但这绝对不是正确的方法。
非常感谢, 达斯汀
library(ggplot2)
library(scales)
library(lubridate)
tl$date <- as.Date(tl$date, format="%d-%m-%Y")
unique(tl$status)
status_levels <- c("Diagnosis", "Monitoring", "Symptoms", "Status", "Change", "Social")
status_colors <- c("#0070C0", "#00B050", "#FFC000", "#C00000", "darksalmon", "darkorchid3")
tl$status <- factor(tl$status, levels=status_levels, ordered=TRUE)
# Der er 20 unique time points i datasættet
positions <- c(0.2, 0.2, -0.5, 0.05, -0.3, 0.5, -0.5, 0.4, -0.2, -0.2, 0.1, 0.3, -0.7, 0.2, -0.4, -0.2, 0.5, -0.5, 0.2, -0.3)
directions <- c(1, 1, -1, 1, -1, 1, -1, 1, -1, - 1, 1, 1, -1, 1, -1, - 1, 1, -1, 1, -1)
line_pos <- data.frame(
"date"=unique(tl$date),
"position"=rep(positions, length.out=length(unique(tl$date))),
"direction"=rep(directions, length.out=length(unique(tl$date)))
)
tl <- merge(x=tl, y=line_pos, by="date", all = TRUE)
tl <- tl[with(tl, order(date, status)), ]
text_offset <- 0.1
tl$month_count <- ave(tl$date==tl$date, tl$date, FUN=cumsum)
tl$text_position <- (tl$month_count * text_offset * tl$direction) +tl$position
month_buffer1 <- 6
month_buffer2 <- 12
month_date_range <- seq(min(tl$date) - months(month_buffer1), max(tl$date) + months(month_buffer2), by='month')
month_format <- format(month_date_range, '%b')
month_tl <- data.frame(month_date_range, month_format)
year_date_range <- seq(min(tl$date) - months(month_buffer1), max(tl$date) + months(month_buffer2), 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_tl <- data.frame(year_date_range, year_format)
timeline_plot<-ggplot(tl,aes(x=0,y=date, col=status, label=events))
timeline_plot<-timeline_plot+labs(col="")
timeline_plot<-timeline_plot+scale_color_manual(values=status_colors, labels=status_levels, drop = FALSE) values=status_colors, labels=status_levels
timeline_plot<-timeline_plot+theme_classic()
timeline_plot<-timeline_plot+geom_vline(xintercept=0,
color = "black", size=0.3)
timeline_plot<-timeline_plot+geom_segment(data=tl[tl$month_count == 1,], aes(x=position,yend=date,xend=0), color='black', size=0.1) #size ændre fra 0.2 til 0.1
# Plot scatter points at zero and date
timeline_plot<-timeline_plot+geom_point(aes(x=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_tl, 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_tl, aes(y=year_date_range,x=-0.1,label=year_format, fontface="bold"),size=2, color='black')
# Show text for each milestone
timeline_plot<-timeline_plot+geom_text(aes(x=text_position,label=events),size=2.5)
print(timeline_plot)