1

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:enter image description here

How can I order the above chart using the colour i.e. 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!

Saurabh Chauhan
  • 3,161
  • 2
  • 19
  • 46

2 Answers2

2

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)

Arthur Yip
  • 5,810
  • 2
  • 31
  • 50
  • You might keep the "loc" variable so that each item has its own row/line.If they don't really mean anything, then you could cover up the y-axis in post processing. – Arthur Yip Oct 19 '20 at 17:25
  • Could you please help me here: https://stackoverflow.com/questions/64470942/how-to-order-primary-y-axis-within-label-in-ggplot – Saurabh Chauhan Oct 21 '20 at 20:03
1

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:

enter image description here

Duck
  • 39,058
  • 13
  • 42
  • 84