Visualizacion De Datos Usando Ggplot2

ADVERTISEMENT

Geoms
Visualización de Datos
-
Funciones geom se utilizan para visualizar resultados. Asigne variables a las propiedades estéticas del geom. Cada geom forma una capa.
usando ggplot2
Geométricas Elementales
Dos Variables
a <- ggplot(economics, aes(date, unemploy))
Guía Rápida
X Continua, Y Continua
Distribución Bivariada Continua
e <- ggplot(mpg, aes(cty, hwy))
h <- ggplot(diamonds, aes(carat, price))
b <- ggplot(seals, aes(x = long, y = lat))
h +
geom_bin2d(binwidth = c(0.25, 500))
a +
geom_blank()
e +
geom_label(aes(label = cty), nudge_x = 1,
A
C
nudge_y = 1, check_overlap = TRUE)
x, y, alpha, color, fill, linetype, size, weight
Geoms
(Bueno para expandir límites)
Data Visualization
-
Use a geom to represent data points, use the geom’s aesthetic properties to represent variables
B
x, y, label, alpha, angle, color, family, fontface,
b +
geom_curve(aes(yend = lat + 1,
Geoms
One Variable
Two Variables
-
hjust, lineheight, size, vjust
Data Visualization
with ggplot2
h +
geom_density2d()
Use a geom to represent data points, use the geom’s aesthetic properties to represent variables
xend=long+1,curvature=z) ) - x, xend, y, yend,
Continuous
Continuous X, Continuous Y
Continuous Bivariate Distribution
e +
geom_jitter(height = 2, width = 2)
Cheat Sheet
x, y, alpha, colour, group, linetype, size
Conceptos Básicos
One Variable
f <- ggplot(mpg, aes(cty, hwy))
Two Variables
h <- ggplot(movies, aes(year, rating))
with ggplot2
alpha, angle, color, curvature, linetype, size
a <- ggplot(mpg, aes(hwy))
x, y, alpha, color, fill, shape, size
f +
geom_blank()
h +
geom_bin2d(binwidth = c(5, 0.5))
Continuous
Continuous X, Continuous Y
Continuous Bivariate Distribution
a +
geom_area(stat = "bin")
Cheat Sheet
a +
geom_path(lineend="butt",
h +
geom_hex()
f <- ggplot(mpg, aes(cty, hwy))
xmax, xmin, ymax, ymin, alpha, color, fill,
h <- ggplot(movies, aes(year, rating))
ggplot2 se basa en la idea que cualquier gráfica se
a <- ggplot(mpg, aes(hwy))
x, y, alpha, color, fill, linetype, size
linetype, size, weight
e +
geom_point()
linejoin="round’ , linemitre=1)
h +
geom_bin2d(binwidth = c(5, 0.5))
f +
geom_blank()
x, y, alpha, colour, fill, size
b + geom_area(aes(y = ..density..), stat = "bin")
h +
geom_density2d()
a +
geom_area(stat = "bin")
f +
geom_jitter()
puede construir usando estos tres componentes: datos,
a +
geom_density(kernal = "gaussian")
xmax, xmin, ymax, ymin, alpha, color, fill,
x, y, alpha, color, fill, shape, size, stroke
x, y, alpha, color, group, linetype, size
x, y, alpha, color, fill, shape, size
x, y, alpha, colour, linetype, size
Basics
x, y, alpha, color, fill, linetype, size
coordenadas y objetos geométricos (geoms). Este
linetype, size, weight
x, y, alpha, color, fill, linetype, size, weight
b + geom_area(aes(y = ..density..), stat = "bin")
f +
geom_jitter()
h +
geom_density2d()
b + geom_density(aes(y = ..county..))
h +
geom_hex()
f +
geom_point()
concepto se llama: gramática de las gráficas.
Función Continua
a +
geom_density(kernal = "gaussian")
a +
geom_polygon(aes(group = group))
e +
geom_quantile()
a+
geom_dotplot()
x, y, alpha, color, fill, shape, size
x, y, alpha, colour, linetype, size
x, y, alpha, color, fill, shape, size
x, y, alpha, colour, fill size
Basics
i <- ggplot(economics, aes(date, unemploy))
x, y, alpha, color, fill, linetype, size, weight
x, y, alpha, color, fill
x, y, alpha, color, fill, group, linetype, size
x, y, alpha, color, group, linetype, size, weight
F M A
b + geom_density(aes(y = ..county..))
f +
geom_point()
h +
geom_hex()
4
f +
geom_quantile()
Continuous Function
4
i +
geom_area()
a+
geom_dotplot()
g <- ggplot(economics, aes(date, unemploy))
x, y, alpha, color, fill, shape, size
x, y, alpha, colour, fill size
3
3
x, y, alpha, color, linetype, size, weight
a +
geom_freqpoly()
b +
geom_rect(aes(xmin = long, ymin=lat,
+
=
x, y, alpha, color, fill
2
2
g +
e +
geom_area()
geom_rug(sides = "bl")
x, y, alpha, color, linetype, size
x, y, alpha, color, fill, linetype, size
)
F M A
xmax= long + 1, ymax = lat + 1)
- xmax, xmin,
f +
geom_rug(sides = "bl")
4
1
4
f +
geom_quantile()
Continuous Function
1
x, y, alpha, color, fill, linetype, size
b + geom_freqpoly(aes(y = ..density..))
g <- ggplot(economics, aes(date, unemploy))
x, y, alpha, color, linetype, size
0
0
3
ymin, alpha, color, fill, linetype, size
alpha, color, linetype, size
3
ymax,
x, y, alpha, color, linetype, size, weight
a +
geom_freqpoly()
a +
geom_histogram(binwidth = 5)
0
1
2
3
4
0
1
2
3
4
data
geom
+
coordinate
=
plot
g +
geom_line()
2
2
i +
geom_line()
g +
geom_area()
x, y, alpha, color, fill, linetype, size, weight
x, y, alpha, color, linetype, size
system
x = F
f +
geom_smooth(model = lm)
f +
geom_rug(sides = "bl")
1
x, y, alpha, color, linetype, size
y = A
1
a +
geom_ribbon(
aes(ymin=unemploy - 900,
x, y, alpha, color, fill, linetype, size
b + geom_histogram(aes(y = ..density..))
b + geom_freqpoly(aes(y = ..density..))
e +
geom_smooth(method = lm)
x, y, alpha, color, group, linetype, size
x, y, alpha, color, fill, linetype, size, weight
Para visualizar resultados, asigne variables a las
0
alpha, color, linetype, size
0
a +
geom_histogram(binwidth = 5)
0
1
)
0
1
2
3
4
2
3
4
ymax=unemploy + 900)
- x, ymax, ymin
Discrete
g +
geom_step(direction = "hv")
data
geom
coordinate
plot
g +
geom_line()
x, y, alpha, color, fill, group, linetype, size, weight
propiedades visuales, o estéticas, como tamaño,
x, y, alpha, color, fill, linetype, size, weight
f +
geom_text(aes(label = cty))
system
x = F
a <- ggplot(mpg, aes(fl))
alpha, color, fill, group, linetype, size
f +
geom_smooth(model = lm)
x, y, alpha, color, linetype, size
F M A
x, y, alpha, color, linetype, size
y = A
i +
geom_step(direction = "hv")
4
4
b + geom_histogram(aes(y = ..density..))
x, y, label, alpha, angle, color, family, fontface,
color y posición x ó y.
b +
geom_bar()
x, y, alpha, color, fill, linetype, size, weight
hjust, lineheight, size, vjust
3
3
e +
geom_text(aes(label = cty), nudge_x = 1,
C
Discrete
g +
Visualizing error
geom_step(direction = "hv")
x, y, alpha, color, group, linetype, size
+
=
x, alpha, color, fill, linetype, size, weight
Segmentos Lineares
2
2
df <- data.frame(grp = c("A", "B"), fit = 4:5, se = 1:2)
f +
geom_text(aes(label = cty))
a <- ggplot(mpg, aes(fl))
A B
nudge_y = 1, check_overlap = TRUE)
x, y, alpha, color, linetype, size
1
1
F M A
propiedades básicas:
x, y, alpha, color, linetype, size
4
e <- ggplot(df, aes(grp, fit, ymin = fit-se, ymax = fit+se))
4
x, y, label, alpha, angle, color, family, fontface,
0
Discrete X, Continuous Y
0
b +
geom_bar()
x, y, label, alpha, angle, color, family, fontface,
0
1
0
1
3
4
hjust, lineheight, size, vjust
3
2
3
4
3
2
Graphical Primitives
g <- ggplot(mpg, aes(class, hwy))
data
geom
coordinate
plot
Visualizando el Error
+
=
e +
geom_crossbar(fatten = 2)
Visualizing error
x, alpha, color, fill, linetype, size, weight
system
hjust, lineheight, size, vjust
2
2
b +
geom_abline(
)
aes(intercept=0, slope=1)
x = F
df <- data.frame(grp = c("A", "B"), fit = 4:5, se = 1:2)
x, y, ymax, ymin, alpha, color, fill, linetype,
y = A
g +
geom_bar(stat = "identity")
df <- data.frame(grp = c("A", "B"), fit = 4:5, se = 1:2)
1
1
color = F
c <- ggplot(map, aes(long, lat))
e <- ggplot(df, aes(grp, fit, ymin = fit-se, ymax = fit+se))
size
b +
geom_hline(
)
size = A
aes(yintercept = lat)
x, y, alpha, color, fill, linetype, size, weight
Discrete X, Continuous Y
0
0
e +
geom_errorbar()
j <- ggplot(df, aes(grp, fit, ymin = fit-se, ymax = fit+se))
0
1
2
3
4
0
1
2
3
4
c +
geom_polygon(aes(group = group))
Graphical Primitives
g <- ggplot(mpg, aes(class, hwy))
data
geom
coordinate
plot
b +
geom_vline(
)
e +
X Discreta, Y Continua
geom_crossbar(fatten = 2)
aes(xintercept = long)
g +
geom_boxplot()
system
x, ymax, ymin, alpha, color, linetype, size,
x, y, alpha, color, fill, linetype, size
x = F
width (also geom_errorbarh())
f <- ggplot(mpg, aes(class, hwy))
x, y, ymax, ymin, alpha, color, fill, linetype,
y = A
g +
geom_bar(stat = "identity")
lower, middle, upper, x, ymax, ymin, alpha,
b +
geom_segment(
xend=long+1))
aes(yend=lat+1,
color = F
j +
geom_crossbar(fatten = 2)
c <- ggplot(map, aes(long, lat))
size
color, fill, linetype, shape, size, weight
e +
geom_linerange()
size = A
x, y, alpha, color, fill, linetype, size, weight
Para construir una gráfica complete este patrón
b +
geom_spoke(
)
e +
geom_errorbar()
c +
geom_polygon(aes(group = group))
aes(angle = 1:1155, radius = 1)
g +
geom_dotplot(binaxis = "y",
x, ymin, ymax, alpha, color, linetype, size
x, y, ymax, ymin, alpha, color, fill, group,
g <- ggplot(economics, aes(date, unemploy))
f +
geom_col()
g +
geom_boxplot()
x, ymax, ymin, alpha, color, linetype, size,
x, y, alpha, color, fill, linetype, size
stackdir = "center")
linetype, size
e +
geom_pointrange()
g +
geom_path(lineend="butt",
x, y, alpha, color, fill
width (also geom_errorbarh())
Requerido
lower, middle, upper, x, ymax, ymin, alpha,
x, y, alpha, color, fill, group, linetype, size
ggplot(data = <DATA >
<DATOS>
) +
linejoin="round’ , linemitre=1)
Una Variable
x, y, ymin, ymax, alpha, color, fill, linetype,
g +
geom_violin(scale = "area")
e +
geom_linerange()
color, fill, linetype, shape, size, weight
j +
geom_errorbar()
shape, size
x, y, alpha, color, linetype, size
x, y, alpha, color, fill, linetype, size, weight
g +
geom_dotplot(binaxis = "y",
x, ymin, ymax, alpha, color, linetype, size
<GEOM_FUNCTION> (
<FUNCION_GEOM>
g <- ggplot(economics, aes(date, unemploy))
x, ymax, ymin, alpha, color, group, linetype,
g +
geom_ribbon(
F M A
aes(ymin=unemploy - 900,
f +
geom_boxplot()
stackdir = "center")
Maps
4
4
Continua
)
size, width (also geom_errorbarh())
mapping =aes( <MAPPINGS>
<ESTETICAS>
),
ymax=unemploy + 900)
e +
geom_pointrange()
g +
geom_path(lineend="butt",
data <- data.frame(murder = USArrests$Murder,
x, y, alpha, color, fill
3
3
x, ymax, ymin, alpha, color, fill, linetype, size
state = tolower(rownames(USArrests)))
x, y, lower, middle, upper, ymax, ymin, alpha,
c <- ggplot(mpg, aes(hwy)); c2 <- ggplot(mpg)
+
=
linejoin="round’ , linemitre=1)
Discrete X, Discrete Y
x, y, ymin, ymax, alpha, color, fill, linetype,
g +
geom_violin(scale = "area")
j +
geom_linerange()
2
2
stat = <STAT> ,
<STAT>
No
map <- map_data("state")
h <- ggplot(diamonds, aes(cut, color))
color, fill, group, linetype, shape, size, weight
shape, size
x, y, alpha, color, linetype, size
e <- ggplot(data, aes(fill = murder))
1
1
x, y, alpha, color, fill, linetype, size, weight
x, ymin, ymax, alpha, color, group, linetype, size
position = <POSITION>
Requerido,
<POSICION>
e +
geom_map(
) +
0
0
g +
geom_ribbon(
aes(ymin=unemploy - 900,
F M A
d<- ggplot(seals, aes(x = long, y = lat))
aes(map_id = state), map = map
c +
geom_area(stat = "bin")
h +
geom_jitter()
f +
geom_dotplot(binaxis = "y",
4
Maps
4
0
1
3
4
0
1
3
4
2
2
data
geom
coordinate
plot
)
expand_limits(
)
ymax=unemploy + 900)
se proveen
data <- data.frame(murder = USArrests$Murder,
x = map$long, y = map$lat
)
+
x, y, alpha, color, fill, shape, size
3
system
3
d +
geom_segment(aes(
x = F
x, ymax, ymin, alpha, color, fill, linetype, size
state = tolower(rownames(USArrests)))
x, y, alpha, color, fill, linetype, size
map_id, alpha, color, fill, linetype, size
stackdir = "center")
+
=
y = A
Discrete X, Discrete Y
j +
geom_pointrange()
2
2
valores
xend = long + delta_long,
map <- map_data("state")
<FUNCION_COORDINADAS>
<COORDINATE_FUNCTION> +
h <- ggplot(diamonds, aes(cut, color))
yend = lat + delta_lat))
e <- ggplot(data, aes(fill = murder))
x, y, alpha, color, fill, group
1
1
Three Variables
x, y, ymin, ymax, alpha, color, fill, group,
iniciales
c +
geom_density(kernel = "gaussian")
<FACET_FUNCTION> +
<FUNCION_FACETA>
x, xend, y, yend, alpha, color, linetype, size
e +
geom_map(
) +
0
0
aes(map_id = state), map = map
d<- ggplot(seals, aes(x = long, y = lat))
h +
geom_jitter()
f +
geom_violin(scale = "area")
linetype, shape, size
0
1
2
3
4
0
1
2
3
4
i +
geom_raster(aes(fill = z), hjust=0.5,
seals$z <- with(seals, sqrt(delta_long^2 + delta_lat^2))
data
geom
coordinate
plot
expand_limits(
)
x = map$long, y = map$lat
d +
geom_rect(aes(xmin = long, ymin = lat,
x, y, alpha, color, fill, group, linetype, size, weight
x, y, alpha, color, fill, shape, size
<SCALE_FUNCTION> +
<FUNCION_ESCALA>
system
d +
geom_segment(aes(
i <- ggplot(seals, aes(long, lat))
vjust=0.5, interpolate=FALSE)
x = F
map_id, alpha, color, fill, linetype, size
xmax= long + delta_long,
x, y, alpha, color, fill, group, linetype, size,
y = A
xend = long + delta_long,
x, y, alpha, fill
ymax = lat + delta_lat))
<THEME_FUNCTION>
<FUNCION_TEMA>
weight
i +
geom_contour(aes(z = z))
yend = lat + delta_lat))
Three Variables
i +
geom_tile(aes(fill = z))
c +
geom_dotplot()
xmax, xmin, ymax, ymin, alpha, color, fill,
Mapas
x, y, z, alpha, colour, linetype, size, weight
x, xend, y, yend, alpha, color, linetype, size
x, y, alpha, color, fill, linetype, size
linetype, size
data <- data.frame(murder = USArrests$Murder,
i +
geom_raster(aes(fill = z), hjust=0.5,
seals$z <- with(seals, sqrt(delta_long^2 + delta_lat^2))
x, y, alpha, color, fill
ggplot(data = mpg, aes(x = cty, y = hwy))
d +
geom_rect(aes(xmin = long, ymin = lat,
state = tolower(rownames(USArrests)))
vjust=0.5, interpolate=FALSE)
i <- ggplot(seals, aes(long, lat))
X Discreta, Y Discreta
xmax= long + delta_long,
RStudio® is a trademark of RStudio, Inc. •
CC BY
RStudio • • 844-448-1212 •
Learn more at • ggplot2 0.9.3.1 • Updated: 3/15
map <- map_data("state")
x, y, alpha, fill
Este comando comienza una gráfica, complétela
ymax = lat + delta_lat))
c +
geom_freqpoly()
g <- ggplot(diamonds, aes(cut, color))
i +
geom_contour(aes(z = z))
i +
geom_tile(aes(fill = z))
k <- ggplot(data, aes(fill = murder))
mediante agregando capas, un geom por capa.
xmax, xmin, ymax, ymin, alpha, color, fill,
x, y, alpha, color, group, linetype, size
x, y, z, alpha, colour, linetype, size, weight
x, y, alpha, color, fill, linetype, size
linetype, size
k +
geom_map(
) +
aes(map_id = state), map = map
g +
geom_count()
datos
estéticas
geom
expand_limits(
)
c +
geom_histogram(binwidth = 5)
x = map$long, y = map$lat
RStudio® is a trademark of RStudio, Inc. •
CC BY
RStudio • • 844-448-1212 •
Learn more at • ggplot2 0.9.3.1 • Updated: 3/15
x, y, alpha, color, fill, shape, size, stroke
qplot(x = cty, y = hwy, data = mpg, geom = "point")
map_id, alpha, color, fill, linetype, size
x, y, alpha, color, fill, linetype, size, weight
Este comando es una gráfica completa, tiene datos, las
Trés Variables
Argumentos
c2 +
geom_qq(aes(sample = hwy))
estéticas están asignadas y por lo menos un geom.
x, y, alpha, color, fill, linetype, size, weight
seals$z <- with(seals, sqrt(delta_long^2
l +
geom_raster(aes(fill = z),
last_plot()
Traducción de argumentos comunes
+ delta_lat^2))
hjust=0.5, vjust=0.5,
label = etiqueta, angle=ángulo
Devuelve la última gráfica
Discreta
interpolate=FALSE)
l <- ggplot(seals, aes(long, lat))
size=tamaño, weight=peso
d <- ggplot(mpg, aes(fl))
alpha=transparencia
x, y, alpha, fill
ggsave("plot.png", width = 5, height = 5)
l +
geom_contour(aes(z = z))
fontface=tipo de letra
l +
geom_tile(aes(fill = z))
d +
geom_bar()
La última gráfica es grabada como una imagen de 5 por
hjust=ajuste horizontal
x, y, z, alpha, colour, group,
x, y, alpha, color, fill, linetype, size,
5 pulgs., usa el mismo tipo de archivo que la extensión
lineheight=grosor de línea
x, alpha, color, fill, linetype, size, weight
linetype, size, weight
width
RStudio® es una marca registrada de RStudio, Inc. •
CC BY
RStudio • • 844-448-1212 •
Para aprender más vaya a y • ggplot2 2.1.0 • Actualizado: 12/16

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go
Page of 2