R - การจัดรูปแบบข้อมูลต่อเดือนและการตัดด้านต่อปี

ฉันกำลังฝึกใช้ R และประสบความเร็จอย่างรวดเร็วในขณะที่พยายามสร้างกราฟของผู้โดยสารสายการบินต่อเดือน

ฉันต้องการแสดงกราฟเส้นรายเดือนแยกกันในแต่ละปีตั้งแต่ปี 1949 ถึง 1960 โดยมีการบันทึกข้อมูลไว้ ในการทำเช่นนี้ ฉันได้ใช้ ggplot เพื่อสร้างกราฟเส้นที่มีค่าต่อเดือน วิธีนี้ใช้งานได้ดี แต่เมื่อฉันพยายามแยกสิ่งนี้ตามปีโดยใช้ facet_wrap() และจัดรูปแบบฟิลด์ month ปัจจุบัน: facet_wrap(format(air$month[seq(1, length(air$month), 12)], "%Y")); มันส่งคืนสิ่งนี้:

กราฟส่งคืน

ฉันได้พยายามจัดรูปแบบด้านด้วยการป้อนลำดับของตัวเองเป็นเวลาหลายปี: rep(c(1949:1960), each = 12) สิ่งนี้ให้ผลลัพธ์ที่แตกต่างออกไปซึ่งดีกว่าแต่ก็ยังผิดอยู่:

กราฟที่สอง

นี่คือรหัสของฉัน:

air = data.frame(
  month = seq(as.Date("1949-01-01"), as.Date("1960-12-01"), by="months"),
  air = as.vector(AirPassengers)
)


ggplot(air, aes(x = month, y = air)) +
  geom_point() +
  labs(x = "Month", y = "Passengers (in thousands)", title = "Total passengers per month, 1949 - 1960") +
  geom_smooth(method = lm, se = F) + 
  geom_line() +
  scale_x_date(labels = date_format("%b"), breaks = "12 month") +
  facet_wrap(format(air$month[seq(1, length(air$month), 12)], "%Y"))
#OR
  facet_wrap(rep(c(1949:1960), each = 12))

แล้วฉันจะสร้างกราฟรายบุคคลต่อปีได้อย่างไร?

ขอบคุณ!


person Lucas Peters-Murphy    schedule 14.06.2020    source แหล่งที่มา


คำตอบ (1)


ในความพยายามครั้งที่สอง คุณเข้าใกล้มาก ปัญหาหลักของข้อมูลคือคุณกำลังพยายามสร้างพล็อตเหลี่ยมที่มีค่าแกน x ต่างกัน (วันที่รวมถึงปี) วิธีแก้ปัญหาง่ายๆ ในการแก้ไขคือแปลงข้อมูลเป็นสเกลแกน x "ทั่วไป" จากนั้นจึงทำพล็อตแบบประกอบ นี่คือโค้ดที่ควรส่งออกพล็อตที่ต้องการ

library(tidyverse)
library(lubridate)

air %>%
  # Get the year value to use it for the facetted plot
  mutate(year = year(month),
         # Get the month-day dates and set all dates with a dummy year (2021 in this case)
         # This will get all your dates in a common x axis scale
         month_day = as_date(paste(2021,month(month),day(month), sep = "-"))) %>%
  # Do the same plot, just change the x variable to month_day
  ggplot(aes(x = month_day, 
             y = air)) +
  geom_point() +
  labs(x = "Month", 
       y = "Passengers (in thousands)", 
       title = "Total passengers per month, 1949 - 1960") +
  geom_smooth(method = lm, 
              se = F) + 
  geom_line() +
  # Set the breaks to 1 month
  scale_x_date(labels = scales::date_format("%b"), 
               breaks = "1 month") +
  # Use the year variable to do the facetted plot
  facet_wrap(~year) +
  # You could set the x axis in an 90° angle to get a cleaner plot
  theme(axis.text.x = element_text(angle = 90,
                                   vjust = 0.5,
                                   hjust = 1))

พล็อตแง่มุมตามปี

person Jonathan V. Solórzano    schedule 14.06.2020
comment
น่าทึ่ง ทำงานได้อย่างสมบูรณ์แบบ! ขอบคุณ. คำถามหนึ่งข้อ: เมื่อกลายพันธุ์ ทำไมคุณถึงเพิ่ม year = year(month) ดูเหมือนจะไม่เปลี่ยนแปลงอะไรเกี่ยวกับ data frame ตามสายตาที่ไม่ได้รับการฝึกฝนของฉัน ขอบคุณ! - person Lucas Peters-Murphy; 15.06.2020
comment
year เป็นฟังก์ชันจากแพ็คเกจ lubridate ที่ได้รับปีของวันที่ (ในกรณีนี้คือคอลัมน์ชื่อเดือน) ดังนั้นบรรทัด year = year(month) จึงเป็นการสร้างคอลัมน์ใหม่ชื่อปีซึ่งมีปีของคอลัมน์วันที่ของคุณ - person Jonathan V. Solórzano; 15.06.2020