ggplot2

Mostrar datos vs mostrar información.

  • stats: Qué mostrar
  • geoms: Como mostrar

Geoms

geom_abline geom_density2d geom_line geom_ribbon
geom_area geom_density_2d geom_linerange geom_rug
geom_bar geom_dotplot geom_map geom_segment
geom_bin2d geom_errorbar geom_path geom_smooth
geom_blank geom_errorbarh geom_point geom_spoke
geom_boxplot geom_freqpoly geom_pointrange geom_step
geom_contour geom_hex geom_polygon geom_text
geom_count geom_histogram geom_qq geom_tile
geom_crossbar geom_hline geom_quantile geom_violin
geom_curve geom_jitter geom_raster geom_vline
geom_density geom_label geom_rect

Stats

stat_bin stat_contour stat_ellipse stat_spoke stat_summary_hex
stat_bin2d stat_count stat_function stat_sum stat_unique
stat_bin_2d stat_density stat_identity stat_summary stat_ydensity
stat_binhex stat_density2d stat_qq stat_summary2d
stat_bin_hex stat_density_2d stat_quantile stat_summary_2d
stat_boxplot stat_ecdf stat_smooth stat_summary_bin

geom_violin (stat_ydensty)

ggplot(mtcars) + aes(y = mpg, x = factor(cyl)) + geom_violin()

geom_violin

geom_dotplot (stat_bindot)

ggplot(mtcars) + aes(x = factor(cyl), y = mpg) +
  geom_dotplot(binaxis = "y", stackdir = "center", binwidth = 1)

geom_dotplot

geom_boxplot (stat_boxplot)

ggplot(mtcars) + aes(factor(cyl), mpg) + geom_boxplot()

geom_boxplot

geom_bar (stat_count)

ggplot(mtcars) + aes(factor(cyl)) + geom_bar(stat = "count")

geom_bar_count

geom_bar (stat_identity)

df <- as.data.frame(table(factor(mtcars$cyl)))
ggplot(df) + aes(x = Var1, y = Freq) + geom_bar(stat = "identity")

geom_bar_identity

geom_point (stat_qq)

ggplot(iris) + aes(sample = Petal.Length) + geom_point(stat = "qq")

geom_point_qq

geom_point

ggplot(diamonds) + aes(price, depth) + geom_point()

geom_point

geom_bin2d (stat_bin2d)

ggplot(diamonds) + aes(price, depth) + geom_bin2d()

geom_bin2d

geom_bin2d (stat_summary2d)

ggplot(diamonds) + aes(price, depth, z = table) + geom_bin2d(stat = "summary2d")

summary2d

geom_hex (stat_binhex)

#install.packages("hexbin")
ggplot(diamonds) + aes(price, depth) + geom_hex(stat = "binhex")

geom_hex

geom_hex (stat_summary_hex)

#install.packages("hexbin")
ggplot(diamonds) + aes(price, depth, z = table) + stat_summary_hex()

stat_summary_hex

geom_points

ggplot(mpg) + aes(displ, hwy) + geom_point()

geom_points2

geom_points + geom_spoke

set.seed(43210)

ggplot(mpg) + aes(displ, hwy, angle = runif(234, 0, 2*pi)) + 
  geom_point() + geom_spoke(radius = 0.5)

geom_spoke

geom_jitter (the overploting problem)

set.seed(86420)
ggplot(mpg) + aes(displ, hwy) + geom_jitter()

geom_jitter

geom_quantile (stat_quantile)

ggplot(mpg) + aes(displ, hwy) + geom_point() + geom_quantile()

geom_quantile

geom_smooth (stat_smooth)

ggplot(mpg) + aes(displ, hwy) + geom_point() + geom_smooth()

geom_smooth

geom_density2d (stat_density2d)

set.seed(98765)
ggplot(mpg) + aes(displ, hwy) + geom_jitter() + geom_density2d()

geom_density2d

geom_path

ggplot(mpg) + aes(displ, hwy) + geom_point() + geom_path()

geom_path

geom_rug

ggplot(mpg) + aes(displ, hwy) + geom_point() + geom_rug()

geom_rug

geom_path (stat_ellipse)

ggplot(mpg) + aes(displ, hwy) + geom_point() + geom_path(stat = "ellipse")

geom_points_ellipse

geom_point (stat_unique)

ggplot(mpg) + aes(displ, hwy) + geom_point(stat = "unique")

geom_points_unique

Gráficos de rangos

  • stat_summary
  • stat_sum_df
  • stat_sum_single

Gráficos de rangos

base-boxplot

geom_crossbar

ggplot(intervalos) + aes(x = names, ymin = q1, ymax = q3, y = med) +
  geom_crossbar()

geom_crossbar

geom_linerange

ggplot(intervalos) + aes(x = names, ymin = q1, ymax = q3, y = med) +
  geom_linerange()

geom_linerange

geom_pointrange

ggplot(intervalos) + aes(x = names, ymin = q1, ymax = q3, y = med) +
  geom_pointrange()

geom_pointrange

geom_errorbar

ggplot(intervalos) + aes(x = names, ymin = q1, ymax = q3, y = med) +
  geom_errorbar()

geom_errorbar

geom_errorbarh

ggplot(intervalos) + aes(y = names, xmin = q1, xmax = q3, x = med) +
  geom_errorbarh()

geom_errorbarh

geom_ribbon

ggplot(intervalos) + aes(x = 1:7, ymin = q1, ymax = q3, y = med) +
  geom_ribbon(alpha = I(1/3)) + geom_pointrange()

geom_ribbon

geom_area

