首页 文章

在散点图(R)中定义日期x轴

提问于
浏览
1

我想生成一个具有日期列的数据框的散点图 . x轴应按月划分,或在过去2年中每2个月划分一次 .

The dataframe:

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

The plot code:

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

我想在y轴上绘制 code ,在x轴上绘制 date . 如何强制x轴按月划分?此外,当我运行我的绘图代码时,x轴格式看起来像mm / dddd,但我想要mm / yyyy . 为什么我会得到那种格式?

我在Shiny应用程序中有~50个数据帧,如 code_1000 . 为了简化操作,我不会共享所有代码 .

提前谢谢你们!

3 回答

  • 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

    非常感谢你的回答!

    我很难在Shiny中为我的数据帧应用这种日期格式 .

    我有51个不同的数据帧,而不是一个数据帧 code_1000 ,从 code_1000code_1050 命名 . 我想应用这种日期格式

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

    在这些数据框中的所有 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 是一个数据框,其中包含命名数据帧的数字(从1000到1050) . 我收到以下错误:

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

    我想学习如何使用 forlapply() 函数来做到这一点,因为我已经读过,在R lapply() 中,大多数时候都是一种更容易的方法 .

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

    如果要在Shiny图中设置限制一致,请设置特定时间限制:

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

相关问题