简体   繁体   中英

Regression line in ggplot2

I am trying to add a regression line to the below plot using ggplot, but it keeps giving me vague errors. I am a newbie, and none of the other questions regarding this subject solved my problem, so please don't get pissed off about similar questions already answered.

library(UsingR,ggplot2); data(galton)  
y <- galton$child  
x <- galton$parent  
freqData <- as.data.frame(table(galton$child, galton$parent))  
names(freqData) <- c("child", "parent", "freq")  
regression <- coef(lm(y~x))  

freqData <- freqData[freqData$freq > 0,]  

g <- ggplot(data=freqData, aes(x = parent, y = child))  
g <- g + scale_size(range = c(2,20), guide = 'none')  
g <- g + geom_point(colour="grey50", aes(size=freq+20,show_guide=FALSE))  
g <- g + geom_point(aes(colour=freq,size=freq))  
g <- g + scale_colour_gradient(low="lightblue",high="darkblue")  

I have tried the below commands:

g <- g + geom_smooth(method="lm",se=FALSE)

(it yields this error: geom_smooth: Only one unique x value each group.Maybe you want aes(group = 1)? )

and

g <- g + geom_abline(intercept = 28.942, slope = 0.646,colour = "red",size = 3)

(but nothing appears on my plot...)

Here is a data.table-solution (write-up prompted by@MikeWise, to showcase the cool plot you designed)

library(UsingR,ggplot2); data(galton)  
library(data.table)

#making data.table object
dat <- galton
setDT(dat)

#getting frequencies    
freqData <- dat[,.(freq=.N),by=.(child,parent)]


g <- ggplot(data=freqData, aes(x = parent, y = child))  
g <- g + scale_size(range = c(2,20), guide = 'none')  
g <- g + geom_point(colour="grey50", aes(size=freq+20,show_guide=FALSE))  
g <- g + geom_point(aes(colour=freq,size=freq))  
g <- g + scale_colour_gradient(low="lightblue",high="darkblue")  
g <- g + geom_smooth(method="lm",se=FALSE)
g

在此输入图像描述

First option

Keep using the function table .We use type.convert to convert the variables parent and child to their appropiate types before plotting the chart.

library(UsingR,ggplot2); data(galton)

# Create data frame
freqData <- data.frame(table(galton$child, galton$parent))
names(freqData) <- c("child", "parent", "freq")  
freqData <- freqData[freqData$freq > 0,] 

# Convert factors to numeric
freqData[] <- lapply(freqData, function(x) type.convert(as.character(x)))

Second option

Using the function aggregate , to prevent type conversion.

freqData <- aggregate(galton, by = list(parent = galton$parent, child = galton$child), 
                      FUN = length)
colnames(freqData)[3] <- "freq" 

Third option

Using dplyr to avoid type conversion.

library(dplyr)
freqData <- galton  %>%  group_by(parent, child) %>% summarise(freq = n())

Plotting the data frame created previously by one of the three options.

# Plot data
g <- ggplot(data=freqData, aes(x = parent, y = child))+ 
  scale_size(range = c(2,20), guide = 'none')  +
  geom_point(colour="grey50", aes(size=freq+20,show_guide=FALSE)) +
  geom_point(aes(colour=freq,size=freq)) +
  scale_colour_gradient(low="lightblue",high="darkblue") +
  geom_smooth(method = lm, se = FALSE)
g

在此输入图像描述

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