ggplot2 使用单独的日期和可变日期帧 r 按日期绘制时间序列 [英] ggplot2 to plot time series by date using seperate date and variable dateframes r
问题描述
我正在尝试绘制时间序列.在一个数据框(y)中,我在列向量中有 56 个项目,并且我有第二个数据框(日期)和相应的日期.我试图将时间序列绘制为 y 轴上的 y 值和 x 轴上的日期.我已经使用 ggplt2 geom_freqpoly 尝试了很多事情,但我无法弄清楚.我对 ggplot 之外的其他方法持开放态度,我也可以将日期和 y 绑定到一个日期框架中,如果它能让事情变得更容易的话.
有什么建议吗?
库(ggplot2)set.seed(123)N<- 500M<-56x<- 矩阵(rnorm(N*M,mean=23,sd=3), N, M)y <- colMeans(x,dim=1)y <-as.data.frame(y)日期 <- seq(as.Date("2018-01-01"), as.Date("2018-02-25"), by="days")日期 <- as.POSIXct(日期, 格式 = "%Y-%m-%d %H:%M")a <- ggplot(日期,aes(y))a + geom_freqpoly()
这里有几个不同包的方法.
ggplot2
ggpubr
格子
我们还可以使用
ggvis
基础 R
我们也可以使用基数 R.
plot(dat$Date, dat$y, xaxt = "n", type = "l", xlab = "Date", ylab = "y")axis.POSIXct(1, at = seq(min(dat$Date), max(dat$Date), by = "week"), format="%b %d")
xts
我们还可以将数据框转换为
性能分析
我们还可以使用
dygraphs
情节
我们也可以使用
我们也可以使用
I'm trying to plot a time series. In one dataframe(y), I have 56 items in a column vector and I have a second dataframe (dates) with corresponding dates. I am trying to graph the time series as the values of y on the y-axis and the dates on the x axis. I have tried a number of things using ggplt2 geom_freqpoly but I can't figure it out. I'm open to other methods besides ggplot and i can cbind date and y into one dateframe as well if it will make things easier.
Any advice?
library(ggplot2)
set.seed(123)
N<- 500
M<-56
x<- matrix( rnorm(N*M,mean=23,sd=3), N, M)
y <- colMeans(x,dim=1)
y <-as.data.frame(y)
Date <- seq(as.Date("2018-01-01"), as.Date("2018-02-25"), by="days")
Date <- as.POSIXct(Date, format = "%Y-%m-%d %H:%M")
a <- ggplot(date, aes(y))
a + geom_freqpoly()
Here are methods from several different packages.
ggplot2
The ggplot2 package works the best on a data frame, so I would suggest you to create a data frame with your data. In addition, not sure why do you want to use geom_freqpoly
. I think geom_line
will work for time-series data.
library(ggplot2)
set.seed(123)
N<- 500
M<-56
x<- matrix( rnorm(N*M,mean=23,sd=3), N, M)
y <- colMeans(x,dim=1)
Date <- seq(as.Date("2018-01-01"), as.Date("2018-02-25"), by="days")
Date <- as.POSIXct(Date, format = "%Y-%m-%d %H:%M")
dat <- data.frame(Date = Date, y = y)
ggplot(dat, aes(x = Date, y = y)) +
geom_line() +
theme_classic()
ggpubr
ggpubr is an extension of the ggplot2 package. We can use ggline
package to plot the data.
library(ggpubr)
ggline(data = dat, x = "Date", y = "y")
lattice
We can also use the xyplot
function from the lattice package.
library(lattice)
xyplot(y ~ Date, data = dat, type = "l")
ggvis
The ggvis package, similar to ggplot, uses grammar of graphics to create plots.
library(ggvis)
ggvis(dat, ~Date, ~y) %>% layer_lines()
Base R
We can also use the base R.
plot(dat$Date, dat$y, xaxt = "n", type = "l", xlab = "Date", ylab = "y")
axis.POSIXct(1, at = seq(min(dat$Date), max(dat$Date), by = "week"), format="%b %d")
xts
We can also convert the data frame to an xts object and then plot it.
library(xts)
dat.ts <- xts(dat$y, order.by = dat$Date)
plot(dat.ts)
PerformanceAnalytics
We can also use the chart.TimeSeries
from the performanceanalytics package to plot the xts
package.
chart.TimeSeries(dat.ts)
dygraphs
The dygraphs package can create interactive time-series plot.
library(dygraphs)
dygraph(dat.ts)
plotly
We can also use plotly to create interactive plot.
library(plotly)
plot_ly(x = ~dat$Date, y = ~dat$y, mode = 'lines')
We can also use highcharter package to create interactive plot.
library(highcharter)
hchart(dat.ts)
这篇关于ggplot2 使用单独的日期和可变日期帧 r 按日期绘制时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!