如何在ggplot中的标签内排序主Y轴

How to order primary Y axis within label in ggplot

我是 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",
      "A",as.POSIXct("1985/02/10"),as.POSIXct("1987/06/10"),"7",
      "B",as.POSIXct("1989/02/11"),as.POSIXct("1990/02/12"),"8",
      "A",as.POSIXct("1997/05/15"),as.POSIXct("1999/06/15"),"9",
      "C",as.POSIXct("2010/08/29"),as.POSIXct("2015/08/29"),"10",
      "B",as.POSIXct("2010/04/11"),as.POSIXct("2012/04/12"),"11",
      "E",as.POSIXct("2004/02/10"),as.POSIXct("2005/02/15"),"12")
max_date = max(df$startdate,df$enddate)

使用以下代码片段:

ggplot(df)+
  geom_segment(aes(y=loc, yend = loc, x = startdate, xend = enddate, colour=event),size = 5,alpha=0.6) +
  geom_label(aes(label=event, y = loc, x=max_date), size=2) +
  xlab("Year") + ylab("LoC") +
  scale_x_datetime(date_breaks = "year", date_labels = "%Y") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5)) +
  facet_grid(rows = vars(event), scales = "free")

它生成以下图:

我想订购 LoC 轴,即标签内的主 Y 轴,即事件(次 Y 轴)。在每个标签中,它应该按升序在主 Y 轴上打印一个点(对于事件 C (4 -> 10) 和 E(6 -> 10) 已经按升序排列)。如何在每个标签内按升序排列其他点,即事件(我尝试使用 forcats 库中的 orderfct_inorder 对数据进行排序,但无法达到预期的结果如下所示)?如何在每个组中打印一个标签?

欢迎指正!

任何帮助都会很棒!谢谢!

由于 df$loc 是一个字符,当 ggplot 将其转换为因子以将其用作 color 参数时,它会按字母顺序选择它的级别:

df$loc = as.factor(df$loc)

"10" 放在 "2" 前面。但是您希望它按数字顺序排列,因此您需要通过执行以下操作将其转换为数字:

df$loc = factor(as.numeric(df$loc), levels=12:1)

levels=12:1是重新排列关卡的顺序,让小值出现在大值之前。

这样就解决了顺序。我不知道如何解决的标签。

您的包裹 forcats 走在了正确的轨道上。 fct_inseq 用于数字顺序,fct_rev 用于翻转它,具体取决于您想要小->大还是相反。 我还为您的活动添加了一个 fct_reorder2(按地点和结束日期排序),但您可能希望按字母顺序排列。

关于您的标签,最好创建一个新的数据框来设置标签的位置。我在下面使用 group_by 完成了此操作并进行了总结,并在下面的评论中使用了其他选项。

library(tidyverse, quietly = TRUE, warn.conflicts = FALSE)
#> Warning: package 'ggplot2' was built under R version 3.6.3
#> Warning: package 'tibble' was built under R version 3.6.3
#> Warning: package 'tidyr' was built under R version 3.6.3
#> Warning: package 'purrr' was built under R version 3.6.3
#> Warning: package 'dplyr' was built under R version 3.6.3
#> Warning: package 'forcats' was built under R version 3.6.3
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",
          "A",as.POSIXct("1985/02/10"),as.POSIXct("1987/06/10"),"7",
          "B",as.POSIXct("1989/02/11"),as.POSIXct("1990/02/12"),"8",
          "A",as.POSIXct("1997/05/15"),as.POSIXct("1999/06/15"),"9",
          "C",as.POSIXct("2010/08/29"),as.POSIXct("2015/08/29"),"10",
          "B",as.POSIXct("2010/04/11"),as.POSIXct("2012/04/12"),"11",
          "E",as.POSIXct("2004/02/10"),as.POSIXct("2005/02/15"),"12") %>%
  mutate(event = event %>% fct_reorder2(enddate, loc),
         loc = loc %>% fct_inseq() %>% fct_rev())
max_date = max(df$startdate,df$enddate)

df_for_labels <- df %>% group_by(event) %>% 
  summarize(date = max_date, # max(enddate) would give you the label at each event's max(enddate)
            loc = n()) # 1 for bottom-right. n() for top-right (counts number of rows in each event)
#> `summarise()` ungrouping output (override with `.groups` argument)

ggplot(df)+
  geom_segment(aes(y=loc, yend = loc, x = startdate, xend = enddate, colour=event),size = 5,alpha=0.6) +
  geom_label(data = df_for_labels, aes(label=event, y = loc, x=date), size=2) +
  xlab("Year") + ylab("LoC") +
  scale_x_datetime(date_breaks = "year", date_labels = "%Y") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5)) +
  facet_grid(rows = vars(event), scales = "free")

reprex package (v0.3.0)

于 2020-10-21 创建