Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在散点图(R)中定义日期x轴_R_Date_Ggplot2 - Fatal编程技术网

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

在散点图(R)中定义日期x轴,r,date,ggplot2,R,Date,Ggplot2,我想生成一个数据帧的散点图,它有一个日期列。x轴应按月分割,或在过去2年中每2个月分割一次 数据帧: 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

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

数据帧:

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")
我想在y轴上绘制
code
,在x轴上绘制
date
。如何强制x轴每月进行分割?另外,当我运行绘图代码时,x轴格式看起来像mm/dddd,但我想要mm/yyyy。为什么我会得到这种格式

在一个闪亮的应用程序中,我有大约50个数据帧,比如
code\u 1000
。为了让事情更简单,我不会分享所有的代码


提前谢谢你们

我认为默认的日期解析器只是被DD-MM-YYY符号弄糊涂了。 如果使用lubridate解析日期,x轴看起来更合理(尽管可能不使用所需的主/次刻度)

我删除了qplot中日期的重新格式化,并添加了缩放功能

库(lubridate)
图书馆(比例尺)
#在海报的问题中含蓄
图书馆(GG2)

code_1000$date我的解决方案与上面的@MarkMiller非常相似,只是我尝试lubridate失败了。我用
strtime
来转换日期

code_1000$date <- strptime(code_1000$date, format = "%d-%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))

limits非常感谢各位的回答

我很难将这种日期格式应用到我的数据帧中

我有51个不同的数据帧,从
code\u 1000
code\u 1050
命名,而不是一个数据帧。我想应用这个日期格式

code_1000$date <- lubridate::dmy(as.character(code_1000$date))
其中,
输入$code.numbers
是一个数据帧,其中包含命名数据帧的数字(从1000到1050)。我得到了以下错误:

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

我想学习如何使用
for
lappy()
函数来实现这一点,正如我在R
lappy()
中读到的,在大多数情况下,这是一种更简单的方法。

谢谢您的回答!当我运行代码_1000$date时,我无法复制它-它对我来说很好,使用从这个页面复制的所有内容,只使用基本包。尝试重新启动R,不要加载任何包-可能与包函数冲突?我确实修改了我的答案,以包括情节所需的软件包。你可能想将此作为一个不同的问题发布,因为它与原来的有很大不同。。。我正在尝试学习使用purr::map函数来解决这种类型的问题。如果它们除了一个变量外几乎完全相同,那么您可以将它们合并成一个更易于使用的纵向格式数据帧?需要更多信息才能回答
code_1000$date <- lubridate::dmy(as.character(code_1000$date))
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)))

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