Adding a 95% confidence interval to NMDS plot - r

I am trying to plot an NMDS plot of species community composition data with ellipses which represent 95% confidence intervals. I generated the data for my NMDS plot using metaMDS and successfully have ordinations generated using the basic plot functions in R (see code below). However, I am struggling to get my data to plot successfully using ggplot2 and this is the only way I have seen 95% CIs plotted on NMDS plots. I am hoping someone is able to help me correct my code so the ellipses show 95% CIs, or could point me in the right direction for achieving this using other methods?
My basic code for plotting my NMDS plot:
orditorp(dung.families.mds, display = "sites", labels = F, pch = c(16, 8, 17, 18) [as.numeric(group.variables$Heating)], col = c("green", "blue", "orange", "black") [as.numeric(group.variables$Dungfauna)], cex = 1.3)
ordiellipse(dung.families.mds, groups = group.variables$Dungfauna, draw = "polygon", lty = 1, col = "grey90")
legend("topleft", "stress = 0.1329627", bty = "n", cex = 1)
My ordination:

I realize this question is old, but I found this post useful for plotting confidence ellipses during my work, and maybe it will help you. Plotting ordiellipse function from vegan package onto NMDS plot created in ggplot2
Edit: Below I have copied the code from the second part of Didzis Elferts's answer on the link above.
Where "sol" is the metaMDS object:
First, make NMDS data frame with group column.
NMDS = data.frame(MDS1 = sol$points[,1], MDS2 = >sol$points[,2],group=MyMeta$amt)
Next, save result of function ordiellipse() as some object.
ord<-ordiellipse(sol, MyMeta$amt, display = "sites", >kind = "se", conf = 0.95, label = T)
Data frame df_ell contains values to show ellipses. It is calculated again with function veganCovEllipse which is hidden in vegan package. This function is applied to each level of NMDS (group) and now it uses arguments stored in ord object - cov, center and scale of each level.
df_ell <- data.frame()
for(g in levels(NMDS$group)){
df_ell <- rbind(df_ell, cbind(as.data.frame(with(NMDS[NMDS$group==g,],
veganCovEllipse(ord[[g]]$cov,ord[[g]]$center,ord[[g]]$scale)))
,group=g))
}
Plotting is done the same way as in previous example. As for the calculating of coordinates for elipses object of ordiellipse() is used, this solution will work with different parameters you provide for this function.
ggplot(data = NMDS, aes(MDS1, MDS2)) + geom_point(aes(color = group)) +
geom_path(data=df_ell, aes(x=NMDS1, y=NMDS2,colour=group), size=1, linetype=2)

Related

Is there a way to rotate the x-axis labels in the visreg package?

*** EDIT **** Figured it out: scales = list(rot = 90)
I am using the visreg() function to plot a linear model comparing bird weights between years. I have three different species, so the graph is very crowded. I want to rotate the x-axis labels so they are perpendicular, but I cannot figure out how to do it. I have used las = 2 in numerous locations, to no avail. Help would be appreciated! Thank you.
visreg(hawk_lm1, xvar = "Year", by = "Species", whitespace = 0.4, las = 2,
points.par = list(cex = 0.5, col = "grey"))
A screenshot of the graph as it is at the moment
According to the documentation of the visreg package, if you use the by= argument the plot is made using the lattice package. In Lattice you can use the scales= argument alongside rot() to rotate the axis, just include the parameter in the visreg function as an additional argument and it should do the job. I created this example from the airquality dataset to illustrate.
library(visreg)
airquality$Heat <- cut(airquality$Temp, 3, labels=c("Cool", "Mild", "Hot"))
fit <- lm(Ozone ~ Solar.R + Wind + Heat, data=airquality)
##default x axis labels
visreg(fit, "Wind", by="Heat", bty="n", ylab="Ozone")
##rotated x axis labels
visreg(fit, "Wind", by="Heat", bty="n", ylab="Ozone", scales=list(x=list(rot=90)))

How to draw forest plot from Dataframe (HR and CI)