intervalos %>% select(-names) %>% gather(stat, value) %>%
  ggplot + aes(y = value, x = rep(1:7, 5), fill= stat) + geom_area()

geom_area

geom_line

intervalos %>% select(-names) %>% gather(stat, value) %>%
  ggplot + aes(y = value, x = rep(1:7, 5), colour= stat) + geom_line()

geom_line

geom_density (stat_density)

ggplot(mtcars) + aes(x = mpg) + geom_density()

geom_density

geom_density (stat_density + stat_function)

ggplot(mtcars) + aes(x = mpg) + geom_density() +
  stat_function(fun = dnorm, colour = "red", 
                arg = list(mean = mean(mtcars$mpg), sd = sd(mtcars$mpg)))

geom_density_function

geom_freqpoly (stat_bin)

ggplot(mtcars) + aes(x = mpg) + geom_freqpoly(binwidth = 2)

geom_freqpoly

geom_histogram (stat_bin)

ggplot(mtcars) + aes(x = mpg) + geom_histogram(binwidth = 2)

geom_histogram

geom_abline (stat_abline)

ggplot() +
  scale_x_continuous(name = "x", limits = c(0,5)) +
  scale_y_continuous(name = "y", limits = c(0,10)) +
  scale_linetype(name = "s") +
  geom_abline(data.example,
              mapping = aes(slope = vx, intercept = vy, linetype = factor(x))) +
  geom_hline(data = data.example, mapping = aes(yintercept = vy)) +
  geom_vline(data = data.example, mapping = aes(xintercept = vy))

geom_abline (stat_abline)

geom_blank

ggplot(mtcars) + aes(x = wt, y = mpg) + geom_point() +
  geom_abline(aes(intercept = a, slope = b), data = df1)

geom_blank

geom_blank

ggplot(mtcars) + aes(x = wt, y = mpg, colour = factor(cyl), size = hp) + 
  geom_blank() + geom_abline(aes(intercept = a, slope = b), data = df1)

geom_blank-a

geom_segment

ggplot(data.example) + aes(x = x, y = y, xend = x + vx, yend = y + vy) +
  geom_segment(arrow = arrow(), size = 2, color = "blue")

geom_segment

geom_rect

ggplot(data.example) + aes(xmin = x, ymin = y, xmax = x + vx, ymax = y + vy) +
  geom_rect(fill = "blue")

geom_rect

geom_step

data.frame(x = sort(rnorm(47))) %>% ggplot + aes(seq_along(x), x) + geom_step()

geom_step

geom_step (stat_edf)

data.frame(x = rnorm(100)) %>% ggplot + aes(x) + geom_step(stat = "ecdf")

geom_step_ecdf

geom_text

ggplot(mtcars) + aes(x = wt, y = mpg, label = rownames(mtcars)) + geom_text()

geom_text

geom_map

require("colmaps")
map_df <- fortify(departamentos)
ggplot(map_df, aes(map_id = id)) +
  geom_map(map = map_df, color = "white", size = 0.1) +
  expand_limits(x = map_df$long, y = map_df$lat) +
  coord_map()

geom_map

geom_map

require("colmaps")
colombia <- colmap(municipios) +
  theme(plot.background = element_rect(fill = "transparent", colour = "transparent"))

geom_map

geom_polygon

map_df %>% filter(id == 25) %>% ggplot + aes(x = long, y = lat) +
  geom_polygon() + coord_fixed()

geom_polygon

geom_tile

diamonds %$% table(color, clarity) %>% as.data.frame %>% ggplot +
  aes(x = color, y = clarity, fill = Freq) + geom_tile()

geom_tile

geom_raster

diamonds %$% table(color, clarity) %>% as.data.frame %>% ggplot +
  aes(x = color, y = clarity, fill = Freq) + geom_raster()

geom_raster

geom_countour

ggplot(volcano3d, aes(x, y, z = z)) +
  stat_contour(binwidth = 2, size = 0.5, colour = "grey50") +
  stat_contour(binwidth = 10, size = 1)

geom_countour

Faceting

iris %>% melt("Species") %>% ggplot + 
  aes(x = value) +
  geom_density() +
  xlab("Valores") + 
  ylab("Densidad") +
  facet_wrap(~variable, scale = "free")

Faceting

Faceting

iris %>% melt("Species") %>% ggplot + 
  aes(x = value) +
  geom_density() +
  xlab("Valores") + 
  ylab("Densidad") +
  facet_grid(Species ~ variable, scales = "free")

Faceting

The Mandelbrot Set - geom_tile

# Create a grid of complex numbers
c.points <- outer(seq(-2.5, 1, by = 0.007), 1i*seq(-1.5, 1.5, by = 0.007),'+')
z <- 0

for (k in 1:30) z <- z^2 + c.points # Iterations of fractal's formula

c.points <- data.frame(reshape2::melt(c.points))
colnames(c.points) <- c("r.id", "c.id", "point")

z.points <- data.frame(reshape2::melt(z))
colnames(z.points) <- c("r.id", "c.id", "z.point")

mandelbrot <- merge(c.points, z.points, by = c("r.id","c.id")) # Mandelbrot Set
mandelbrot <- subset(mandelbrot, is.finite(-abs(z.point)))

# Plotting only finite-module numbers
ggplot(mandelbrot) +
  aes(Re(point), Im(point), fill = exp(-abs(z.point))) +
  geom_tile() +
  theme(legend.position = "none", axis.title.x = element_blank(), 
        axis.title.y = element_blank(), 
        panel.background = element_rect(fill = "#132B43"),
        panel.grid = element_blank())

The Mandelbrot Set - geom_tile