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)
Related
This sounds like a really basic question and it probably is but I can't figure out how to change the line width when plotting a locfit object. If you do a simple test, such as:
plot(locfit::locfit(~rnorm(n = 1000)))
and compare it with
plot(locfit::locfit(~rnorm(n = 1000)), lwd = 2.0)
You will see that the plotted line has the same thickness in both plots. So using lwd does not work when plotting a locfit object? Is there any workaround?
Thanks!
You could use your model to predict the output to use lines on an empty plot which makes it possible to change the linewidth with lwd like this:
library(locfit)
#> locfit 1.5-9.7 2023-01-02
set.seed(7)
fit <- locfit(~rnorm(n = 1000))
plot(fit)
set.seed(7)
xvalues <- seq(min(rnorm(n = 1000)), max(rnorm(n = 1000)), length.out = 100)
pred <- predict(fit, xvalues)
plot(1, type="n", xlab="", ylab="", xlim=c(-3, 3), ylim=c(0, 0.4))
lines(xvalues, pred, lwd = 10)
Created on 2023-02-09 with reprex v2.0.2
There is not currently a way to do that in the existing function. When you call plot() on the locfit object, it calls preplot.locift() on your object, and then plot.preplot.locfit() which calls plot.locfit.1d(). The relevant lines from the code are:
plot(xev[ord], yy[ord], type = "n", xlab = xlab, ylab = ylab,
main = main, xlim = range(x$xev[[1]]), ylim = ylim,
...)
}
lines(xev[ord], yy[ord], type = type, lty = lty, col = col)
As you can see, the ... goes through to the plot function, but the line actually gets added with lines() which does not have access to other arguments specified in ...
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
I have a big database and I run a ROC curve with nsROC package. I need to have log scale on x-axis. How can I do it with plot function?
ns_roc <- gROC(sei$score, sei$label)
par(xlog = TRUE)
plot(ns_roc, xlim = range(1e-6:1), ylim = range(0:1), xaxs = "r", yaxs = "r")
This is my error message:
Error in plot.default(xx, c(0, obj$roc, 1), type = type, xlim = c(0, 1), :
formal argument "xlim" matched by multiple actual arguments
The issue is that plot here is not the generic R plot function. It is invoking plot.groc which is a separate function that defines xlim explicitly.
Type plot.groc after you install your package to see the whole function. You will see this inside the function:
[removed]
plot(xx, c(0, obj$roc, 1), type = type, xlim = c(0, 1), ylim = c(0,
1), lwd = lwd, xlab = xlab, ylab = ylab, main = main,
...)
axis(1, at = seq(0, 1, 0.01), labels = F, tck = -0.01)
axis(1, at = seq(0.1, 0.9, 0.1), labels = F, tck = -0.02)
[removed]
This is your problem.
You may have to make your own version of this function.
It is not clear to me whether you want logarithmic ticks on your x-axis (e.g., axis(1, c(0,0.001,0.01,0.1,1)) instead of equally-spaced tickmarks) or if you want the whole x-axis on the log scale (e.g., log(False-Positive Rate) between -Inf and 0 at different powers of 10).
I'm not sure why using a logarithmic axis (-Inf to 0) is a good idea here, which is what you seem to want to graph. You may not be able to use the nsROC package without 1) modifying your ROC curve data at x=0, and 2) writing your own functions for a log-axis in an otherwise perfectly interpretable set of ROC curves. It makes interpreting the AUC more difficult as well.
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
I have the following script:
FGM = function (n,r,z){
x = r*sqrt(n)/(2*z)
Px = 1-pnorm(x)
}
re = 10000
data = data.frame(abs(rnorm(re,0,1)), abs(rnorm(re,0,1)), abs(rnorm(re,0,1)))
colnames(data) = c("n","r","z")
data$Px = FGM(data$n,data$r,data$z)
data$x = data$r*sqrt(data$n)/(2*data$z)
par(mar=c(4.5,4.5,1,1))
plot(data$x,data$Px, xlim = c(0,3), pch = 19, cex = 0.1, xaxs="i", yaxs="i",
xlab = expression(paste("Standardized mutational size (",italic(x), ")")),
ylab = expression(paste("P"[a],"(",italic(x),")")))
Which is a recreation of the graph found here (box 2). You can see in this script that I do this by just plotting 10000 small black points with various values of n,z, and r. This seems like an ugly work around, I think I should just be able to give R my function
FGM = function (n,r,z){
x = r*sqrt(n)/(2*z)
Px = 1-pnorm(x)
}
and have it plot a line on a graph. However, a few hours of scouring the web has been unproductive, and I tried a few ways with abline and lines but nothing worked, is there a way of doing it with these functions or another function?
Tried this...
plot(data$x,data$Px, xlim = c(0,3), ylim = c(0,0.5), xaxs="i", yaxs="i",
xlab = expression(paste("Standardized mutational size (",italic(x), ")")),
ylab = expression(paste("P"[a],"(",italic(x),")")), type = "n")
curve(1-pnorm(r*sqrt(n)/(2*z)), add=T)
>Error in curve(1 - pnorm(r * sqrt(n)/(2 * z)), add = T) :
'expr' must be a function, or a call or an expression containing 'x'
>
#PaulRegular offered this solution but it still plots based on data, not the formula itself. I'm looking for a solution which can produce the curve properly without large values of "re" - using the following but with "re" set to 10 you can see what I mean...
data <- data[order(data$x),]
lines(data$x, data$Px, lwd=1)
You can pass a function of just one variable to plot. I guess that you are looking for:
plot(function(x) 1-pnorm(x),0,3)
Try sorting your data by x, then add the line:
data <- data[order(data$x),]
lines(data$x, data$Px, lwd=2)