I have a data of cox regression from spss containing following columns.
I am thinking to use this data as dataframe in R and create a forest plot out of it. How can i create a forest plot from this data in R? How to create forest plot from dataframe containing HR/OR and CIs. ?
Here is reproducable data as follows, it would be great help if you teach me how to make one. I tried but couldnt make one.
HR<-c(2,3,5)
ci_u<-c(1.2,1.1,1.3)
ci_l<-c(1.3,1.4,1.3)
names<-c("High","Low","medium")
datf<-data.frame(HR,ci_u,ci_l,sig,ns)
I am suggesting a simple ggplot approach as it offers great control. The underlying idea is to plot HRs as points and then add CIs as error bars. I altered your dataset because
you did not define sig and ns variables in your data frame
the point estimates do not fall between upper and lower CI values. I understand that you made up these values, but I am changing since the plot wont look good as the CI lines will fall only at one side of the point.
I used the following dataframe
dataset <- data.frame(
study_label = c(paste(rep("Study", 4), 1:4, sep = "_")),
HR = c(.72, 1.4, 1.7, 1.4),
lci = c(.52, 1.1, 1.3, 1.2),
uci = c(.83, 1.9, 2.1, 1.5)
)
require(ggplot2)
ggplot(dataset, aes(y = study_label, x = HR))+
geom_point()+ #map HRs as points on x axis and variables/study labels at y
geom_errorbar(aes(xmin = lci, xmax = uci))+ #add CIs as error bars
geom_vline(xintercept = 1, linetype = "dashed")#draw a vertical line at x=1 as null for ratio estimates
Please see the output

Share area of full confidence interval (base graphics)

I am using the following code in R to a plot a linear regression with confidence interval bands (95%) around the regression line.
Average <- c(0.298,0.783429,0.2295,0.3725,0.598,0.892,2.4816,2.79975,
1.716368,0.4845,0.974133,0.824,0.936846,1.54905,0.8166,1.83535,
1.6902,1.292667,0.2325,0.801,0.516,2.06645,2.64965,2.04785,0.55075,
0.698615,1.285,2.224118,2.8576,2.42905,1.138143,1.94225,2.467357,0.6615,
0.75,0.547,0.4518,0.8002,0.5936,0.804,0.7,0.6415,0.702182,0.7662,0.847)
Area <-c(8.605,16.079,4.17,5.985,12.419,10.062,50.271,61.69,30.262,11.832,25.099,
8.594,17.786,36.995,7.473,33.531,30.97,30.894,4.894,8.572,5.716,45.5,69.431,
40.736,8.613,14.829,4.963,33.159,66.32,37.513,27.302,47.828,39.286,9.244,19.484,
11.877,9.73,11.542,12.603,9.988,7.737,9.298,14.918,17.632,15)
lm.out <- lm (Area ~ Average)
newx = seq(min(Average), by = 0.05)
conf_interval <- predict(lm.out, newdata = data.frame(Average = newx), interval ="confidence",
level = 0.95)
plot(Average, Area, xlab ="Average", ylab = "Area", main = "Regression")
abline(lm.out, col = "lightblue")
lines(newx, conf_interval[,2], col = "blue", lty ="dashed")
lines(newx, conf_interval[,3], col = "blue", lty ="dashed")
I am stuck because the graph I got reports the bands just for the first part pf the line, leaving out all the remaining line (you find the link to the image at the bottom of the message). What is going wrong? I would also like to shade the area of the confidence interval (not just the lines corresponding to the limits) but I can't understand how to do it.
Any help would be really appreciated, I am completely new in R.
This is very easy with the ggplot2 -library. Here is the code:
library(ggplot2)
data = data.frame(Average, Area)
ggplot(data=data, aes(x=Area, y=Average))+
geom_smooth(method="lm", level=0.95)+
geom_point()
Code to install the library:
install.packages("ggplot2")

Confidence interval bands in ggplot2 when using stat_quantile?

