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)))
Related
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)
I am trying to plot my gam result. I want to turn the labels of the plots into Chinese. But, the x label will be used for all plots. How to creat different x-labels for different plots?
fit <- gam(happiness ~ s(age) + s(edu) + s(mobility), family = octa(R=5), data = data) plot(fit, xlab = c("年龄","教育”))
You could simply change the column names, not sure how to do this in Chinese though.
library(mgcv)
set.seed(2) ## simulate some data...
dat <- gamSim(1,n=400,dist="normal",scale=2)[1:3]
names(dat)[2:3] <- c("ONE", "TWO")
b <- gam(y~s(ONE)+s(TWO),data=dat)
plot(b,pages=1,residuals=TRUE) ## show partial residuals
From a bootstrapping model I have 1000 sets of coefficients for this regression model:
y = b0 + b1x + b2(x^2)
What is the function call to plot a quadratic line if I already have the coefficients? I.E. I do not want to "fit" a linear model to my data.
I tried adding lines via a for loop to my ggplot object:
for (i in 1:1000) {
reg_line <- stat_function(fun=function(x) quad$coefficients[1] +
quad$coefficients[i,2]*x + quad$coefficients[i,3]*(x**2))
reg_lines <- reg_lines + reg_line}
That didn't work - it seems to only add the last line in the loop.
The reason I want to add 1000 regression lines to my plot is because it is for a homework problem - I am well aware this is not a common use case.
There may be other ways to do this, but hopefully this can give you some ideas. I used the mtcars dataset and generated some bootstrap samples for modelling. You can skip this step.
library(ggplot2)
library(tidyr)
library(dplyr)
data(mtcars)
drat=seq(min(mtcars$drat), max(mtcars$drat), length.out=100)
# Bootstrap function
bs <- function() {
df = mtcars[sample(1:nrow(mtcars), replace=TRUE),]
lm_fit <- lm(mpg ~ drat+I(drat^2), data=df)
data.frame(Model=predict(lm_fit, newdata=data.frame(drat))) # Replace with your own
}
foo <- replicate(10, bs()) # Simulate
You would start from here since you should already have a data frame or list of predicted values from your 1,000 bootstrap models. Reshape it into a very long form to create a grouping column for the geom_line function.
foo_long <- data.frame(foo, drat) %>%
pivot_longer(cols=-drat, names_to="Model", values_to="mpg")
ggplot(data = mtcars, aes(x = drat, y = mpg)) +
geom_point(color='blue') +
geom_line(data = foo_long, aes(x=drat, y=mpg, group=Model, color=Model)) +
guides(color=FALSE)
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))
I would like to change the color of coefficient lines based on whether the point estimate is negative or positive in a ggplot2 coefficient plot in R. For example:
require(coefplot)
set.seed(123)
dat <- data.frame(x = rnorm(100), z = rnorm(100))
mod1 <- lm(y1 ~ x + z, data = dat)
coefplot.lm(mod1)
Which produces the following plot:
In this plot, I would like to change the "x" variable to red when plotted. Any ideas? Thanks.
I think, you cannot do this with a plot produced by coefplot.lm. The package coefplot uses ggplot2 as the plotting system, which is good itself, but does not allow to play with colors as easily as you would like. To achieve the desired colors, you need to have a variable in your dataset that would color-code the values; you need to specify color = color-code in aes() function within the layer that draws the dots with CE. Apparently, this is impossible to do with the output of coefplot.lm function. Maybe, you can change the colors using ggplot2 ggplot_build() function. I would say, it's easier to write your own function for this task.
I've done this once to plot odds. If you want, you may use my code. Feel free to change it. The idea is the same as in coefplot. First, we extract coefficients from a model object and prepare the data set for plotting; second, actually plot.
The code for extracting coefficients and data set preparation
df_plot_odds <- function(x){
tmp<-data.frame(cbind(exp(coef(x)), exp(confint.default(x))))
odds<-tmp[-1,]
names(odds)<-c('OR', 'lower', 'upper')
odds$vars<-row.names(odds)
odds$col<-odds$OR>1
odds$col[odds$col==TRUE] <-'blue'
odds$col[odds$col==FALSE] <-'red'
odds$pvalue <- summary(x)$coef[-1, "Pr(>|t|)"]
return(odds)
}
Plot the output of the extract function
plot_odds <- function(df_plot_odds, xlab="Odds Ratio", ylab="", asp=1){
require(ggplot2)
p <- ggplot(df_plot_odds, aes(x=vars, y=OR, ymin=lower, ymax=upper),asp=asp) +
geom_errorbar(aes(color=col),width=0.1) +
geom_point(aes(color=col),size=3)+
geom_hline(yintercept = 1, linetype=2) +
scale_color_manual('Effect', labels=c('Positive','Negative'),
values=c('blue','red'))+
coord_flip() +
theme_bw() +
theme(legend.position="none",aspect.ratio = asp)+
ylab(xlab) +
xlab(ylab) #switch because of the coord_flip() above
return(p)
}
Plotting your example
set.seed(123)
dat <- data.frame(x = rnorm(100),y = rnorm(100), z = rnorm(100))
mod1 <- lm(y ~ x + z, data = dat)
df <- df_plot_odds(mod1)
plot <- plot_odds(df)
plot
Which yields
Note that I chose theme_wb() as the default. Output is a ggplot2object. So, you may change it quite a lot.