简体   繁体   中英

How to order geom_segment ggplot with colour

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")
max_date = max(df$startdate,df$enddate)

Using the following code snippet:

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

I can able to output the following chart: 在此处输入图片说明

How can I order the above chart using the colour ie using the event field (in other word how can I perform group by operation on the event field so that first it should display first all events A then events B, C etc.)? I have tried to use scale_x_continuous and reorder from tidyverse package but it didn't work. How can I display more "Year" on the x-axis? I tried to use scale_x_date (mentioned here R: ggplot display all dates on x axis but it needs as.Date and ggplot geom_segment needs as.POSIXct format). Please feel free to correct me!

Any help would be great! Thank you!

Two options. I've also reversed your x and y so you don't have to use coord_flip() and made several other small modifications including the x-axis labels (you were looking for scale_y_datetime since you flipped the axes and the "dates" were actually in POSIXct). Also, one difference with Duck's answer is my scales = "free" in facet_grid . You might decide your labels and your "loc" variable may not make sense given these new graphs anyway.

library(tibble); library(ggplot2)
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)

ggplot(df)+
  geom_segment(aes(y=event, yend = event, x = startdate, xend = enddate, colour=event),size = 5,alpha=0.6) +
  geom_label(aes(label=event, y = event, 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))


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

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

Consider this as an option, as mentioned by @ArthurYip setting reorder could affect the sense of your plot. You could avoid the labels and using facet_grid() in next way:

library(ggplot2)
#Plot
ggplot(df)+
  geom_segment(aes(x=loc, xend =loc,y = startdate, yend = enddate,colour=event),size = 5,alpha=0.6) +
  coord_flip() + xlab("LoC") + ylab("Year")+
  facet_grid(event~.,switch = "x")

Output:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM