How to replicate plot with two panels? - r

I am trying to replicate this plot, here:
Here is the source of this plot, slide 89:
http://www.drizopoulos.com/courses/Int/JMwithR_CEN-ISBS_2017.pdf
The top of the plot is the hazard function over time, whereas the bottom green curve is the fitted linear mixed effects model over time.
I have been able to plot both of these separately, however, cannot seem to combine them using either par(mfrow=c(2,1)) or the gridExtra package (because only one is a ggplot object).
I am using the aids and aids.id datasets (as a part of the JM package) in R.
# Load packages JM and lattice
library("JM")
library("lattice")
library("ggplot2")
#Fit models
lmeFit.aids <- lme(CD4 ~ obstime + obstime:drug,
random = ~ obstime | patient, data = aids)
coxFit.aids <- coxph(Surv(Time, death) ~ drug, data = aids.id, x = TRUE)
#Plot longitudinal process
p1<-ggplot(data=aids,aes(x=obstime,y=fitted(lmeFit.aids)))
p1<-p1+geom_smooth(se=FALSE)
p1
#Plot survival process
library(rms)
p2<-psm(Surv(Time,death)~1,data=aids.id)
survplot(p2,what='hazard')
Thank you!

Up front, patchwork allows you to combine ggplot2 and base graphics into one plot. Adapted from ?wrap_elements,
library(ggplot2)
library(patchwork)
gg <- ggplot(mtcars, aes(mpg, disp)) + geom_point()
gg / wrap_elements(full = ~ plot(mtcars$mpg, mtcars$disp))

I was able to extract the values of the hazard at various time points using the survest() function. Then, I was able to plot this using ggplot, meaning I could use grid.arrange().
est<-survest(p2,,what='hazard')
hazard<-data.frame(time=est$time, hazard=est$surv)

Related

Add r square and equation on a scatter plot

My r² and equation doesn't show on my scatter plot using visreg function
library(visreg)
variable1_lm<- lm(variable1 ~ variable2,
data = PCA)
visreg(variable1_lm , "variable2", gg = TRUE)
What code should I use so it displays on top of it?
Thanks
You should reproducible data with your code. Here is an example with the iris data set that comes with R:
library(visreg)
library(ggplot2)
SL.lm <- lm(Sepal.Length~Sepal.Width, iris)
RSQ <- summary(SL.lm)$r.squared
visreg(SL.lm, gg=TRUE) + ggtitle(paste("R-Square =", round(RSQ, 4)))

Plotting an nlme object in ggplot2

I calculated a linear-mixed model using the nlme package. I was evaluating a psychological treatment and used treatment condition and measurement point as predictors. I did post-hoc comparisons using the emmans package. So far so good, everything worked out well and I am looking forward to finish my thesis. There is only one problem left. I am really really bad in plotting. I want to plot the emmeans for the four measurement points for each group. The emmip function in emmeans does this, but I am not that happy with the result. I used the following code to generate the result:
emmip(HLM_IPANAT_pos, Gruppe~TP, CIs=TRUE) + theme_bw() + labs(x = "Zeit", y = "IPANAT-PA")
I don't like the way the confidence intervals are presented. I would prefer a line bar with "normal" confidence bars, like the one below, which is taken from Ireland et al. (2017). I tried to do it in excel, but did not find out how to integrate seperate confidence intervals for each line. So I was wondering if there was the possibility to do it using ggplot2. However, I do not know how to integrate the values I obtained using emmeans in ggplot. As I said, I really have no idea about plotting. Does someone know how to do it?
I think it is possible. Rather than using emmip to create the plot, you could use emmeans to get the values for ggplot2. With ggplot2 and the data, you might be able to better control the format of the plot. Since I do not have your data, I can only suggest a few steps.
First, after fitting the model HLM_IPANAT_pos, get values using emmeans. Second, broom::tidy this object. Third, ggplot the above broom::tidy object.
Using mtcars data as an example:
library(emmeans)
# mtcars data
mtcars$cyl = as.factor(mtcars$cyl)
# Model
mymodel <- lm(mpg ~ cyl * am, data = mtcars)
# using ggplot2
library(tidyverse)
broom::tidy(emmeans(mymodel, ~ am | cyl)) %>%
mutate(cyl_x = as.numeric(as.character(cyl)) + 0.1*am) %>%
ggplot(aes(x = cyl_x, y = estimate, color = as.factor(am))) +
geom_point() +
geom_line() +
geom_errorbar(aes(ymin = conf.low, ymax = conf.high), width = 0.1)
Created on 2019-12-29 by the reprex package (v0.3.0)

r: Blank graph when plotting multiple lines on scatterplot

My goal is to produce a graph showing the differences between regression lines using continuous vs categorical variables. I'm using is the "SleepStudy" dataset from Lock5Data, and I want to show the regression lines predicting GPA from ClassYear as either continuous or categorical. The code is below:
library(Lock5Data)
data("SleepStudy")
fit2 <- lm(GPA ~ factor(ClassYear), data = SleepStudy)
fit2_line <- aggregate(fit2$fitted.values ~ SleepStudy$ClassYear, FUN = mean)
colnames(fit2_line) <- c('ClassYear','GPA')
options(repr.plot.width=5, repr.plot.height=5)
library(ggplot2)
ggplot() +
geom_line(data=fit2_line, aes(x=ClassYear, y=GPA)) + # Fit line, ClassYear factor
geom_smooth(data=SleepStudy, method='lm', formula=GPA~ClassYear) + # Fit line, ClassYear continuous
geom_point(data=SleepStudy, aes(x=ClassYear, y=GPA)) # Data points as dots
What is producing the blank graph? What am I missing here?
You have to define the data you are using for the geom_smooth in the ggplot(). This code works:
ggplot(data=SleepStudy, aes(y = GPA,x = ClassYear)) +
geom_smooth(data=SleepStudy, method='lm', formula=y~x)+
geom_line(data=fit2_line, aes(x=ClassYear, y=GPA)) +
geom_point(data=SleepStudy, aes(x=ClassYear, y=GPA))

Stacke different plots in a facet manner

To train with ggplot and to improve my skills in writing R functions I decided to build a series of functions that produces survival plots, with all kinds of extras. I managed to build a good working function for the basic survival plot, now I am getting to the extras. One thing I would like to do is an option that stacks an area plot of the number at risk at a given time point, on top of the survival plot. I would like it to look just like the facet_grid option of ggplot, but I did not manage to do it with this function. I do not want the two plots binded, like we can do with grid.arrange, but rather to have the same x-axis.
The following code produces the two (simplified) plots that I would like to stack on top of each other. I tried to do this with facet_grid, but I don't think the solution lies in this
library(survival)
library(ggplot2)
data(lung)
s <- survfit(Surv(time, status) ~ 1, data = lung)
dat <- data.frame(time = c(0, s$time),
surv = c(1, s$surv),
nr = c(s$n, s$n.risk))
pl1 <- ggplot(dat, aes(time, surv)) + geom_step()
pl2 <- ggplot(dat, aes(time, nr)) + geom_area()
First, melt your data to long format.
library(reshape2)
dat.long<-melt(dat,id.vars="time")
head(dat.long)
time variable value
1 0 surv 1.0000000
2 5 surv 0.9956140
3 11 surv 0.9824561
4 12 surv 0.9780702
5 13 surv 0.9692982
6 15 surv 0.9649123
Then use subset() to use only surv data in geom_step() and nr data in geom_area() and with facet_grid() you will get each plot in separate facet as variable is used to divide data for facetting and for subsetting. scales="free_y" will make pretty axis.
ggplot()+geom_step(data=subset(dat.long,variable=="surv"),aes(time,value))+
geom_area(data=subset(dat.long,variable=="nr"),aes(time,value))+
facet_grid(variable~.,scales="free_y")

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