Adding Regression Lines to Multiple Scatter Plots - r

Had a look around and couldn't find an answer to my question, so finally stopped lurking. I've been creating multiple scatter plots comparing each column to the others as shown here
I used the script
attach(`File`)
plot(`Files`[,c(2,3,4,5,6,7,8)])
However I can't seem to correctly input the command to annotate the regression line and r2 value onto the graphs.

here is the solution. Assume Z is your design matrix.
z=matrix(rnorm(500),ncol=5)
pairs( z, panel=function(x,y){
points(x,y)
abline(lm(y~x), col='red')
text(0,1.5,labels = paste('R2=',round((cor(x,y))^2,2)) ,col='red' )
})
and result should be like this

Related

Line not appearing on plot when using lines() in R

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.

Julia - Displaying several plots in the same plot (not subplot)

Plotting several series in a same plot display is possible and also several subplots in a display. But I want several plots which can be completely different things (not necessarily a series or graph of a map) to be displayed exactly in one frame. How can I do that? In Maple you assign names for each plot like
P1:=...:, P2:= ...: and then using plots:-display(P1,P2,...); and it works. But I want to do this in Julia. Let's say I have the following plots as an example;
using Plots
pyplot()
x=[1,2,2,1,1]
y=[1,1,2,2,1]
plot(x,y)
p1=plot(x,y,fill=(0, :orange))
x2=[2,3,3,2,2]
y2=[2,2,3,3,2]
p2=plot(x2,y2,fill=(0, :yellow))
Now how to have both P1 and P2 in one plot? I don't one a shortcut or trick to write the output of this specific example with one plot line, note that my question is general, for example p2 can be a curve or something else, or I may have a forflow which generates a plot in each step and then I want to put all those shapes in one plot display at the end of the for loop.
Code for a simple example of trying to use plot!() for adding to a plot with arbitrary order.
using Plots
pyplot()
x=[1,2,2,1,1]
y=[1,1,2,2,1]
p1=plot(x,y,fill=(0, :orange))
x2=[2,3,3,2,2]
y2=[2,2,3,3,2]
p2=plot!(x2,y2,fill=(0, :orange))
p3=plot(x,y)
display(p2)
p5=plot!([1,2,2,1,1],[2,2,3,3,2],fill=(0, :green))
By running the above code I see the following plots respectively.
But what I expected to see is a plot with the green rectangle added inside the plot with the two orange rectangles.
The way to plot several series within the same set of axes is with the plot! function. Note the exclamation mark! It's part of the function name. While plot creates a new plot each time it is invoked, plot! will add the series to the current plot. Example:
plot(x, y)
plot!(x, z)
And if you are creating several plots at once, you can name them and refer to them in plot!:
p1 = plot(x, y)
plot!(p1, x, z)
Well, if you do that, what you will have is subplots, technically. That's what it means.
The syntax is
plot(p1, p2)
Sorry, I don't know how to plot a whole plot (conversely to a series) over an other plot.. For what it concerns the order of the plots, you can create as many plots as you want without display them and then display them wherever you want, e.g.:
using Plots
pyplot()
# Here we create independent plots, without displaying them:
x=[1,2,2,1,1]
y=[1,1,2,2,1]
p1=plot(x,y,fill=(0, :orange));
x2=[2,3,3,2,2]
y2=[2,2,3,3,2]
p2=plot(x2,y2,fill=(0, :orange));
p3=plot(x,y);
p5=plot([1,2,2,1,1],[2,2,3,3,2],fill=(0, :green));
# Here we display the plots (in the order we want):
println("P2:")
display(p2)
println("P3:")
display(p3)
println("P5:")
display(p5)
println("P1:")
display(p1)

R, suppress plot from curve function

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.

Multiple Regression lines in R

I am trying to have output 2 different graphs with a regression line. I am using the mtcars data set which I believe you can load into R. So, I am comparing 2 different pairs of information to create a regression line. And the problem seems to be that the 2nd regression line from the 2nd graph is for some reason in the first graph as well.
I just want it to show 1 regression line in each graph the way it should be.
mtcars
names(mtcars)
attach(mtcars)
par(mfrow=c(1,2), bg="white")
with(mtcars,
{
regrline=(lm(gear~mpg))
abline(regrline)
plot(mpg,gear,abline(regrline, col="red"),main="MPG vs Gear")
# The black line in the first graph is the regression line(blue) from the second graph
regrline=(lm(cyl~disp))
abline(regrline)
plot(disp,cyl,abline(regrline, col="blue"),main="Displacement vs Number of Cylinder")
})
Also when I run the code separately for plotting, I don't see the black line. Its only when I run it with the: with() it causes a problem.
First of all, you really should avoid using attach. And for functions that have data= parameters (like plot and lm), its usually wiser to use that parameter rather than with().
Also, abline() is a function that should be called after plot(). Putting it is a parameter to plot() doesn't really make any sense.
Here's a better arrangement of your code
par(mfrow=c(1,2), bg="white")
regrline=lm(gear~mpg, mtcars)
plot(gear~mpg,mtcars,main="MPG vs Gear")
abline(regrline, col="red")
regrline=lm(cyl~disp, mtcars)
plot(cyl~disp,mtcars,main="Displacement vs Number of Cylinder")
abline(regrline, col="blue")
You got that second regression line because you were calling abline() before plot() for the second regression, do the line drew on the first plot.
Here is your code cleaned up a little. You were making redundant calls to abline that was drawing the extra lines.
By the way, you don't need to use attach when you use with. with is basically a temporary attach.
par(mfrow=c(1,2), bg="white")
with(mtcars,
{
regrline=(lm(gear~mpg))
plot(mpg,gear,main="MPG vs Gear")
abline(regrline, col="red")
regrline=(lm(cyl~disp))
plot(disp,cyl,main="Displacement vs Number of Cylinder")
abline(regrline, col="blue")
}
)

for loop generates multiple plots

I have a question about the for loop in combination with the plot function.
I want to use a for loop function (see below) to plot multiple points in one plot. But my loop generates for each point his one plot. So with an i of 35 I generate 35 plot. My question is, is there a way to plot all the points in the same plot?
pdf("test plot.pdf")
for (i in 1:nrow(MYC)){
plot(MYC[i,1], MYC[i,2]
}
dev.off()
Thank you all!
As mentioned in the comments, you are in essence trying to do multiple plots with a loop. R doesn't understand that actually want to plot only points. There's a cure for that, and it comes in vials of points(). Before calling a loop, construct your plot using the type argument. This will make an empty plot, something along the lines of:
plot(your.data, type = "n")
You can then use your loop (with points) to add points to this existing plot.

Resources