如何在ggplot中的标签内订购主要的Y轴 [英] How to order primary Y axis within label in ggplot

查看:102
本文介绍了如何在ggplot中的标签内订购主要的Y轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ggplot库的新手.并尝试使用以下data.frame绘制图:

I am new to ggplot library. And trying to draw the plot using the following 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")

它生成以下图:

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

I would like to order the LoC axis i.e primary Y-axis within the label i.e. event (secondary Y-axis). Within each label, it should print a point on the primary Y-axis in ascending order (For event C (4 -> 10) and E(6 -> 10) are already in ascending order). How can I order the other points in ascending order within each label i.e. event (I tried to sort data using order and fct_inorder from forcats library but couldn't able to achieve the desired result as shown below)? How can I print a single label within each group?

请随时纠正我!

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

Any help would be great! Thank you!

推荐答案

使用包forcats在正确的轨道上. fct_inseq用于数字顺序,fct_rev用于翻转它,具体取决于您要的是小->大还是相反. 我还为您的事件添加了fct_reorder2(按loc和enddate排序),但您可能希望将其按字母顺序保留.

You were on the right track with the package forcats. fct_inseq for numeric order, and fct_rev to flip it depending if you want small->large or opposite. I also added a fct_reorder2 for your events (ordered by loc and enddate) but you might want to keep it in alphabetical order.

关于标签,最好创建一个新的数据框以设置标签的位置.我在下面使用group_by和summary进行了此操作,并在下面的注释中使用了其他选项.

About your labels, it's probably best to create a new data frame to set where the labels are. I've done this below with group_by and summarize, and with other options in the comments below.

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软件包(v0.3.0)创建于2020-10-21 sup>

Created on 2020-10-21 by the reprex package (v0.3.0)

这篇关于如何在ggplot中的标签内订购主要的Y轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