R, suppress plot from curve function - r

when using the "curve" function in R, how do you suppress/stop the plot from showing up? For example, this code always plots the curve
my_curve = curve(x)
Is there a parameter to do this or should I being using a different function? I just want the x y points as a dataframe from the curve.

curve() is from the graphics library and is unhandy for generating lists.
Just try using:
x = seq(from, to, length.out = n)
y = function(x)
If you stick to the curve function, the closest to a solution I know is adding dev.off() after the curve() statement!

Here's a way to take advantage of the part of curve that you want without generating a plot.
I made a copy of the curve function (just type curve in the console); called it by a new name (curve2); and commented out the four lines at the end starting with if (isTRUE(add)). When it's called and assigned, I had a list with two vectors—x and y. No plot.

Related

Plot multiple similar equations in r

there
I am new on R. I want to plot a graph like this.
The curves are created by these equations :
(log(0.4)-(0.37273*log(x)-1.79389))/0.17941
(log(0.5)-(0.37273*log(x)-1.79389))/0.17941
(log(0.6)-(0.37273*log(x)-1.79389))/0.17941
etc. The equations are similar, the only difference is the first log(XXX). I already manually draw the graph by repeating plot() for each equation.
But I think there must be a way to just assign a simple variable like
x<-c(0.4,0.5,0.6,0.7)
and then plot all the curves automatically. I tried to use data frame to make a set of equations, but failed.
You can create a function-generating function and then loop over values of interest. For example
# takes a value, returns a function
logfn <- function(b) {
function(x) (log(b)-(0.37273*log(x)-1.79389))/0.17941
}
x <- c(0.4,0.5,0.6,0.7)
# empty plot
plot(0,0,type="n", ylim=c(-5,5), xlim=c(1,8), xlab="Lenght", ylab="Z-score")
# add plots for questions with `curve()`
for(v in x) {
curve(logfn(v)(x),add=T)
}

Graphing functions in R?

So, I am able to use the plot() function in R to graph different functions. However, I am finding that the graphs in R do not typically show the entire curve of the function. Let me use an example:
fun <- function(x){
x^3 + 2*x^2 + 3*x + 4
}
plot(fun)
However, when I plot the same function using the Desmos Graphing Calculator it shows all four quadrants of the Cartesian graph whereas R is only showing one:
My question: How can I modify RPlot to show all four quadrants as opposed to just one as in the above case?
I think you can do this just by extending the default range (which is [0,1]):
plot(fun,from=-5,to=5,ylim=c(-8,8),col="red")
grid()
abline(v=0,h=0,lty=2)
I've added a few frills to make it look a little more like the desired plot. Adding a point on the y axis is easy; adding the x-intercept is not quite so easy.
points(0,fun(0),pch=16)
points(Re(polyroot(c(4,3,2,1))[2]),0,pch=16)

Break x-axis in plot

I want to break the x-axis of a plot of a cumulative distribution function for which I use the function plot.stepfun, but don't seem to be able to figure out how.
Here's some example data:
set.seed(1)
x <- sample(seq(1,20,0.01),300,replace=TRUE)
Then I use the function ecdf to get the empirical cumulative distribution function of x:
x.cdf <- ecdf(x)
And I change the class of x.cdf to stepfun, because I prefer to call plot.stepfun directly over using plot.ecdf (which also uses plot.stepfun, but has fewer possibilities to customize the plot).
class(x.cdf) <- "stepfun"
Then I am able to create a plot as follows:
plot(x.cdf, do.point=FALSE)
But now I want to break up the x-axis between 12 and 20, e.g. using axis.break [plotrix-library] such as here, but since I have no ordinary x and y-argument for plotting, I don't know how to do this.
Any help would be very much appreciated!
"Breaking the axis between 12 and 20" doesn't make a lot of sense to me since 20 is the end of the x range, so I will exemplify breaking it between 12 and 15. The plotrix.axis.break function doesn't actually do very much (as can be seen if you step through that example.) All it does is put a couple of slashes at a particular location, the "breakpos". All the rest of the work needs to be done with regular plotting functions and plot.stepfun isn't really set up to do it, so I'm using regular plot.default with the type="s" argument. You need to do the offsetting of the x values, the arguments to the ecdf function and the labels in the axis arguments.
png()
plot( c(seq(1,12,0.1), seq(15,20,0.1)-3), # Supply the range, shifted
x.cdf(c(seq(1,12,0.1), seq(15,20,0.1))), # calc domain values, not shifted
type="s", xaxt="n", xlab="X", ylab="Quantile")
axis(1, at=c( 1:12, (16:20)-3), labels=c(1:12, (16:20)) ) #shift x's, labels unshifted
axis.break(breakpos=12)
dev.off()

