简体   繁体   中英

ggplot2 and regression lines and R^2 values

I know there have been a number of entries with regards to adding R^2 values to plots, but I am having trouble following the codes. I am graphing a scatter plot with three categories. I have added a linear regression line for each one. I would now like to add r^2 values for each but I can't figure out how to do this.

My code:

veg <- read.csv("latandwtall2.csv", header=TRUE)

library("ggplot2")

a <- ggplot(veg, aes(x=avglat, y=wtfi, color=genus)) + geom_point(shape=19, size=4)
b <- a + scale_colour_hue(l=50) + stat_smooth(method = "lm", formula = y ~ x, size = 1, se = FALSE)
c <- b + labs(x="Latitude", y="Weight (g)")
d <- c + theme_bw()
e <- d + theme(panel.grid.minor=element_blank(), panel.grid.major=element_blank())

#changes size of text
f <- e + theme(
  axis.title.x = element_text(color="black", vjust=-0.35, size=15, face="bold"),
  axis.title.y = element_text(color="black" , vjust=0.35, size=15, face="bold")   
)
g <- e+theme(legend.key=element_rect(fill='white'))
g 

Any help with how to add R^2 values would be greatly appreciated. Thanks!

If you build a data frame with the r-squared values, you might be able to (mostly) automate the positioning of the annotation text by including it as a call to geom_text .

Here's a toy example. The rsq data frame is used in geom_text to place the r-squared labels. In this case, I've set it up to put the labels just after the highest x-value and the predict function gets the y-value. It's probably too much work for a single plot, but if you're doing this a lot, you can turn it into a function so that you don't have to repeat the set-up code every time, and maybe add some fancier logic to make label placement more flexible:

library(reshape2) # For melt function

# Fake data
set.seed(12)
x = runif(100, 0, 10)
dat = data.frame(x, y1 = 2*x + 3 + rnorm(100, 0, 5),
                 y2 = 4*x + 20 + rnorm(100, 0, 10))
dat.m = melt(dat, id.var="x")

# linear models
my1 = lm(y1 ~ x, data=dat)
my2 = lm(y2 ~ x, data=dat)

# Data frame for adding r-squared values to plot
rsq = data.frame(model=c("y1","y2"), 
                r2=c(summary(my1)$adj.r.squared,
                      summary(my2)$adj.r.squared),
                x=max(dat$x),
                y=c(predict(my1, newdata=data.frame(x=max(dat$x))),
                    predict(my2, newdata=data.frame(x=max(dat$x)))))

ggplot() + 
  geom_point(data=dat.m, aes(x, value, colour=variable)) + 
  geom_smooth(data=dat.m, aes(x, value, colour=variable), 
              method="lm", se=FALSE) +
  geom_text(data=rsq, aes(label=paste("r^2 == ", round(r2,2)), 
                          x=1.05*x, y=y, colour=model, hjust=0.5), 
            size=4.5, parse=TRUE)

在此处输入图片说明

I can't really reproduce what you're doing but you need to use annotate()

Something that could work (puting the R2 on the 10th point) would be :

R2 = 0.4
i = 10
text = paste("R-squared = ", R2, sep="")
g = g + annotate("text", x=avglat[i], y=wtfi[i], label=text, font="Calibri", colour="red", vjust = -2, hjust = 1)

Use vjust and hjust to adjust the position of the text to the point (change the i), and just fill the variable R2 with your computed rsquared. You can choose the point you like or manually enter the x,y coordinate it's up to you. Does that help ?

PS. I put extra parameters (font, colours) so that you have the flexibility to change them.

Build the model separately, get the R^2 from there, and add it to the plot. I'll give you some dummy code, but it would be of better quality if you had given us a sample data frame.

r2 = summary(lm(wtfi ~ avglat, data=veg))$r.squared
#to piggyback on Romain's code...
i=10
g = g + annotate("text", x=avglat[i], y=wtfi[i], label=round(r2,2), font="Calibri", colour="red", vjust = -2, hjust = 1)

The way I wrote it here you don't need to hard-code the R^2 value in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM