library(ggplot2)
Conceptos básicos
head(mpg)
## manufacturer model displ year cyl trans drv cty hwy fl class
## 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compact
## 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compact
## 3 audi a4 2.0 2008 4 manual(m6) f 20 31 p compact
## 4 audi a4 2.0 2008 4 auto(av) f 21 30 p compact
## 5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compact
## 6 audi a4 2.8 1999 6 manual(m5) f 18 26 p compact
ggplot(data=mpg, aes(x=cty,y=hwy)) +
geom_point(aes(color=cyl)) +
geom_smooth(method="lm") +
coord_cartesian() +
scale_color_gradient() +
theme_bw() + xlab("titulo x") + labs(title="titulo principal")
ggsave("plot.png",width=5,height=5)
qplot(x=cty,y=hwy,color=cyl,data=mpg,geom="point")
Una variable
a <- ggplot(mpg,aes(hwy))
a + geom_area(stat="bin")
b <- geom_area(aes(y=..density..),stat="bin")
a + geom_density(kernel="gaussian")
# b + geom_density(aes(y=..county..))
a + geom_dotplot()
a + geom_freqpoly()
# b + geom_freqpoly(aes(y=..density..))
a + geom_histogram(binwidth=5)
Discreta
c <- ggplot(mpg,aes(fl))
c + geom_bar()
a + geom_bar()
Dos variables
d <- ggplot(mpg,aes(cty,hwy))
d + geom_blank()
d + geom_jitter()
d + geom_point()
d + geom_rug(sides="bl")
d + geom_text(aes(label=cty))
Una discreta y una continua
e <- ggplot(mpg,aes(class,hwy))
e + geom_bar(stat="identity")
e + geom_boxplot()
e + geom_dotplot(binaxis="y",stackdir="center")
e + geom_violin(scale="area")
Dos discretas
head(diamonds)
## carat cut color clarity depth table price x y z
## 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
## 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
## 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
## 4 0.29 Premium I VS2 62.4 58 334 4.20 4.23 2.63
## 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75
## 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48
f <- ggplot(diamonds,aes(cut,color))
f + geom_jitter()
Distribución continua bivariada
head(movies)
## title year length budget rating votes r1 r2 r3
## 1 $ 1971 121 NA 6.4 348 4.5 4.5 4.5
## 2 $1000 a Touchdown 1939 71 NA 6.0 20 0.0 14.5 4.5
## 3 $21 a Day Once a Month 1941 7 NA 8.2 5 0.0 0.0 0.0
## 4 $40,000 1996 70 NA 8.2 6 14.5 0.0 0.0
## 5 $50,000 Climax Show, The 1975 71 NA 3.4 17 24.5 4.5 0.0
## 6 $pent 2000 91 NA 4.3 45 4.5 4.5 4.5
## r4 r5 r6 r7 r8 r9 r10 mpaa Action Animation Comedy Drama
## 1 4.5 14.5 24.5 24.5 14.5 4.5 4.5 0 0 1 1
## 2 24.5 14.5 14.5 14.5 4.5 4.5 14.5 0 0 1 0
## 3 0.0 0.0 24.5 0.0 44.5 24.5 24.5 0 1 0 0
## 4 0.0 0.0 0.0 0.0 0.0 34.5 45.5 0 0 1 0
## 5 14.5 14.5 4.5 0.0 0.0 0.0 24.5 0 0 0 0
## 6 14.5 14.5 14.5 4.5 4.5 14.5 14.5 0 0 0 1
## Documentary Romance Short
## 1 0 0 0
## 2 0 0 0
## 3 0 0 1
## 4 0 0 0
## 5 0 0 0
## 6 0 0 0
g <- ggplot(movies,aes(year,rating))
g + geom_bin2d(binwidth=c(5,0.5))
g + geom_density2d()
Función continua
head(economics)
## date pce pop psavert uempmed unemploy
## 1 1967-06-30 507.8 198712 9.8 4.5 2944
## 2 1967-07-31 510.9 198911 9.8 4.7 2945
## 3 1967-08-31 516.7 199113 9.0 4.6 2958
## 4 1967-09-30 513.3 199311 9.8 4.9 3143
## 5 1967-10-31 518.5 199498 9.7 4.7 3066
## 6 1967-11-30 526.2 199657 9.4 4.8 3018
h <- ggplot(economics,aes(date,unemploy))
h + geom_area()
h + geom_line()
h + geom_step(direction="hv") +
theme(panel.background = element_rect(colour="pink"))