Plot a change point curve in one command

I want to plot a curve that has a change point at x=5
Until now I am using the code
curve(exp(0.68+0.92*x), from=0,to=5, xlim=c(0,12), ylim=c(0,500))
curve(exp(0.68+0.92*x-0.7*(x-5)), from=5,to=12, add=T)
Is it possible to write it in one line (one curve command)? I was thinking
something like this
curve(exp(0.47+0.8*x-0.7*(x-5)*if(x<5,0,1)), from=0,to=12, xlim=c(0,12), ylim=c(0,500))
but it doesn't work for R
Using ifelse you can create one data series:
values = ifelse(x <= 5, exp(0.68+0.92*x), exp(0.68+0.92*x-0.7*(x-5))
and plot them:
curve(values)
and if you insist on a one-liner you can combine the ifelse and the call to curve:
curve(ifelse(x <= 5, exp(0.68+0.92*x), exp(0.68+0.92*x-0.7*(x-5)))
although separating the code into two lines makes it easier to read imo.
You could just write a function that plots both curves:
myfun <- function(...) {
plot(...)
lines(...)
}
You have to give the right arguments of course. The result is two curves in one plot

Write using mouse on R plot?

I created scattergram using the plot() function in R.
Is there any possibility to draw on this graph?
I would like to add a straight line and get parameters of it, but in my opinion abline() can be inconvenient (I would like to draw many lines and choose one which will be most proper).
How can I accomplish this task?
Take a look at RStudio and this example:
library(manipulate)
data = matrix(rnorm(20), ncol = 2)
example <- function(data, a, b){
plot(data[,1],data[,2])
abline(a = a, b = b)
}
manipulate(
example(data, a, b),
a = slider(-5,5),
b = slider(-5,5)
)
This will put a new line on the plot, and allow you to tweak its slope and intercept.
This was inspired by the example on this page: http://support.rstudio.org/help/discussions/questions/106-rstudio-manipulate-command
Note that this requires installing RStudio (it ships with the manipulate package, I believe). For more info, see the site.
Others' solutions with locator can be done in base R.
Use locator(), a function that allows you to get the coordinates of the mouse pointer when clicking on a plot. Then use
plot(cars)
xy <- locator(n=2)
lines(xy, col="red", lwd=5)
lm(y~x, xy)
abline(coef(lm(y~x, xy)))
coef(lm(y~x, xy))
(Intercept) x
33.142094 1.529687
Of course the correct way of fitting lines through data is to use a proper model. Here is how you can do it with lm:
abline(coef(lm(dist~speed, cars)), col="blue")
I made the following graph with this code:
The thick red line is the line connecting my two mouse clicks
The black line is the abline through these points
The blue line is the line of best fit produced by lm
Warning 1: locator only works on some graphics devices. See ?locator for more details.
Warning 2: Drawing lines of fit by hand could well be a really stupid idea. Use a regression function like lm or a smoothing function like loess instead.
If you were hoping to add horizontal or vertical lines to your plot interactively, you may want to use the locator() function to capture the position of a mouse click on the plot.
For example, the following code would allow the repeated addition of vertical lines to an existing plot:
repeat {
click.loc <- locator(1)
if(!is.null(click.loc)) abline(v=click.loc$x)
else break
}
You could adapt this for horizontal lines with abline(h=click.loc$y)

Resources