I'm trying to make a persp3D plot but I can't figure out how to add dates on the x-axis. It only lets me change the lable of the axis, but I haven't found a way to get certain values on the axis.
persp3D(x= (1:iNumberOfDays),y= (1:iNumberOfVariables) ,z= zValue, xlab = "Time")
I also have a date vector vDates with iNumberOfDays elements which I would like to use on the x-axis as on any regular 2d lineplot or something.
You can try to pass vDates to the x argument and set ticktype = "detailed" as follows:
persp3D(x=vDates, y=1:iNumberOfVariables, z=zValue, xlab="Time", ticktype="detailed")
Related
I have a dataset like revenue and date.
I used arima to plot the data.
ts_data = ts(dataset$Revenue,frequency = 7)
arima.ts = auto.arima(ts_data)
pred = forecast(arima.ts,h=30)
plot(pred,xaxt="n")
When I plot the data, it produces plot like below.
My expectations are below,
I need to display values in Million for predicted values like 13.1M.
I need to show x-axis as date instead of data points numbers.
I tried several links but couldn't crack it. Below are the experiments I made,
Tried with start date and end date in ts_data that also doesnt work.My start date is "2019-09-27" and end date is "2020-07-02"
tried wit axis_date in plot function that also doesnt work.
Please help me to crack the same.
Thanks a lot.
You can specify axis tick values and labels with axis()
plot(pred,xaxt="n", yaxt="n") # deactivate x and y axis
yl<-seq(-5000000,15000000,by=5000000) # position of y-axis ticks
axis(2, at=yl, label=paste(yl/1000000, "M")) # 2 = left axis
You can specify the desired position of y axis ticks with at and the labels to be associated with label. In order to obtain the values like 10 M I have used the function paste to join the numbers with the letter M.
You could use the same method also for x-axis, even tough more efficient methods probably exist. Not having the specific data available I have used a generic spacing and labels only to give you the idea. Once you have set the desired position you can generate the sequence of dates associated with it (to generate a sequence of dates see https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/seq.Date)
axis(1, at=seq(1,40,by=1), label=seq(as.Date("2019-09-27"),as.Date("2020-07-02"),by="week")) # 1 = below axis
You can also change the format of the dates displayed with format() for example label=format(vector_of_date, "%Y-%b-%d") to have year-month(in letter)-day.
pairs(iris[,1:4])
will produce a set of scatter plots relating the 4 variables to each other.
It will scale each axis differently depending on the range of each variable.
Is there a way that I can instead set the axes to be the same for every plot?
You can pass xlim and ylim arguments into pairs():
pairs(iris[,1:4], xlim=c(0,8), ylim=c(0,8))
When I use plot() to plot a time serious variable, it only shows dots. I use second code, lines(), to link all the dots. Is this really necessary? Or I did something wrong...
The data is as following. I use the plot() and lines() to draw the graph to see the trend.
YYYYMM<-c("200907","200908","200909","200910","200911","200912","201001","201002","201003","201004","201005","201006","201007","201008","201009","201010","201011","201012","201101","201102","201103","201104","201105","201106")
a<-c(1158,1455,1134,1371,1352,1277,1408,1270,1000,1462,1419,0,0,0,0,0,0,0,0,0,0,0,0,0)
a_number_trend<-data.frame(YYYYMM,a)
a_number_trend
plot(a_number_trend$YYYYMM,a_number_trend$a,las=2,type="l",col="blue")
lines(a_number_trend$YYYYMM,a_number_trend$a,las=2,type="l",col="blue")
The plot is like this at beginning.
Then become this.
But I want the line only without the short bar. Or to change the short bars into points.
Convert your YYYYMM column to an actual R ?Date object. Then you can get everything lining up properly:
a_number_trend$date <- as.Date(
paste0(a_number_trend$YYYYMM,"01"),
format="%Y%m%d"
)
plot(a ~ date, data=a_number_trend, type="l", xaxt="n", ann=FALSE)
The below axis is not stricly necessary (remove xaxt="n" above if you want the default Date axis calculations instead).
axis.Date(
1,
at=seq(min(a_number_trend$date), max(a_number_trend$date), by="1 month"),
format="%Y%m",
las=2
)
Using R and polygon I'm trying to shade the area under the line of a plot from the line to the x-axis and I'm not sure what I am doing wrong here.
The shading is using some point in the middle of the y range to shade from, not 0, the x-axis.
The data set ratioresults is a zoo object but I don't think that's the issue since I tried coercing the y values to as.numeric and as.vector and got the same results.
Code:
plot(index(ratioresults),ratioresults$ratio, type="o", col="red")
polygon(c(1,index(ratioresults),11),c(0, ratioresults$ratio, 0) , col='red')
What's index(ratioresults)? For a simple zoo object I see:
> index(x)
[1] "2003-02-01" "2003-02-03" "2003-02-07" "2003-02-09" "2003-02-14"
which is a vector of Date objects. You are trying to prepend/append values of 1 and 11 to this vector. Its not going to work.
Here's a reproducible example:
x=zoo(matrix(runif(11),ncol=1),as.Date("2012-08-01") + 0:10)
colnames(x)="ratio"
plot(index(x),x$ratio,type="o",col="red",ylim=c(0,1))
polygon(index(x)[c(1,1:11,11)],c(0,x$ratio,0),col="red")
Differences from yours:
I call my thing x.
I set ylim on the plot - I don't know how your plot managed to start at 0 on the Y axis.
I complete the polygon using the x-values of the first and 11th (last) point, rather than 1 and 11 themselves.
#With an example dataset: please provide one when you need help!
ratioresults<-as.zoo(runif(10,0,1))
plot(index(ratioresults),ratioresults, type="o", col="red",
xaxs="i",yaxs="i", ylim=c(0,2))
polygon(c(index(ratioresults),rev(index(ratioresults))),
c(as.vector(ratioresults),rep(0,length(ratioresults))),col="red")
The issue with your question is that the x-axis is not a line defined by a given y value by default, so one way to fill under a curve to the x-axis using polygon would be to define a y values for the x-axis using ylim (here I chose 0). Whatever value you choose you will want to specify that the plot stop exactly at the value using yaxs="i".
You also have to construct your polygon with the value you chose for you x-axis.
Is it possible to add more than one x-axis to a plot in R? And to put an annotation next to each scale?
Edit > here's the result of Nick Sabbe idea. For the annotation (a little text at the left of each axis), is it possible ?
You can use the line argument of axis() to place an axis higher or lower, this way you can make multiple axes. With mtext() you can then add a label to the side. Do note that the plot itself is only on one scale so you need to rescale the points and labels of the other scale accordingly:
# Plot and first axis:
plot(1:10,1:10,bty="n",col="red",pch=16,axes=FALSE,xlab="",ylab="")
axis(2,0:11,las=1)
axis(1,0:11,line=1,col="red",col.ticks="red",col.axis="red")
mtext("Label 1",1,line=1,at=0.2,col="red")
# Secondary points and axis:
points(rnorm(10,50,20)/10, rnorm(10,5,2),pch=16, col="blue" )
axis(1,0:11,labels=0:11*10,line=3,col="blue",col.ticks="blue",col.axis="blue")
mtext("Label 2",1,line=3,at=0.2,col="blue")
You can use ?axis for that. Parameter at is in the scale of the original axis of the plot, and you can pass labels to show other values.
You have to scale the axess labels yourself, though.
A very simple/silly example:
plot(1:10,1:10)
axis(side=4, at=c(3,7), labels=c(30,70))
Finally, note that most people consider adding multiple axes to a plot bad form...