Parameters in R lines Function - r

I want to plot the 97% confidence interval and prediction interval into my other plot using different colours and legends. I want them to be represented by just straight lines across the scatter plot graph.
Please forgive me as I'm new to R so any tips also on how to phrase this question would be helpful!
I'm confused what to put in as the x and y coordinates in the lines function
I tried to put in a and m to match but it creates a very weird complicated graph that I'm sure is not correct, would you mind explaining what I should be putting inside the place where I put HELP in the code below?
attach(my_data)
plot(a, m,
xlab="a", ylab = "m",
main = "Confidence intervals and prediction intervals",
ylim = c(10,50))
abline(lm.fit,lwd=5,col='pink')
p_pred <- predict(lm.fit,data.frame(a=c(14.50)),interval="prediction",level=0.97)
p_conf <- predict(lm.fit,data.frame(a=c(14.50)),interval="confidence",level=0.97)
lines(HELP,p_conf[,"lwr"], col="red", type="b", pch="+")
lines(HELP,p_conf[,"upr"], col="red", type="b", pch="+")
lines(HELP,p_pred[,"upr"], col="blue", type="b", pch="*")
lines(HELP,p_pred[,"lwr"], col="blue", type="b", pch="*")
legend("bottomright",
pch=c("+","*"),
col=c("red","blue"),
legend = c("confidence","prediction"))
I'm sure for this problem the solution is very simple, so I apologize as I am not that familiar with R if I am asking an easy question!

If you want just plot you don't need to calculate predicitons yourself but you can use geom_smooth from ggplot2 library. As in the example below:
library(ggplot2)
ggplot(mtcars, aes(mpg,cyl))+
geom_point()+
geom_smooth(method = "lm", level =0.97)

Thank you for the suggestions on the page! I actually ended up fixing the problem by creating a new variable that took the min and max value of the x variable and then the lines fitted on the graph.

Related

R: abline does not add line to my graph

I try to draw line graph using R. The lines have been plotted, but the abline line doesn't show up.
M <- c(1.0,1.5,2.0,2.5,3)
y <- c(0.0466,0.0522,0.0608,0.0629,0.0660)
plot(M, y, type="l", col="red", xlab="sdr",
ylab="simulated type I error rate")
abline(h=c(0.025,0.075),col=4,lty=2)
This is my simple coding for the graph. Any ways to make the line pop out?
Try this instead:
M <- c(1.0,1.5,2.0,2.5,3)
y <- c(0.0466,0.0522,0.0608,0.0629,0.0660)
plot(M, y, type="l",col="red",xlab="sdr", ylim = c(0.025, 0.075),
ylab="simulated type I error rate")
abline(h=c(0.025,0.075),col=4,lty=2)
by using ylim.
I would refer you to read my answer for another post: curve() does not add curve to my plot when “add = TRUE” for more about setting ylim when plotting several objects on a graph.

How do I plot an abline() when I don't have any data points (in R)

I have to plot a few different simple linear models on a chart, the main point being to comment on them. I have no data for the models. I can't get R to create a plot with appropriate axes, i.e. I can't get the range of the axes correct. I think I'd like my y-axis to 0-400 and x to be 0-50.
Models are:
$$
\widehat y=108+0.20x_1
$$$$
\widehat y=101+2.15x_1
$$$$
\widehat y=132+0.20x_1
$$$$
\widehat y=119+8.15x_1
$$
I know I could possibly do this much more easily in a different software or create a dataset from the model and estimate and plot the model from that but I'd love to know if there is a better way in R.
As #Glen_b noticed, type = "n" in plot produces a plot with nothing on it. As it demands data, you have to provide anything as x - it can be NA, or some data. If you provide actual data, the plot function will figure out the plot margins from the data, otherwise you have to choose the margins by hand using xlim and ylim arguments. Next, you use abline that has parameters a and b for intercept and slope (or h and v if you want just a horizontal or vertical line).
plot(x=NA, type="n", ylim=c(100, 250), xlim=c(0, 50),
xlab=expression(x[1]), ylab=expression(hat(y)))
abline(a=108, b=0.2, col="red")
abline(a=101, b=2.15, col="green")
abline(a=132, b=0.2, col="blue")
abline(a=119, b=8.15, col="orange")

Plot a log-curve to a scatter plot

