plotting multiple xts objects in one window - r

I have found some answers to this online but for some reason am interpreting incorrectly because I cannot get it to work. My goal is to simply use the xts plotting feature (with the the way it creates the axis, gridlines,etc.) to plot multiple plots:
x <- xts(data.frame(a=1:100, b=100:1),seq(from=as.Date("2010-01-01"), by="days", len=100))
> plot(x, screens=1)
Warning messages:
1: In plot.xts(x, screens = 1) :
only the univariate series will be plotted
2: In plot.window(...) : "screens" is not a graphical parameter
3: In plot.xy(xy, type, ...) : "screens" is not a graphical parameter
4: In axis(1, at = xycoords$x, labels = FALSE, col = "#BBBBBB", ...) :
"screens" is not a graphical parameter
5: In axis(1, at = xycoords$x[ep], labels = names(ep), las = 1, lwd = 1, :
"screens" is not a graphical parameter
6: In axis(2, ...) : "screens" is not a graphical parameter
7: In title(screens = 1) : "screens" is not a graphical parameter
Another try:
> plot(x, plot.type="single")
Warning messages:
1: In plot.xts(x, plot.type = "single") :
only the univariate series will be plotted
2: In plot.window(...) : "plot.type" is not a graphical parameter
3: In plot.xy(xy, type, ...) : "plot.type" is not a graphical parameter
4: In axis(1, at = xycoords$x, labels = FALSE, col = "#BBBBBB", ...) :
"plot.type" is not a graphical parameter
5: In axis(1, at = xycoords$x[ep], labels = names(ep), las = 1, lwd = 1, :
"plot.type" is not a graphical parameter
6: In axis(2, ...) : "plot.type" is not a graphical parameter
7: In title(plot.type = "single") :
"plot.type" is not a graphical parameter
To be clear: I can do this using lines but I wonder if there is a way to do this all at once.

You could coerce to zoo to use plot.zoo:
plot(as.zoo(x), screens=1)
plot(as.zoo(x), plot.type='single')
Or, you could install xtsExtra which has a newer plot.xts method
#install.packages("xtsExtra", repos='http://r-forge.r-project.org')
library(xtsExtra)
plot(x, screens=1)

I could be wrong but I think plot.xts is no longer part of xtsExtra and has moved to the main xts. Source. Maybe this note will help people in the future trying to figure out plotting in xts..

Related

R: Effects Plot, Ordinal Regression: In regularize.values(x, y, ties, missing(ties)) :

I am trying to visualize the probabilities of a simple ordinal regression:
model4<-polr(as.factor(Cycle)~VarS,data=data,Hess=TRUE), where Cycle is the frequency of cycling/week and VarS an independent variable ranging from 1-4. There are no NA values in either.
plot(Effect(focal.predictors = c("VarS"), model4)) throws
Warning messages:
1: In effect.llines(x[good], y[good], lwd = lwd, lty = lty, col = colors[1], :
spline interpolation may be unstable with only 1 points
2: In effect.llines(x[good], y[good], lwd = lwd, lty = lty, col = colors[1], :
spline interpolation may be unstable with only 3 points
3: In effect.llines(x[good], y[good], lwd = lwd, lty = lty, col = colors[1], :
spline interpolation may be unstable with only 3 points
4: In effect.llines(x[good], y[good], lwd = lwd, lty = lty, col = colors[1], :
spline interpolation may be unstable with only 1 points
5: In effect.llines(x[good], y[good], lwd = lwd, lty = lty, col = colors[1], :
spline interpolation may be unstable with only 4 points
6: In regularize.values(x, y, ties, missing(ties)) :
collapsing to unique 'x' values
The resulting plot looks strange:
Resulting graph
I tried plot(Effect(focal.predictors = c("VarS",model4), xlevels = list(VarS = 1:4))) but that throws Error in model.frame.default(as.factor(Cycle) ~ VarS + VarS, data = list( : variable lengths differ (found for 'VarS')
(note the "VarS + VarS")
A multiline plot with the same data looks just fine (also the stacked version looks OK):
Multiline
Any hints on what I am doing wrong?

How do I use the parameter 'bw=sj' in R

We have been told to make a histogram and line using our given data. I can make the histogram I think correctly. However we were told to use bw='sj' in our density function. I do not understand how I would put this to use.
i tried putting it in the hist() function as I thought it is a parameter however I get an error that says:
Warning messages:
1: In plot.window(xlim, ylim, "", ...) : "bw" is not a graphical parameter
2: In title(main = main, sub = sub, xlab = xlab, ylab = ylab, ...) :
"bw" is not a graphical parameter
3: In axis(1, ...) : "bw" is not a graphical parameter
4: In axis(2, ...) : "bw" is not a graphical parameter
This is part of my code that deals with the problem in R.
# histogram 1
rdi4p -> data_shhs[,'rdi4p']
hist(rdi4p ,probability=TRUE,col=rgb(0,0,1,1/4),breaks=30,
xlab="rdi4p",
main="Histogram 1",col.axis="blue")
lines(x=density(x= rdi4p),type="l",col="blue",lwd=3)
Of course, I don't have your data to work on (in particular we would need to know what rdi4p and sj were to make this fully reproducible), so I'll make up our own values for these variables:
set.seed(1) # Make example reproducible
rdi4p <- rnorm(1000) # Vector of 1000 samples from normal distribution
sj <- diff(range(rdi4p))/30 # 1/30 of the range of vector rdi4p
Now we draw the histogram using your code:
hist(rdi4p, probability = TRUE, col = rgb(0, 0, 1, 1/4), breaks = 30,
xlab = "rdi4p", main = "Histogram 1", col.axis = "blue")
and then we add the line. Note that we have to pass the parameter bw = sj to the density function, which is itself sitting inside the call to lines:
lines(x = density(x = rdi4p, bw = sj), type = "l", col = "blue", lwd = 3)

R 4.0.2 for windows: Trying to force the plot axis' to fit on x(-3 to 3) and y(1e-6 to 1)

I've been trying to force R to make the plot axis' longer on both the x and y to compare it to a similar dataset. I've used both the xlim and ylim commands within the plot() function and the axis() function after the plot function, but neither has worked. After talking with a co-worker we found that I'm getting the following warnings with my script:
Warning messages:
1: In doTryCatch(return(expr), name, parentenv, handler) :
display list redraw incomplete
2: In doTryCatch(return(expr), name, parentenv, handler) : invalid graphics state
3: In doTryCatch(return(expr), name, parentenv, handler) : invalid graphics state
4: In doTryCatch(return(expr), name, parentenv, handler) :
display list redraw incomplete
5: In doTryCatch(return(expr), name, parentenv, handler) : invalid graphics state
6: In doTryCatch(return(expr), name, parentenv, handler) : invalid graphics state
Attached below is a sample of the plot code and the subsequent result:
plot(x = q.ca[13:242], y = Censored.Alpha, log = "y",
xlab = "Z-Score",
main = "QQ Plot of DAC hours for Censored Alpha Radiation",
ylab = expression(paste("DAC*hr for Alpha (", mu, "Ci)", sep = " "),
xlim = c(-4, 4), ylim = c(1e-6, 1)))
lines(q.ca, za)
abline(h = 1e-04, col = "red", lty = "dashed")
text(grconvertX(0.05, "npc"), grconvertY(0.9, "npc"), GSDA.c, pos = 4)
text(grconvertX(0.05, "npc"), grconvertY(0.95, "npc"), GMA.c, pos = 4)
Plot for the listed code above:
Can anyone help me with the errors? I'm unsure of where they're forming. More importantly, though, is anyone able to help me with the dimensions of the plot?
EDIT: I isolated the functions that generated the plot I was trying to create and for some reason now it's working ¯_(ツ)_/¯, if anyone can help me decipher whats going on in the errors, that would be much appreciated, so I don't make the same mistake again.

Getting warning message while using plot function

I have my database table from which i retrieve a column and plot it using plot function.
Here is column of table
Profit
1 21200
2 28000
3 29600
4 30200
5 33000
6 26800
7 32600
8 30000
9 28000
10 34000
Here 60 rows are present but i am showing only 10 rows.
when i try to plot the graph i am getting a straight line parallel to x axis but here profit is changing, so i don't think that it should parallel to x-axis.Since table is present in database in aws i am retrieving the profit column from table first then plotting using plot function.Here is plot function
choices = dbGetQuery(pool,"select Profit from input11;")
plot(Choices, type = "l", lwd = 3, main = "Profit",col = "green", xlab =
"Number of Overbooking", ylab = "Profit")
i am also getting warning messages here:
Warning messages:
1: In plot.window(xlim, ylim, log, ...) :
graphical parameter "type" is obsolete
2: In axis(side = side, at = at, labels = labels, ...) :
graphical parameter "type" is obsolete
3: In title(xlab = xlab, ylab = ylab, ...) :
graphical parameter "type" is obsolete
But when i remove type = "l", warning message disappears. But I want the plot in straight line format only.
Based on this R Help thread, the Profit column is class of factor, let's test:
Below works fine, when numeric:
plot(1:10, type = "l")
When we have factors, plots but with warnings:
plot(factor(1:10), type = "l")
Warning messages:
1: In plot.window(xlim, ylim, log = log, ...) :
graphical parameter "type" is obsolete
2: In axis(if (horiz) 2 else 1, at = at.l, labels = names.arg, lty = axis.lty, :
graphical parameter "type" is obsolete
3: In title(main = main, sub = sub, xlab = xlab, ylab = ylab, ...) :
graphical parameter "type" is obsolete
4: In axis(if (horiz) 1 else 2, cex.axis = cex.axis, ...) :
graphical parameter "type" is obsolete

Error in xy.coords(x, y, xlabel, ylabel, log) : 'x' and 'y' lengths differ for Gamma distribution plot

I'm trying to plot the pdf distribution of Gamma(alpha=29, beta = 3) on a graph, but I get the error: "Error in xy.coords(x, y, xlabel, ylabel, log) : 'x' and 'y' lengths differ". Why?
x <- seq(0, 1000, by = 1)
y <- dgamma(x, shape = 3, rate = 1/29, scale = 1/rate, log = FALSE)
plot(x, y, xlabel = "x", ylabel = "Gamma(29,3)")
Seems like you are probably ignoring errors. For me, the line
y <- dgamma(x, shape = 3, rate = 1/29, scale = 1/rate, log = FALSE)
gives the error
Error in dgamma(length(x), shape = 3, rate = 1/29, scale = 1/rate, log = FALSE) :
object 'rate' not found
so your y variable is never set. You must have a left over one from some other code that is a different length than x. You should check before plotting by looking at length(x) and `length(y). Use
y <- dgamma(x, shape = 3, rate = 1/29, log = FALSE)
instead. This will use the default value for rate which is what you want. Maybe you were looking at the default parameter values for the function? You can't specify names of other parameters when passing values for a parameter when you call a function, you can only do this when you define a function.
Also your plot() command returns an error. The parameter names are xlab= and ylab= and not xlabel= and ylabel=

Resources