gpt4 book ai didi

r - 如何在ggplot中的标签内对主Y轴进行排序

转载 作者:行者123 更新时间:2023-12-03 16:32:26 26 4
gpt4 key购买 nike

我是新来的 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")
它生成以下图:
enter image description here
我想订购 LoC 轴,即标签内的主要 Y 轴,即事件(次要 Y 轴)。在每个标签中,它应该按升序在主 Y 轴上打印一个点(对于事件 C (4 -> 10) 和 E(6 -> 10) 已经按升序排列)。如何在每个标签中按升序对其他点进行排序,即事件(我尝试使用来自 order 库的 fct_inorderforcats 对数据进行排序,但无法获得所需的结果,如下所示)?如何在每个组内打印单个标签?
enter image description here
请随时纠正我!
任何帮助都会很棒!谢谢!

最佳答案

您使用包裹 forcats 走在正确的轨道上.fct_inseq对于数字顺序,和 fct_rev翻转它取决于你想要小->大还是相反。
我还添加了一个 fct_reorder2对于您的事件(按 loc 和 enddate 排序),但您可能希望按字母顺序保留它。
关于您的标签,最好创建一个新的数据框来设置标签的位置。我在下面使用 group_by 和 summary 以及下面评论中的其他选项完成了此操作。

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")

创建于 2020-10-21 由 reprex package (v0.3.0)

关于r - 如何在ggplot中的标签内对主Y轴进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64470942/

26 4 0