I would like to draw a straight line on plot using the following linear equation.
y = 2.522x-1.331
I used the following code to get a scatterplot.
data=read.csv("C://book.csv")
plot(data$x,data$y)
You need to use function abline:
abline(a=-1.331, b=2.522)
Argument a is the intercept and argument b the slope.
See ?abline for more details.
Use abline, e.g.
abline(-1.331, 2.522)
Related
I'm doing a polynomial regression and I want to plot it.
My code is the following:
to create polynomial regression to degree 1
mreg6=lm(user_Score~poly(year_of_Release,1))
to create plot
plot(year_of_Release~user_Score, col='gray')
to create line
lines(sort(year_of_Release),predict(mreg6)[order(year_of_Release)],col='red')
to create legend
legend('topright',lty=1:2,col=c('red'),c('degree 1'))
When I run the code, there is no error but the line does not appear on graph.
Do any of you know what I might be doing wrong?
I found my answer: I was putting ~ instead of , in plot() so it was giving me another regression.
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.
I'd like to draw a graph which has a sawtooth shape, connecting some points.The problem is that when I use lines() I can't get the correct pattern
These are the points:
I'd like to get the following lines (red line):
and
With lines() I don't get the correct plot. I tried these codes:
A)
lines(opsOK$X, opsOK$VariableCost, lty=2, col=cols[2])
points(opsOK$X, opsOK$VariableCost)
B)
lines(opsOK$X, opsOK$VariableCost, type="s", lty=2, col=cols[2])
points(opsOK$X, opsOK$VariableCost)
Is there a way I can draw these graphs?
Thanks a lot!
As R. Schifini pointed out, it's necessary to add the missing points to the graph.
I made a plot of an empirical distribution function (EDF) using plot.ecdf(x, ...).
In order to visualize normality, I'm looking in r for a qqline equivalent to draw a simple diagonal line in my plot.
The normplot() function in MATLAB is doing the same thing (See the red line in the plot on this link: http://www.mathworks.de/de/help/stats/normplot.html). Thanks.
As mentioned in the comments, just call qqline():
x <- ecdf(rnorm(10))
plot.ecdf(x)
qqline(x)
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)