head(anscombe)
## Set X Y
## 1 1 10 8.04
## 2 1 8 6.95
## 3 1 13 7.58
## 4 1 9 8.81
## 5 1 11 8.33
## 6 1 14 9.96
str(anscombe)
## 'data.frame': 44 obs. of 3 variables:
## $ Set: int 1 1 1 1 1 1 1 1 1 1 ...
## $ X : int 10 8 13 9 11 14 6 4 12 7 ...
## $ Y : num 8.04 6.95 7.58 8.81 8.33 ...
anscombe$Set <- as.factor(anscombe$Set)
str(anscombe)
## 'data.frame': 44 obs. of 3 variables:
## $ Set: Factor w/ 4 levels "1","2","3","4": 1 1 1 1 1 1 1 1 1 1 ...
## $ X : int 10 8 13 9 11 14 6 4 12 7 ...
## $ Y : num 8.04 6.95 7.58 8.81 8.33 ...
uno <- subset(anscombe, Set=="1")
dos <- subset(anscombe, Set=="2")
tres <- subset(anscombe, Set=="3")
cuatro <- subset(anscombe, Set=="4")
## XProm YProm Xvar Yvar Corr
## 1 9 7.500682 10.23256 3.837388 0.8163662
## XProm YProm Xvar Yvar Corr
## 1 9 7.500909 11 4.127269 0.8164205
## XProm YProm Xvar Yvar Corr
## 1 9 7.500909 11 4.127629 0.8162365
## XProm YProm Xvar Yvar Corr
## 1 9 7.5 11 4.12262 0.8162867
## XProm YProm Xvar Yvar Corr
## 1 9 7.500909 11 4.123249 0.8165214
library(ggplot2)
# Canvas sobre el que vamos a dibujar
plotAns <- ggplot(anscombe,aes(X,Y, color = Set))
# Diagrama de lineas
plotAns <- plotAns + geom_line()
# regresión lineal
plotAns <- plotAns + geom_smooth(method=lm, se=FALSE)
plotAns
# Canvas sobre el que vamos a dibujar
plotAns <- ggplot(anscombe,aes(X,Y))
# Diagrama de dispersion
plotAns <- plotAns + geom_point()
# Regresion lineal
plotAns <- plotAns + geom_smooth(method=lm, se=FALSE)
# promedio X
plotAns <- plotAns + geom_vline (aes ( xintercept = SummaryStats[1,1]))
# promedio Y
plotAns <- plotAns + geom_hline (aes ( yintercept = SummaryStats[1,2]))
# facetas
plotAns <- plotAns + facet_grid(. ~ Set)
plotAns