Display density() graph with date in x axis using R - r

I have a partial success with
input = "date,data
1-1-2015,5.5
2-1-2016,1.0
3-1-2016,4.0
4-1-2016,4.0
5-1-2019,3.0"
new = read.csv(text=input)
new$date = as.Date(new$date, "%d-%m-%Y")
new$date = as.numeric(new$date, as.Date("2015-01-01"), units="days") #https://stat.ethz.ch/pipermail/r-help/2008-May/162719.html
plot(density(new$date))
Resulting in working graph, unfortunately x axis is obviously formatted as integers. How can I produce graph with x axis formatted as data?
I expected
new = read.csv(text=input)
new$date = as.Date(new$date, "%d-%m-%Y")
plot(density(new$date))
to work, unfortunately it crashed with Error in density.default(new$date) : argument 'x' must be numeric.

density() wasn't really optimized to work with dates. The easiest fix would probably be to just replace the default axis labeling with date values. Here's how you can do that
plot(density(new$date), xaxt="n")
at<-axTicks(1)
axis(1,at, as.Date(at, origin="1970-01-01"))

Related

Error in Plotting a Time Series in R

I have a time series which goes like this
Time,Return
1,1.22
2,0.95
3,0.25
4,0.25
5,-1.51
I wanted to plot a simple time series - time on X axis and return on Y axis
dataset <- read.csv("DWDMData.csv",TRUE)
plot(y = dataset$Return,x = dataset$Time,type="1")
However, I get an error saying
Error in plot.xy(xy, type, ...) : invalid plot type '1'
What is the reason for this error? How do I fix it?
Have a look at ?plot for available plot types.
I'm assuming you want to plot a line graph:
plot(x = dataset$Time, y = dataset$Return, type = "l")

rcharts nPlot not recognizing quarter values on x-axis

Still very new to using rCharts and R, so please excuse me if the question sounds very stupid!
I'm trying to plot a time series chart using Quarter labels along the x-axis, simple example:
quarters <- c("Q413","Q313","Q213","Q13")
values <- c("120","40","60","80")
testing = data.frame(quarters,values)
tfrPlot <- nPlot(x="quarter", y="values", data = testing, type = "lineChart")
But this doesn't plot the graph and instead generates value between -1 and 1 on the x-axis. I made sure the quarters were factors as well, so I don't know how to proceed.
The error is primarily caused by a typo x="quarter which should be x=quarters, but even with this we will have errors. nvd3 with lineChart expects y to be numeric or continuous, so values<-as.numeric(c("120","40","60","80")) will also be required. Then one last thing the date conversion from R to Javascript in rCharts is still not optimal. One way to force it to work would be to pass the date in numeric form and then tell nvd3 how to handle it. Here is an example:
quarters <- as.Date(c("2013-03-31", "2013-06-30", "2013-09-30", "2013-12-31"))
values <- as.numeric(c("120","40","60","80"))
testing = data.frame(quarters,values)
tfrPlot <- nPlot(x="quarters", y="values", data = testing, type = "lineChart")
tfrPlot$xAxis(
tickFormat =
"#! function(d) {
return d3.time.format('%b %Y')(new Date(d * 24 * 60 * 60 * 1000))
} !#"
)
tfrPlot
You will probably agree this is more painful than it should be, and we are working on a much better way to handle this.

plotting DCC results with R

I have been running a dcc garch on R; the results is presented as matrix
I would like to extract the second column as a vector to plot, with date on the x-axis.
For the moment, if I define
DCCrho = dccresults$DCC[,2]
then head(DCCrho) yields this:
1 0.9256281
2 0.9256139
3 0.9245794
...
any help to redefine this as a simple vector of numerical values?
any other option to graph the results of dcc with date on the x-axis?
Thanks a lot!
While trying this
x <- cbind(DCCrho, com_30[,2])
head(x)
and this:
matplot(DCCrho ~ x[,2], x, xaxt = "n", type='l')
yields the following error message:
"Error in array(x, c(length(x), 1L), if (!is.null(names(x))) list(names(x), :
invalid first argument"
Apparently it was a matter of length of the vector; the Date and the DCC results need to be vectors of same length.
One also needs to plot both date and DCCrho as shown below.
matplot(com_30$date, DCCrho, xaxt = "n", type='l')
axis(1, com_30$date, format(com_30$date, "%y"), cex.axis = .7)
I'm assuming that when you said, "I would like to extract the second row..." that you actually meant "column", because you did the following: dccresults$DCC[,2] Also, as pointed out by a previous comment, the code isn't reproducible, so it's difficult to propose and answer with certainty. However, I'll do my best.
You said you wanted DCCrho to be a "simple vector of numerical values". I'm assuming that this is largely a matter of the way that the values are displayed. Does DCCrho = as.vector(dccresults$DCC[,2]) look better?.
As for the error message, I think that it's b/c in matplot(x,y, ...), x can't be a formula. Try matplot(DCCrho, x[,2]).
If you just want to plot the DCCrho value across some index, you could try something like the following:
Y <- as.vector(dccresults$DCC[,2])
X <- seq_along(Y)
plot(X,Y)
Does that work? Aside from the arbitrary time index, what did you intend to reference as "time"? I don't see a part of the code that you supplied (e.g., a column in dccresults$DCC) that would be an obvious candidate for use as a "date".

Time data values in R

