ggplot2使用单独的日期和可变日期框架按日期绘制时间序列 [英] ggplot2 to plot time series by date using seperate date and variable dateframes r
问题描述
我正在尝试绘制时间序列.在一个数据帧(y)中,我在列向量中有56个项目,在第二个数据帧(日期)中有相应的日期.我正在尝试将时间序列绘制为y轴上的y值和x轴上的日期.我已经尝试过使用ggplt2 geom_freqpoly进行许多操作,但是我无法弄清楚.我对ggplot以外的其他方法持开放态度,如果可以使事情变得更容易,我也可以将date和y绑定到一个dateframe中.
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.
有什么建议吗?
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
ggplot2 软件包在数据框,所以我建议您使用数据创建一个数据框.另外,不确定为什么要使用geom_freqpoly
.我认为geom_line
将适用于时间序列数据.
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 是 ggplot2 软件包.我们可以使用ggline
包来绘制数据.
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的问题中的xyplot
函数包.
We can also use the xyplot
function from the lattice package.
library(lattice)
xyplot(y ~ Date, data = dat, type = "l")
ggvis
ggvis 软件包的问题,类似于 ggplot 的问题,使用图形语法创建绘图.
The ggvis package, similar to ggplot, uses grammar of graphics to create plots.
library(ggvis)
ggvis(dat, ~Date, ~y) %>% layer_lines()
基本R
我们也可以使用底数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
我们还可以将数据框转换为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
我们还可以使用performanceanalytics 包以绘制xts
包.
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 情节.
library(plotly)
plot_ly(x = ~dat$Date, y = ~dat$y, mode = 'lines')
我们还可以使用 highcharter 包来创建互动情节.
We can also use highcharter package to create interactive plot.
library(highcharter)
hchart(dat.ts)
这篇关于ggplot2使用单独的日期和可变日期框架按日期绘制时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!