2017-05-18 175 views
1

我想生成具有日期列的數據框的散點圖。 x軸應該按月進行分割,或者在過去的2年中每2個月進行一次分割。在散點圖(R)中定義日期x軸

數據框:

code_1000 <- 
    as.data.frame(cbind(
    c("3", "3", "7", "7", "7", "7", "2", "2", "4", "4"), 
    c("344", "344", "73", "73", "71", "72", "21", "27", "42", "43"), 
    c("9-02-2017", "10-01-2016","9-02-2014", "25-03-2015", "9-02-2017", 
     "10-06-2017", "8-04-2017", "25-08-2016", "07-08-2017", "15-09-2016" 
    ) 
)) 
names(code_1000) <- c("number", "code", "date") 

劇情代碼:

qplot(data=code_1000, 
     x=format(as.Date(date),"%b/%Y"), 
     y=code, 
     geom=c("point"), 
     na.rm=TRUE, 
     xlab="Emission date", ylab="Code", 
     size=1, col=2)+theme_bw()+theme(legend.position="none") 

我想繪製在x軸上code在y軸和date。我如何強制x軸按月分割?另外,當我運行我的繪圖代碼時,x軸格式看起來像mm/dddd,但我想要mm/yyyy。我爲什麼得到這種格式?

我有50個數據框像code_1000在一個Shiny應用程序。爲了使事情更簡單,我沒有分享所有的代碼。

謝謝各位提前!

回答

1

我認爲默認的日期解析器只是被你的DD-MM-YYY表示法所困惑。 如果您解析與lubridate日期,x軸看起來更合理(儘管可能不是主要/次要蜱你想要的。)

我刪除您的日期重新格式化qplot內,並添加了縮放功能。

library(lubridate) 
library(scales) 

# implicit in poster's question 
library(ggplot2) 

code_1000$date <- lubridate::dmy(as.character(code_1000$date)) 

qplot(
    data = code_1000, 
    x = date, 
    y = code, 
    geom = c("point"), 
    na.rm = TRUE, 
    xlab = "Emission date", 
    ylab = "Code", 
    size = 1, 
    col = 2 
) + theme_bw() + theme(legend.position = "none") + scale_x_date(
    date_breaks = "1 year", 
    date_minor_breaks = "1 month", 
    labels = date_format("%m-%Y") 
) 

enter image description here

0

我的解決辦法結束了非常類似上面@MarkMiller,除了我在lubridate嘗試未能奏效。我用strptime來代替日期。

code_1000$date <- strptime(code_1000$date, format = "%d-%M-%Y") 

此外,我覺得ggplot功能是代替qplot更加靈活和簡潔。裏面一個閃亮的應用特別是,qplot可能會給不同的結果(?):

library(tidyverse) 
library(scales) # needed for date_format() 
ggplot(code_1000, aes(date, code)) + 
    geom_point(size=2, col="steelblue") + 
    theme_bw() + 
    labs(x="Emission Date", y="Code") + 
    scale_x_datetime(labels = date_format("%m/%Y")) 

如果你想設置的限制必須跨越閃亮地塊一致,設定具體的時間限制:

limits <- strptime(c("01-01-2014", "01-01-2018"), format = "%d-%m-%Y") 
ggplot(code_1000, aes(date, code)) + 
    geom_point(size=2, col="steelblue") + theme_bw() + 
    labs(x="Emission Date", y="Code") + 
    scale_x_datetime(labels = date_format("%m-%Y"), #minor_breaks = "1 month" 
        date_breaks = "1 year", limits = as.POSIXct(limits)) 
+0

謝謝你的回答馬特!當我運行 code_1000 $ date < - strptime(code_1000 $ date,format =「%d-%M-%Y」) 日期列成爲。你知道它發生的原因嗎?再次感謝。 –

+0

我無法重現,只有使用基本包才能正常工作,使用從此頁複製的所有內容。嘗試重新啓動R並且不加載任何包 - 可能與包函數衝突?我確實修改了我的答案,以包含劇情需要的軟件包。 –

0

謝謝非常適合你的答案!

我很難在Shiny中爲我的數據框應用這種日期格式。

取代一個數據幀code_1000,我有51個不同的數據幀,從code_1000code_1050。我想所有這些dataframes的date列應用此日期格式

code_1000$date <- lubridate::dmy(as.character(code_1000$date)) 

。我試圖用for來做到這一點,但它變得有點令人困惑,並沒有奏效。

for (m in 1:nrow(input)){ 

    assign(paste0("code_",input$code.numbers[m])$date, lubridate::dmy(as.character(eval(parse(text=paste0("code_",input$code.numbers[m])))$date))) 

    } 

input$code.numbers是包含名爲dataframes(1000年至1050年)的數字的數據幀。我得到了以下錯誤:

Error in paste0("code_",input$code.numbers[m])$date : 
    $ operator is invalid for atomic vectors 

我想學習如何做到這一點使用forlapply()功能,因爲我已閱讀,R中lapply()是大多數時候比較簡單的方法。

+0

您可能想將其作爲一個不同的問題發佈,因爲它與原始版本顯着不同...我試圖學習如何使用purr :: map函數來處理此類型的問題。如果除了變量外,它們幾乎完全相同,那麼可以將它們合併到一個更容易處理的縱向格式數據框中?需要更多信息才能回答 –