how can I have a data set of only time intervals (no dates) in R, like the following:
TREATMENT_A TREATMENT_B
1:01:12 0:05:00
0:34:56 1:08:09
and compute mean times, etc, and draw boxplots with time intervals in the y-axis?
I am new to R, and I searched for this but found no example in the net.
Thanks
The chron-package has a 'times' class that supports arithmetic. You could also do all of that with POSIXct objects and format the date-time output to not include the date. I thought axis.POSIXct function has a format argument that should let you have time outputs. However, it does not seem to get dispatched properly, so I needed to construct the axis "by hand."
dft <- data.frame(x= factor( sample(1:2, 100, repl=TRUE)),
y= Sys.time()+rnorm(100)*4000 )
boxplot(y~x, data=dft, yaxt='n')
axis(2, at=seq(from=range(dft$y)[1], to =range(dft$y)[2], by=3000) ,
labels=format.POSIXct(seq(from=range(dft$y)[1], to =range(dft$y)[2], by=3000),
format ="%H:%M:%S") )
There did turn out to be an appropriate method, Axis.POSIXt (to which I thought boxplot should have been turning for plotting, but it did not seem to recognize the class of the 'y' argument):
boxplot(y~x, data=dft, yaxt='n')
Axis(side=2, x=range(dft$y), format ="%H:%M:%S")
Regarding your request for something "simpler", take a look at theis ggplot2 based solution, using the dft dataframe defined above with POSIXct times. (I did try with the chron-times object but got a message saying ggplot did not support that class):
require(ggplot2); p <- ggplot(dft, aes(x,y))
p + geom_boxplot()
Check out the "lubridate" package, and the "hms" function within it.

R plot with an x time axis: how to force the ticks labels to be the days?

I have this file in csv format:
timestamp,pages
2011-12-09T11:20:50.33,4
2012-01-23T17:44:02.71,132
2012-01-28T15:07:59.34,168
The first column is a timestamp, the second one is a page count.
I need to plot the page count on the vertical axis and the timestamp on the horizontal axis.
The timestamps are not regularly spaced, I have one day in december ant two close days in january.
I tried this code
df = read.csv("my_data.csv")
df$timestamp = strptime(df$timestamp, "%Y-%m-%dT%H:%M:%S")
plot(df$timestamp,df$pages)
and I got a plot with just one tick on the middle of the x axis and with the label "Jan": it's not wrong but I would like to have three ticks with just the day number and the month.
I tried
plot(df$timestamp,df$pages,xaxt="n")
axis.Date(1,df$timestamp,"days")
but no x axis is plotted.
Any idea?
Thank you
I would as.Date() your timestamp like this:
df$timestamp = as.Date(strptime(df$timestamp, "%Y-%m-%dT%H:%M:%S"))
This works then:
plot(df$timestamp,df$pages,xaxt="n")
axis.Date(1,at=df$timestamp,labels=format(df$timestamp,"%b-%d"),las=2)
This will work:
plot(df$timestamp,df$pages,xaxt="n")
axis.POSIXct(1, at=df$timestamp, labels=format(df$timestamp, "%m/%d"))
Essentially in axis.POSIXct (note that you have POSIXct dates in your data frame) you specify where to have the axis ticks (at) and what the labels are.
Typically I like my dates label vertical rather than horizontal. To get that use par(las=2) before the plot.
I found this: http://personality-project.org/r/r.plottingdates.html
Which gave me my solution...
dm = read.csv("my_data.csv", sep=",", head=TRUE)
dm$DateTime <- as.POSIXct(dm$timestamp, format="%Y-%m-%dT%H:%M:%S")
daterange=c(as.POSIXlt(min(dm$DateTime)), as.POSIXlt(max(dm$DateTime)))
plot(pages ~ DateTime, dm, xaxt = "n")
axis.POSIXct(1, at=seq(daterange[1], daterange[2], by="day"), format="%b %d")
The important parts being daterange and at=seq(..., by="day").
I hope this can help. I made this function that allows adding a fixed number of equidistant time ticks.
By setting first="month", the function puts the tick to the 1st of each month. If first="day" the function puts the tick to the 00:00 hour of each day.
Of course, the plot must be created with xaxt="n" argument.
By default, it adds 10 ticks (ticks.n=10) with a dd/mm format (format.x="%d/%m"), no first day of the month or day, and horizontal orientation of the labels (las=1).
axis.time=function(time.x=Sys.time(),ticks.n=10,format.x="%d/%m",first="none",las=1){
tz=attr(time.x,"tzone")
if (first == "day"){
time.x=seq(time.x[1],time.x[length(time.x)],60*30)
time.x=time.x[which(diff(as.numeric(format(time.x,"%H")))<0)+1]
time.x=strptime(as.character(as.Date(time.x)),"%Y-%m-%d",tz)
} else if (first == "month") {
time.x=seq(time.x[1],time.x[length(time.x)], 60*60*24/2)
time.x=time.x[which(diff(as.numeric(format(time.x,"%d")))<0)+1]
time.x=strptime(as.character(as.Date(time.x)),"%Y-%m-%d",tz)
} else {
time.x = seq(time.x[1],time.x[length(time.x)], length.out=ticks.n)
}
axis.POSIXct(side = 1,x = time.x,at = time.x,format = format.x,las=las)
Suppose you have a data frame:
df1=data.frame(time=seq(Sys.time()-1e8,Sys.time(),length.out = 100),Y=runif(100))
a plot with plot(df1) will put the X-axis ticks only at the beginning of each year. If you plot as plot(df1,xaxt="n") you can use the axis.time function:
axis.time(time.x = df1$time,first = "month",las=2,format.x = "%m-%y")
to get a tick on the first day of each month and with a different format and alignment.

Resources