R: abline does not add line to my graph - r

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.

Related

Parameters in R lines Function

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.

Access lines plotted by R using basic plot()

I am trying to do the following:
plot a time series in R using a polygonal line
plot one or more horizontal lines superimposed
find the intersections of said line with the orizontal ones
I got this far:
set.seed(34398)
c1 <- as.ts(rbeta(25, 33, 12))
p <- plot(c1, type = 'l')
# set thresholds
thresholds <- c(0.7, 0.77)
I can find no way to access the segment line object plotted by R. I really really really would like to do this with base graphics, while realizing that probably there's a ggplot2 concoction out there that would work. Any idea?
abline(h=thresholds, lwd=1, lty=3, col="dark grey")
I will just do one threshold. You can loop through the list to get all of them.
First find the points, x, so that the curve crosses the threshold between x and x+1
shift = (c1 - 0.7)
Lower = which(shift[-1]*shift[-length(shift)] < 0)
Find the actual points of crossing, by finding the roots of Series - 0.7 and plot
shiftedF = approxfun(1:length(c1), c1-0.7)
Intersections = sapply(Lower, function(x) { uniroot(shiftedF, x:(x+1))$root })
points(Intersections, rep(0.7, length(Intersections)), pch=16, col="red")

How to plot two Dataset in same graph using poweRlaw

I have two data set, I need to plot them in same graph. Here is the two dataset.
The following is the code I used to plot the data. How to plot above data in same plot ? How to set the graph legend on the x-axis? I tried setting it but it didn't work I got some error.
m_bs = conpl$new(sample_data1$V1)
m_eq = conpl$new(sample_data2$V1)
est = estimate_xmin(m_bs, xmax=5e+5)
est_eq = estimate_xmin(m_eq, xmax=Inf)
m_bs$setXmin(est_bs)
m_eq$setXmin(est_eq)
plot(m_bs)
lines(m_bs)
d = plot(m_eq, draw =FALSE)
points(d$x, d$y, col=2)
lines(m_eq,col=2,lwd=2)
Kindly let me know thanks.
You code works find for me when I used simulated data. However, I think your problem is with your data. In particular, you need to set the xlim values in your plot command. Something like:
min_x = min(sample_data1$V1, sample_data1$V2)
max_x = max(sample_data1$V1, sample_data1$V2)
plot(m_bs, xlim=c(min_x, max_x))
Should do the trick. To add a legend, just use the legend function
legend("bottomleft", col=1:2, legend = c("BS", "EQ"), lty=1)

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)

Plot with two Y axes : confidence intervals

I am trying to plot several points with error bars, with two y axes.
However at every call of the plotCI or errbar functions, a new plot is initialized - with or without par(new=TRUE) calls -.
require(plotrix)
x <- 1:10
y1 <- x + rnorm(10)
y2<-x+rnorm(10)
delta <- runif(10)
plotCI(x,y=y1,uiw=delta,xaxt="n",gap=0)
axis(side=1,at=c(1:10),labels=rep("a",10),cex=0.7)
par(new=TRUE)
axis(4)
plotCI(x,y=y2,uiw=delta,xaxt="n",gap=0)
I have also tried the twoord.plot function from plotrix, but I don't think it's possible to add the error bars.
With ggplot2 I have only managed to plot in two different panels with the same Y axis.
Is there a way to do this?
Use add=TRUE,
If FALSE (default), create a new plot; if TRUE, add error bars to an
existing plot.
For example the last line becomes:
plotCI(x,y=y2,uiw=delta,xaxt="n",gap=0,add=TRUE)
PS: hard to do this with ggplot2. take a look at this hadley code
EDIT
The user coordinate system is now redefined by specifying a new user setting. Here I do it manually.
plotCI(x,y=y1,uiw=delta,xaxt="n",gap=0)
axis(side=1,at=c(1:10),labels=rep("a",10),cex=0.7)
usr <- par("usr")
par(usr=c(usr[1:2], -1, 20))
plotCI(x,y=y2,uiw=delta,xaxt="n",gap=0,add=TRUE,col='red')
axis(4,col.ticks ='red')

Resources