【发布时间】:2021-02-01 16:42:33
【问题描述】:
我是 ggplot 库的新手。并尝试使用以下data.frame 绘制情节:
library(tidyverse)
df <-tribble(~event, ~startdate,~enddate,~loc,
"A",as.POSIXct("1984/02/10"),as.POSIXct("1987/06/10"),"1",
"B",as.POSIXct("1984/02/11"),as.POSIXct("1990/02/12"),"2",
"A",as.POSIXct("1992/05/15"),as.POSIXct("1999/06/15"),"3",
"C",as.POSIXct("2003/08/29"),as.POSIXct("2015/08/29"),"4",
"B",as.POSIXct("2002/04/11"),as.POSIXct("2012/04/12"),"5",
"E",as.POSIXct("2000/02/10"),as.POSIXct("2005/02/15"),"6")
max_date = max(df$startdate,df$enddate)
使用如下代码sn-p:
ggplot(NULL)+
geom_segment(data = df,aes(x=loc, xend =loc,y = startdate, yend = enddate,colour=event),size = 5,alpha=0.6) +
geom_label(aes(label=df$event,x = df$loc,y=max_date), size=2) +
#geom_point(data=final_df,aes(x=newspaper,y=date),color="black") + Point from other data frame
coord_flip() + xlab("LoC") + ylab("Year")
我如何使用颜色订购上述图表,即使用event 字段(换句话说,我如何对event 字段执行分组操作,以便首先它应该首先显示所有事件A然后是事件 B、C 等)?我尝试使用tidyverse 包中的scale_x_continuous 和reorder,但没有成功。如何在 x 轴上显示更多“年份”?我尝试使用scale_x_date(这里提到R: ggplot display all dates on x axis,但它需要as.Date和ggplotgeom_segment需要as.POSIXct格式)。请随时纠正我!
任何帮助都会很棒!谢谢!
【问题讨论】:
-
你可以按事件重新排序,但是你的 LoC 就没有意义了。