วิธีสั่งซื้อ ggplot geom_segment พร้อมสี

Oct 19 2020

ฉันยังใหม่กับ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")
max_date = max(df$startdate,df$enddate)

ใช้ข้อมูลโค้ดต่อไปนี้:

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

ฉันสามารถส่งออกแผนภูมิต่อไปนี้:

ฉันจะจัดลำดับแผนภูมิด้านบนโดยใช้สีเช่นการใช้eventฟิลด์ได้อย่างไร (หรืออีกนัยหนึ่งฉันจะจัดกลุ่มตามการดำเนินการบนeventฟิลด์ได้อย่างไรก่อนอื่นจึงควรแสดงเหตุการณ์ทั้งหมดก่อนจากนั้นเหตุการณ์ B, Cเป็นต้น) ฉันได้พยายามใช้scale_x_continuousและreorderจากtidyverseแพ็คเกจ แต่ไม่ได้ผล ฉันจะแสดง "ปี" เพิ่มเติมบนแกน x ได้อย่างไร ฉันพยายามใช้scale_x_date(กล่าวถึงที่นี่R: ggplot แสดงวันที่ทั้งหมดบนแกน xแต่ต้องการas.Dateและggplot geom_segmentต้องการas.POSIXctรูปแบบ) โปรดแก้ไขฉันด้วย!

ความช่วยเหลือใด ๆ จะดีมาก! ขอขอบคุณ!

คำตอบ

2 ArthurYip Oct 19 2020 at 04:41

สองตัวเลือก ฉันได้ย้อนกลับ x และ y ของคุณแล้วด้วยดังนั้นคุณจึงไม่ต้องใช้coord_flip()และทำการแก้ไขเล็ก ๆ น้อย ๆ อีกหลายอย่างรวมถึงป้ายชื่อแกน x (คุณกำลังมองหาscale_y_datetimeตั้งแต่คุณพลิกแกนและ "วันที่" อยู่ใน POSIXct) นอกจากนี้หนึ่งความแตกต่างกับคำตอบของเป็ดเป็นเครื่องชั่งน้ำหนักของฉัน = "ฟรี" facet_gridใน คุณอาจตัดสินใจว่าป้ายกำกับของคุณและตัวแปร "loc" ของคุณอาจไม่สมเหตุสมผลหากใช้กราฟใหม่เหล่านี้

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

สร้างเมื่อ 2020-10-18 โดยแพ็คเกจ reprex (v0.3.0)

1 Duck Oct 19 2020 at 04:36

พิจารณาสิ่งนี้เป็นตัวเลือกดังที่กล่าวไว้ในการจัดลำดับการตั้งค่าใหม่ของ@ArthurYipอาจส่งผลต่อความรู้สึกของพล็อต คุณสามารถหลีกเลี่ยงฉลากและใช้facet_grid()ในวิธีถัดไป:

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

เอาท์พุต: