Combine multiple ggpredict plots in one - r

I build a model with the lmer-fuction (m1). Now I want to plot the predicting values. I used this code:
p1 <- ggpredict(m1, "variable1")
p2 <- ggpredict(m1, "variable2")
p3 <- ggpredict(m1, "variable3")
With plot(p1) I would get the single plot as output. But I want that all three plots are visualized in one. Is there a way to overlay/combine them?

We can use patchwork to arrange multiple plots
library(patchwork)
p1|p2|p3

Related

Combine quantile and continuous models in plot (cph, rms)?

I would like to combine continuous and quantile models in the same plot to compare and contrast the two approaches (xtile is a function that returns the quantile as factor):
q.s <- cph(inc ~ rcs(exposure,3), data=data)
q.q <- cph(inc ~ xtile(exposure,3), data=data)
p.s <- Predict(q.s, exposure, fun=exp)
p.q <- Predict(q.q, exposure, fun=exp)
ggplot.Predict gives a nice plot of either model - but I would like to combine both. Is this possible?
I have added an example - which I hope might illustrate what I would like to generate.
enter image description here
Try this where plot1/2 are your quantile and continuous plots (can change heights, widths, number of columns, number of rows):
p1 <- ggplot.Predict(..plot1 options here...)
p2 <- ggplot.Predict(..plot2 options here...)
library(gridExtra)
grid.arrange(p1, p2,
ncol=2, nrow=1, widths=c(2,2), heights=c(2))
For more information, check out the gridExtra package.

Combining output from smatr with ggplot2

I have a dataset of leaf trait measurements made at multiple sites at two contrasting seasons. I am interested to explore the association/line fit between a pair of traits and to differentiate the seasons at each site.
Rather than a linear regression, I would prefer to use the Standardised Major Axis approach within the smatr package:
e.g. sma.site1 <- sma(TraitA ~ TraitB * Visit, data=subset(myfile, Site=="Site1")) # testing the null hypothesis of common slopes for the two Visits (Seasons) at a given Site.
I can produce a handy lattice plot in ggplot2 with a separate panel for each Site and the points differentiated by Visit:
e.g. qplot(TraitB, TraitA, data=myfile, colour=Visit) + facet_wrap(~Site, ncol=2)
However, if I add trend lines fitted with the additional argument in ggplot2:
+ geom_smooth(aes(group=Visit), method="lm", se=F)
……, those lines are not a good match for the sma coefficients.
What I would like to do is fit the lines suggested by the sma test onto the ggplot lattice. Is there an easy, or efficient, way to do that?
I know that I can subset the data, produce a plot for each site, add the relevant lines with + geom_abline() and then stitch the separate plots up together with grid.arrange(). But that feels very long-winded.
I would be grateful for any pointers.
I don't know anything about the smatr package but you should be able to tweak this to get the right values. Since you provided no data I used the leaf data from the example in the pkg. The basic idea is to pull out the slope & intercept from the returned sma object and then facet the geom_abline. I may be misinterpreting the object, though.
library(smatr)
library(ggplot2)
data(leaflife)
do.call(rbind, lapply(unique(leaflife$site), function(x) {
obj <- sma(longev~lma*rain, data=subset(leaflife, site=x))
data.frame(site=x,
intercept=obj$coef[[1]][1, 1],
slope=obj$coef[[1]][2, 1])
})) -> fits
gg <- ggplot(leaflife)
gg <- gg + geom_point(aes(x=lma, y=longev, color=soilp))
gg <- gg + geom_abline(data=fits, aes(slope=slope, intercept=intercept))
gg <- gg + facet_wrap(~site, ncol=2)
gg
I just saw this question and am not sure if you are still interested in this. I run the code by hrbrmstr, and found actually the only thing you need to change is:
obj <- sma(longev~lma*rain, data=subset(leaflife, site == x))
then you can get the plot with four lines for each group.
and also

R - How to histogram multiple matrixes using qplot/ggplot2

