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)