I am facing a probably pretty easy-to-solve issue: adding a log- curve to a scatter plot.
I have already created the corresponding model and now only need to add the respective curve/line.
The current model is as follows:
### DATA
SpStats_urbanform <- c (0.3702534,0.457769,0.3069843,0.3468263,0.420108,0.2548158,0.347664,0.4318018,0.3745645,0.3724192,0.4685135,0.2505839,0.1830535,0.3409849,0.1883303,0.4789871,0.3979671)
co2 <- c (6.263937,7.729964,8.39634,8.12979,6.397212,64.755192,7.330138,7.729964,11.058834,7.463414,7.196863,93.377393,27.854284,9.081405,73.483949,12.850917,12.74407)
### Plot initial plot
plot (log10 (1) ~ log10 (1), col = "white", xlab = "PUSHc values",
ylab = "Corrected GHG emissions [t/cap]", xlim =c(0,xaxes),
ylim =c(0,yaxes), axes =F)
axis(1, at=seq(0.05, xaxes, by=0.05), cex.axis=1.1)
axis(2, at=seq(0, yaxes, by=1), cex.axis=1.1 )
### FIT
fit_co2_urbanform <- lm (log10(co2) ~ log10(SpStats_urbanform))
### Add data points (used points() instead of simple plot() bc. of other code parts)
points (co2_cap~SpStats_urbanform, axes = F, cex =1.3)
Now, I've already all the fit_parameters and are still not able to construct the respective fit-curve for co2_cap (y-axis)~ SpStats_urbanform (x-axis)
Can anyone help me finalizing this little piece of code ?
First, if you want to plot in a log-log space, you have to specify it with argument log="xy":
plot (co2~SpStats_urbanform, log="xy")
Then if you want to add your regression line, then use abline:
abline(fit_co2_urbanform)
Edit: If you don't want to plot in a log-log scale then you'll have to translate your equation log10(y)=a*log10(x)+b into y=10^(a*log10(x)+b) and plot it with curve:
f <- coefficients(fit_co2_urbanform)
curve(10^(f[1]+f[2]*log10(x)),ylim=c(0,100))
points(SpStats_urbanform,co2)

Error: "Hit <Return> to see next plot: " in r

I have the following code
frame()
Y = read.table("Yfile.txt",header=T,row.names=NULL,sep='')
X = read.table("Xfile.txt",header=F,sep='')
plot(Y$V1~X$V1,pch=20,xlim=c(0,27))
par(new=T)
plot(Y$V1~X$V2,pch=20,xlim=c(0,27),col='red')
par(new=T)
plot(Y$V1~Y$V3,pch=20,xlim=c(0,27),col='blue')
par(new=T)
All is well and I get the 3 graphs on the same plot. However, when I want to divide X$V1, X$V2 and X$V3 to normalise the data such that
plot(Y$V1~X$V1/Y$V2,pch=20,xlim=c(0,27))
par(new=T)
plot(Y$V1~X$V2/Y$V2,pch=20,xlim=c(0,27),col='red')
par(new=T)
plot(Y$V1~Y$V3/Y$V2,pch=20,xlim=c(0,27),col='blue')
par(new=T)
I get the message
Hit Return to see next plot:
and the graphs just won't show in the same plot. Could anybody tell me what is happening and how to solve it?
If you want to use arithmetic operations in formula you have to use I() functions. So
plot(Y$V1~I(X$V1/Y$V2),pch=20,xlim=c(0,27))
par(new=T)
plot(Y$V1~I(X$V2/Y$V2),pch=20,xlim=c(0,27),col='red')
par(new=T)
plot(Y$V1~I(Y$V3/Y$V2),pch=20,xlim=c(0,27),col='blue')
par(new=T)
works.
Following help page to formula:
To avoid this confusion, the function
I() can be used to bracket those
portions of a model formula where the
operators are used in their arithmetic
sense. For example, in the formula y
~ a + I(b+c), the term b+c is to be
interpreted as the sum of b and c.
Edit. You could do it without formula in one command:
plot(c(X$V1/Y$V2, X$V2/Y$V2, Y$V3/Y$V2), rep(Y$V1, 3),
pch=20, xlim=c(0,27),
col=rep(c("black", "red", "blue"), each=30)
)
I'm not sure why you get the error, but using points instead of plot for the second and third graph is a much more elegant solution (and gets rid of those par calls)

change look-and-feel of plot to resemble hist

I used the information from this post to create a histogram with logarithmic scale:
Histogram with Logarithmic Scale
However, the output from plot looks nothing like the output from hist. Does anyone know how to configure the output from plot to resemble the output from hist? Thanks for the help.
A simplified, reproducible version of the linked answer is
x <- rlnorm(1000)
hx <- hist(x, plot=FALSE)
plot(hx$counts, type="h", log="y", lwd=10, lend="square")
To get the axes looking more "hist-like", replace the last line with
plot(hx$counts, type="h", log="y", lwd=10, lend="square", axes = FALSE)
Axis(side=1)
Axis(side=2)
Getting the bars to join up is going to be a nightmare using this method. I suggest using trial and error with values of lwd (in this example, 34 is somewhere close to looking right), or learning to use lattice or ggplot.
EDIT:
You can't set a border colour, because the bars aren't really rectangles – they are just fat lines. We can fake the border effect by drawing slightly thinner lines over the top. The updated code is
par(lend="square")
bordercol <- "blue"
fillcol <- "pink"
linewidth <- 24
plot(hx$counts, type="h", log="y", lwd=linewidth, col=bordercol, axes = FALSE)
lines(hx$counts, type="h", lwd=linewidth-2, col=fillcol)
Axis(side=1)
Axis(side=2)
How about using ggplot2?
x <- rnorm(1000)
qplot(x) + scale_y_log10()
But I agree with Hadley's comment on the other post that having a histogram with a log scale seems weird to me =).

Resources