我的原始数据是周数据,如何在r中将其绘制为周数据?
我的数据最初以周为单位(以下示例)。我发现很难执行时间序列数据,因为该数据始终位于我的原始数据是周数据,如何在r中将其绘制为周数据?,r,ggplot2,time-series,R,Ggplot2,Time Series,我的数据最初以周为单位(以下示例)。我发现很难执行时间序列数据,因为该数据始终位于dd-mm-yy的起始位置 WEEK SALES 1: 29.2010 60.48 2: 30.2010 95.76 3: 31.2010 51.66 4: 32.2010 73.71 5: 33.2010 22.05 提前谢谢 我们可以使用lubridate包中的函数将周数转换为日期,然后在x轴上绘制日期,在y轴上绘制销售 library(tidyverse) lib
dd-mm-yy
的起始位置
WEEK SALES
1: 29.2010 60.48
2: 30.2010 95.76
3: 31.2010 51.66
4: 32.2010 73.71
5: 33.2010 22.05
提前谢谢 我们可以使用
lubridate
包中的函数将周数转换为日期,然后在x轴上绘制日期,在y轴上绘制销售
library(tidyverse)
library(lubridate)
dat2 <- dat %>%
separate(WEEK, into = c("WEEK", "YEAR"), convert = TRUE) %>%
mutate(Date = ymd("2010-01-01") + weeks(WEEK - 1))
ggplot(dat2, aes(x = Date, y = SALES)) +
geom_line()
库(tidyverse)
图书馆(lubridate)
dat2%
单独(周,转换为=c(“周”,“年”),转换为=真)%>%
突变(日期=ymd(“2010-01-01”)+周(第1周))
ggplot(dat2,aes(x=日期,y=销售额))+
geom_线()
数据
dat <- read.table(text = " WEEK SALES
1 '29.2010' 60.48
2 '30.2010' 95.76
3 '31.2010' 51.66
4 '32.2010' 73.71
5 '33.2010' 22.05",
header = TRUE, stringsAsFactors = FALSE,
colClasses = c("character", "character", "numeric"))
dat <- read.table(text = " WEEK SALES
1 '29.2010' 60.48
2 '30.2010' 95.76
3 '31.2010' 51.66
4 '32.2010' 73.71
5 '33.2010' 22.05
6 '1.2011' 37.5
7 '2.2011' 45.2
8 '3.2011' 62.9",
header = TRUE, stringsAsFactors = FALSE,
colClasses = c("character", "character", "numeric"))
dat%
突变(日期=ymd(粘贴(年份,“01”,“01”,“sep=“-”)))+周(第1周))
数据
dat <- read.table(text = " WEEK SALES
1 '29.2010' 60.48
2 '30.2010' 95.76
3 '31.2010' 51.66
4 '32.2010' 73.71
5 '33.2010' 22.05",
header = TRUE, stringsAsFactors = FALSE,
colClasses = c("character", "character", "numeric"))
dat <- read.table(text = " WEEK SALES
1 '29.2010' 60.48
2 '30.2010' 95.76
3 '31.2010' 51.66
4 '32.2010' 73.71
5 '33.2010' 22.05
6 '1.2011' 37.5
7 '2.2011' 45.2
8 '3.2011' 62.9",
header = TRUE, stringsAsFactors = FALSE,
colClasses = c("character", "character", "numeric"))
dat ggplot(数据,aes(x=周,y=销售,组=1)+geom_线()@ConorNeilson我试过了。它没有给出正确的绘图。@DoraFan我已经根据您的新请求更新了我的答案。我还回滚了您的编辑,因为您提供的新数据框与旧示例的标题不同,这可能会让未来的读者感到困惑。但我已经知道您正在处理的数据可能是什么样子。非常感谢太多了!这非常适合2010年的日期!但是,我有2011-2018年的数据,所以到了2011年,数据仍然停留在2010年,我如何修复它?谢谢!@DoraFan请更新您的示例数据框,以反映不同年份的数据。我更新了它,但我没有设法将整个表放在那里。@DoraFan如果我的帖子是正确的话OLVES你的问题,请考虑接受它作为答案。@多拉凡如果我的帖子解决了你的问题,请考虑接受它作为答案。