I would like to add the median spline and corresponding confidence interval bands to a ggplot2 scatter plot. I am using the 'quantreg'-package, more specifically the rqss function (Additive Quantile Regression Smoothing).
In ggplot2 I am able to add the median spline, but not the confidence interval bands:
fig = ggplot(dd, aes(y = MeanEst, x = N, colour = factor(polarization)))
fig + stat_quantile(quantiles=0.5, formula = y ~ qss(x), method = "rqss") +
geom_point()
The quantreg-package comes with its own plot function; plot.rqss. Where I am able to add the confidence bands (bands=TRUE):
plot(1, type="n", xlab="", ylab="", xlim=c(2, 12), ylim=c(-3, 0)) # empty plot
plotfigs = function(df) {
rqss_model = rqss(df$MeanEst ~ qss(df$N))
plot(rqss_model, bands=TRUE, add=TRUE, rug=FALSE, jit=FALSE)
return(NULL)
}
figures = lapply(split(dd, as.factor(dd$polarization)), plotfigs)
However plot function that comes with the quantreg-package is not very flexible/well suited for my needs. Is it possible to get the confidence bands in a ggplot2 plot? Perhaps by mimicking the method used in the quantreg-package, or simply copying them from the plot?
Data: pastebin.
You almost have it. When you call
plot(rqss_model, bands=TRUE, add=TRUE, rug=FALSE, jit=FALSE)
The function very helpfully returns the plotted data. All we do is grab the data frame. First a minor tweak to your function, return the data in a sensible way
plotfigs = function(df) {
rqss_model = rqss(df$MeanEst ~ qss(df$N))
band = plot(rqss_model, bands=TRUE, add=TRUE, rug=FALSE, jit=FALSE)
data.frame(x=band[[1]]$x, low=band[[1]]$blo, high=band[[1]]$bhi,
pol=unique(df$polarization))
}
Next call the function and condense
figures = lapply(split(dd, as.factor(dd$polarization)), plotfigs)
bands = Reduce("rbind", figures)
Then use geom_ribbon to plot
## We inherit y and color, so have to set them to NULL
fig + geom_ribbon(data=bands,
aes(x=x, ymin=low, ymax=high,
y=NULL, color=NULL, group=factor(pol)),
alpha=0.3)

plotting nls fits with overlapping prediction intervals in a single figure

Say I some data, d, and I fit nls models to two subsets of the data.
x<- seq(0,4,0.1)
y1<- (x*2 / (0.2 + x))
y1<- y1+rnorm(length(y1),0,0.2)
y2<- (x*3 / (0.2 + x))
y2<- y2+rnorm(length(y2),0,0.4)
d<-data.frame(x,y1,y2)
m.y1<-nls(y1~v*x/(k+x),start=list(v=1.9,k=0.19),data=d)
m.y2<-nls(y2~v*x/(k+x),start=list(v=2.9,k=0.19),data=d)
I then want to plot the fitted model regression line over data, and shade the prediction interval. I can do this with the package investr and get nice plots for each subset individually:
require(investr)
plotFit(m.y1,interval="prediction",ylim=c(0,3.5),pch=19,col.pred='light blue',shade=T)
plotFit(m.y2,interval="prediction",ylim=c(0,3.5),pch=19,col.pred='pink',shade=T)
However, if I plot them together I have a problem. The shading of the second plot covers the points and shading of the first plot:
1: How can I make sure the points on the first plot end up on top of the shading of the second plot?
2: How can I make the region where the shaded prediction intervals overlap a new color (like purple, or any fusion of the two colors that are overlapping)?
Use adjustcolor to add transparency like this:
plotFit(m.y1, interval = "prediction", ylim = c(0,3.5), pch = 19,
col.pred = adjustcolor("lightblue", 0.5), shade = TRUE)
par(new = TRUE)
plotFit(m.y2, interval = "prediction", ylim = c(0,3.5), pch = 19,
col.pred = adjustcolor("light pink", 0.5), shade = TRUE)
Depending on what you want you can play around with the two transparency values (here both set to 0.5) and possibly make only one of them transparent.

Resources