Getting warning message while using plot function - r

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

Related

R - matplot x-axis not showing date inputs

I am just a beginner in R and I have a set of historical monthly yield data that looks like the sample data below:
dates rt
1 1990-01 0.0790
2 1990-02 0.0800
3 1990-03 0.0817
4 1990-04 0.0804
5 1990-05 0.0801
These data are stored in a data frame called 'data'.
I then attempt to plot the graph as follow.
#First attempt:
data <- data.frame(dates, rt)
matplot(data, type = 'l', xlab = 'month', ylab = 'US 3M rates',
main = 'US short rates 1990 - 2019')
The resulting graph showed the x-axis labels as 0, 50, 100 etc. (I have a total 350 rows of data)
#Second attempt:
data <- data.frame(dates, rt)
matplot(data, type = 'l', xlab = 'month', ylab = 'US 3M rates',
main = 'US short rates 1990 - 2019', xaxt = 'n')
axis(1, at = data$dates)
The resulting graph left the x-axis blank with the following warning/error messages:
Warning in xy.coords(x, y, xlabel, ylabel, log = log, recycle = TRUE) :NAs introduced by coercion
Warning in xy.coords(x, y, xlabel, ylabel, log) :NAs introduced by coercion
Warning in axis(1, at = data$dates) : NAs introduced by coercion
Error in axis(1, at = data$dates) : no locations are finite
#Third attempt:
data <- data.frame(dates, rt)
matplot(data, type = 'l', xlab = 'month', ylab = 'US 3M rates', main = 'US short rates 1990 - 2019', xaxt = 'n')
axis(1, at = format(data$dates, "%y-%m"))
Similarly, this also left the x-axis blank and an error message as follows:
Error in format.default(data$dates, "%y-%m") : invalid 'trim' argument
May I know how should the code be tweaked such that the x-axis can show the dates as per the sample data, i.e. showing 1990-01 and so on so forth?
The problem is that the datesĀ“ column is just a character vector and you have to tell R what 1900-01` means.
The easiest way would be to convert the dates column to Date format and then to use plot instead of matplot:
data$dates <- as.Date(paste0(data$dates, "-01"))
plot(data, type = 'l', xlab = 'month', ylab = 'US 3M rates',
main = 'US short rates 1990 - 2019')

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)

Is there a way to plot points and a line on the same plot?

I have a vector called GoldbachCounts which I want to graph against a predictor function. I want to graph the predictor function as a line, overtop of the points of the vector. The code I used threw a few different errors depending on what I was trying, but the latest was Error in plot.window(...) : invalid 'ylim' value, even though I specified a ylim which I knew would work for both. This code also displays the legend I wanted, even though it threw an error, but it did not display the plot itself. The code that threw this error follows
x <- seq.int(1,300000,1)
y <- x/((log(x))^2)
plot(x, GoldbachCounts, main = "Goldbach Counts", xlab = "x", ylab = "y", ylim = 13000,
type = "b", pch = 20, cex = .25,
lines(x, y, col = "red", cex = 1.5),
legend("topleft", c("Counts", "Predictor"), fill = c("black", "red")))
By changing ylim to c(0,13000), it now throws the error Error in strsplit(log, NULL) : non-character argument
ylim takes both a minimum and a maximum for the axis. So try changing to ylim = c(1, 13000), or whatever you wish the minimum to be

plotting multiple xts objects in one window

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..

Resources