我有一個資料集,其中包含有關個人隨時間作業的位置的資訊,其中時間定義為年/月(并在我的資料集中顯示為數值 YYYYMM)。我運行一個 ggplot 來可視化個人在給定作業場所停留的時間以及他們如何走動。position_dodge當同一個人在同一個月內在多個地方作業時,我曾經使它可見。
在下面的簡單示例中:
- 個人 A 從 2012 年 1 月(即 201201)到 2012 年 12 月就位 1 作業
- 個人 B 從 2012 年 1 月到 2012 年 6 月在位置 2 作業,然后從 2012 年 7 月到 2012 年 11 月切換到位置 2
- 個人 C 于 2012 年 1 月至 2012 年 4 月在位 1 作業,2012 年 2 月至 2012 年 6 月在位 2
- 個人 D 僅在 2012 年 1 月期間就地作業 1
我的查詢與如何使用時間間隔有關。在我的資料集中,時間段變數是指整個月。例如,個人 A 從 01/01/2012 到 31/12/2012 實際上在作業場所 1 作業,而個人 D 從 01/01/2012 到 31/01/2012 在作業場所 1 作業。
# individual A
a_id <- c(rep('A',12))
a_period <- c(seq(201201, 201212))
a_workplace <-c(rep(1,12))
# individual B
b_id <- c(rep('B',11))
b_period <- c(seq(201201,201206), seq(201207,201211))
b_workplace <-c(rep(1,6), rep(2,5))
# individual C
c_id <- c(rep('C',9))
c_period <- c(seq(201201,201204), seq(201202,201206))
c_workplace <-c(rep(1,4), rep(2,5))
# individual D
d_id <- c(rep('D',1))
d_period <- c(seq(201201,201201))
d_workplace <-c(rep(1,1))
# final data frame
id <- c(a_id, b_id, c_id, d_id)
period <- c(a_period, b_period, c_period, d_period)
workplace <- as.factor(c(a_workplace, b_workplace, c_workplace, d_workplace))
mydata <- data.frame(id, period, workplace)
ggplot(mydata, aes(x = id, y = period, color = workplace))
geom_line(position = position_dodge(width = 0.1), size = 2)
scale_x_discrete(limits = rev)
scale_y_continuous(breaks = seq(201201, 201212, by = 1))
coord_flip()
theme(axis.text.x = element_text(angle=45, hjust=1),
legend.position = c(.8, .2),
legend.direction = "vertical",
legend.background = element_rect(linetype = "solid", colour = "black"),
panel.background = element_rect(fill = "grey97"))
labs(y = "time", title = "Work affiliation")
上面的 ggplot 將年/月視為單個時間點。例如,它沒有顯示個人 D 的作業歷史。我如何考慮個人作業場所級別的每個連續序列,從第一個月的第一天開始并在連續序列的最后一個月的最后一天結束。我還想將年/月變數從數字格式轉換為日期格式,以便更輕松地進行操作。
PS: I highlight each consecutive sequence in the paragraph above because the same individual may work in a given place for a few months, leave for a time period and then return to working again in this same workplace later on. In these cases, the two time intervals the individual work in this given workplace should be considered separately.
uj5u.com熱心網友回復:
關于將數字轉換為日期型別的第二個問題,我得到了答案:
library(lubridate) # handling and conversion of datetype
lubridate::ymd() # turns your numeric into a date
as.Date() #turns your characterstring into date type which is by the way the
#proper way you should handover timerelated data to ggplot
這應該為您的代碼做:
mydata$period=lubridate::ymd(mydata[,2])
uj5u.com熱心網友回復:
也許這可以完成作業:D
mydata$period=as.Date(lubridate::ymd(mydata[,2]))
x11()
ggplot(mydata, aes(x = id, y = period, color = workplace))
geom_line(position = position_dodge(width = 0.1), size = 2)
scale_x_discrete(limits = rev)
coord_flip()
theme(axis.text.x = element_text(angle=45, hjust=1),
legend.position = c(.8, .2),
legend.direction = "vertical",
legend.background = element_rect(linetype = "solid", colour = "black"),
panel.background = element_rect(fill = "grey97"))
labs(y = "time", title = "Work affiliation")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/319256.html
標籤:r date ggplot2 sequence yearmonth
上一篇:混合大小寫幾乎完成,但for回圈長度與全名和字母長度不同
下一篇:在R中繪制回歸串列的系數