I'm using R to read and plot data from NetCDF files (ncdf4). I've started using R only recently thus I'm very confused, I beg your pardon.
Let's say from the files I obtain N 2-D matrixes of numerical values, each with different dimensions and many NA values.
I have to histogram these values in the same plot, with bins of given width and within given limits, the same for every matrix.
For just one matrix, I can do this:
library(ncdf4)
library(ggplot2)
file0 <- nc_open("test.nc")
#Read a variable
prec0 <- ncvar_get(file0,"pr")
#Some settings
min_plot=0
max_plot=30
bin_width=2
xlabel="mm/day"
ylabel="PDF"
title="Precipitation"
#Get maximum of array, exclude NAs
maximum_prec0=max(prec0, na.rm=TRUE)
#Store the histogram
histo_prec0 <- hist(prec0, xlim=c(min_plot,max_plot), right=FALSE, breaks=seq(0,ceiling(maximum_prec0),by=bin_width))
#Plot the histogram densities using points instead of bars, which is what we want
qplot(histo_prec0$mids, histo_prec0$density, xlim=c(min_plot,max_plot), color=I("yellow"), xlab=xlabel, ylab=ylabel, main=title, log="y")
#If necessary, can transform matrix to vector using
#vector_prec0 <- c(prec0)
However it occurs to me that it would be best to use a DataFrame for plotting multiple matrixes. I'm not certain of that nor on how to do it. This would also allow for automatic legends and all the advantages that come from using dataframes with ggplot2.
What I want to achieve is something akin to this:
https://copy.com/thumbs_public/j86WLyOWRs4N1VTi/scatter_histo.jpg?size=1024
Where on Y we have the Density and on X the bins.
Thanks in advance.
To be honest, it is unclear what you are after (scatter plot or histogram of data with values as points?).
Here are a couple of examples using ggplot which might fit your goals (based on your last sentence: "Where on Y we have the Density and on X the bins"):
# some data
nsample<- 200
d1<- rnorm(nsample,1,0.5)
d2<- rnorm(nsample,2,0.6)
#transformed into histogram bins and collected in a data frame
hist.d1<- hist(d1)
hist.d2<- hist(d2)
data.d1<- data.frame(hist.d1$mids, hist.d1$density, rep(1,length(hist.d1$density)))
data.d2<- data.frame(hist.d2$mids, hist.d2$density, rep(2,length(hist.d2$density)))
colnames(data.d1)<- c("bin","den","group")
colnames(data.d2)<- c("bin","den","group")
ddata<- rbind(data.d1,data.d2)
ddata$group<- factor(ddata$group)
# plot
plots<- ggplot(data=ddata, aes(x=bin, y=den, group=group)) +
geom_point(aes(color=group)) +
geom_line(aes(color=group)) #optional
print(plots)
However, you could also produce smooth density plots (or histograms) directly in ggplot:
ddata2<- cbind(c(rep(1,nsample),rep(2,nsample)),c(d1,d2))
ddata2<- as.data.frame(ddata2)
colnames(ddata2)<- c("group","value")
ddata2$group<- factor(ddata2$group)
plots2<- ggplot(data=ddata2, aes(x=value, group=group)) +
geom_density(aes(color=group))
# geom_histogram(aes(color=group, fill=group)) # for histogram instead
windows()
print(plots2)

Adjusting distance between plots

I have a couple of ggplot plots that I want to merge using grid.arrange()
When I merge the plots there's a large white area around each plot making them far away from each other.
Is there a way to adjust the distance between the plots? and the size of the white area around the plots?
You can use theme(plot.margin) function in ggplot2 to reduce the spacing.
A simple working example here :
library(grid)
library(gridExtra)
library(ggplot2)
x <- seq(1,10,1)
y <- dnorm(x,mean=10,sd=0.5)
# Create p1
p1 <- qplot(x,y) + theme(plot.margin=unit(c(1,1,-0.5,1),"cm"))
# Create p2
p2 <- qplot(x,y) + theme(plot.margin=unit(c(-0.5,1,1,1),"cm"))
grid.arrange(p1,p2)
Edit
The four numbers are c(bottom,left,top,right)
Sample output

How to combine three separate interaction plots into a single figure?

Apologies for that. Here's my question with a reproducible data set:
library(effects)
data(Arrests)
Arrests$year <- as.factor(Arrests$year)
arrests.mod <- glm(released ~ employed + citizen + checks + colour*year +
colour*age, family=binomial, data=Arrests)
t.effects <- allEffects(arrests.mod)
plot(t.effects, "colour:year")
plot(t.effects, "colour:age")
Is it possible to combine the two plots into a single figure?
par(mfrow=c(2,1))
This doesn't work. I.e. the figures are reproduced separately in two graphs, but not in the same figure.
par(mfrow=c(2,1)) don't work for grid plots. It is only for base graphics. You can use gridExtra to arrange lattice plots.
library(gridExtra)
p1 <- plot(t.effects, "colour:year")
p2 <- plot(t.effects, "colour:age")
grid.arrange(latticeGrob(p1),
latticeGrob(p2))
You can reference specific effects from your alleffects object with vector indices. In your case I believe t.effects[4:5] or, equivalently, t.effects[c("colour:year", "colour:age")]
library(effects)
data(Arrests)
Arrests$year <- as.factor(Arrests$year)
arrests.mod <- glm(released ~ employed + citizen + checks + colour*year + colour*age, family=binomial, data=Arrests)
t.effects <- allEffects(arrests.mod)
plot(t.effects[4:5])

Resources