Paquetes requeridos “visred” y “ggplot2”

library(visreg)
lm1 <- lm(Ozone ~ Solar.R + Wind,data=airquality)
visreg(lm1, partial=T)

#Funcion Partial permite visualizar los residuos=TRUE
library(visreg)
lm2 <- lm(Ozone ~ Solar.R, data=airquality)
visreg(lm2, partial=F)

library(ggplot2)
ggplot(airquality, aes(x=Temp, y=Ozone))+ geom_point()
## Warning: Removed 37 rows containing missing values (geom_point).

ggplot(airquality, aes(x=Temp, y=Ozone))+ geom_point()+geom_smooth(se = FALSE)
## Warning: Removed 37 rows containing non-finite values (stat_smooth).
## Warning: Removed 37 rows containing missing values (geom_point).

ggplot(airquality, aes(x=Temp, y=Ozone))+ geom_point()+geom_smooth(method = lm,se = FALSE)
## Warning: Removed 37 rows containing non-finite values (stat_smooth).
## Warning: Removed 37 rows containing missing values (geom_point).

ggplot(airquality, aes(x=Temp, y=Ozone))+ geom_point(aes(colour = factor(Month)))+geom_smooth(se = FALSE)
## Warning: Removed 37 rows containing non-finite values (stat_smooth).
## Warning: Removed 37 rows containing missing values (geom_point).

ggplot(airquality, aes(x=Temp, y=Ozone))+ geom_point(colour = "red3", size = 2)+geom_smooth(se = FALSE)
## Warning: Removed 37 rows containing non-finite values (stat_smooth).
## Warning: Removed 37 rows containing missing values (geom_point).

ggplot(airquality, aes(x=Temp, y=Ozone))+ geom_point(shape = 21, colour = "black", fill = "yellow", size = 2, stroke = 1)+ geom_smooth(se = FALSE)
## Warning: Removed 37 rows containing non-finite values (stat_smooth).
## Warning: Removed 37 rows containing missing values (geom_point).

Recta con dos pendientes

data(mtcars)
head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
ggplot(mtcars) + 
  geom_jitter(aes(x=disp,y=mpg), colour="blue") + geom_smooth(aes(disp,mpg), method=lm, se=FALSE) +
  geom_jitter(aes(x=hp,y=mpg), colour="red") + geom_smooth(aes(hp,mpg), method=lm, se=FALSE) +
  labs(x = "Displacement", y = "Unidades (N)")

En algunas ocasiones podriamos tener organizados nuestros datos en forma concatenados por columnas.

plantas <- data.frame(y = runif(100), 
                   x = runif(100), 
                   ID = sample(c("GrupoA","GrupoB"), size = 100, replace = TRUE))
plantas = within(plantas, {
          y[ID == "GrupoA"] = y[ID == "GrupoA"] + 0.5
          y[ID == "GruposB"] = y[ID == "GrupoB"] - 0.5
  })
head(plantas)
##           y          x     ID
## 1 0.8419401 0.47235632 GrupoA
## 2 0.5084067 0.44529016 GrupoB
## 3 0.7746432 0.43098675 GrupoB
## 4 0.6685016 0.69141651 GrupoB
## 5 0.5612129 0.67445061 GrupoB
## 6 0.9377578 0.01555604 GrupoA
ggplot(plantas, aes(x = x, y = y, color = ID)) + 
   geom_point() + geom_smooth(method = "lm")

#Multiples regresiones
#Para ello hay que ajustar los datos con "reshape"
library(reshape2)
head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
mtcars2 = melt(mtcars, id.vars='mpg') #Utilizamos la variable dependiente
head(mtcars2)
##    mpg variable value
## 1 21.0      cyl     6
## 2 21.0      cyl     6
## 3 22.8      cyl     4
## 4 21.4      cyl     6
## 5 18.7      cyl     8
## 6 18.1      cyl     6
ggplot(mtcars2) +
  geom_jitter(aes(value,mpg, colour=variable),) + geom_smooth(aes(value,mpg, colour=variable), method=lm, se=FALSE) +
  facet_wrap(~variable, scales="free_x") +
  labs(x = "Displacement", y = "Unidades (N)")