How to use ggforce(Visualization) Package in R

 library(tidyverse)

library(ggforce)

library(nycflights13)

p <- airports %>%

  filter(lon < 0, tzone != "\\N") %>%

  ggplot(aes(lon, lat, color = tzone)) + 

  geom_point(show.legend = FALSE)  


p

p +

  geom_mark_rect() 

p + 

  geom_mark_rect(aes(label = tzone)) 

p + 

  geom_mark_rect(aes(label = tzone), show.legend = FALSE) +

  theme_void() 

p + 

  geom_mark_hull(aes(label = tzone)) +

  theme_void() 

p + 

  geom_mark_hull(aes(label = tzone, fill = tzone), show.legend = FALSE) +

  theme_void() 


p + 

  geom_mark_hull(aes(label = tzone, fill = tzone), show.legend = FALSE, expand = unit(3, "mm")) +

  theme_void() 

p + 

  geom_mark_hull(aes(label = tzone, fill = tzone), show.legend = FALSE, expand = unit(3, "mm")) +

  theme_no_axes() 

p +

  facet_zoom(xlim = c(-155, -160.5), ylim = c(19, 22.3))

p +

  geom_mark_hull(aes(label = tzone, fill = tzone), show.legend = FALSE, expand = unit(3, "mm")) +

  theme_no_axes() +

  facet_zoom(x = tzone == "America/Los_Angeles")

p +

  geom_mark_hull(aes(fill = tzone), expand = unit(3, "mm")) +

  coord_cartesian(xlim = c(-130, -180), ylim = c(50, 75))  +

  geom_voronoi_segment()


prep_planes <- planes %>%

  filter(year > 1998, year < 2005) %>%

  filter(engine != "Turbo-shaft") %>%

  select(manufacturer, engine) %>%

  head(500)


prep_planes %>%

  gather_set_data(1:2) %>%

  ggplot(aes(x, id = id, split = y, value = 1))  +

  geom_parallel_sets(aes(fill = engine)) 


prep_planes %>%

  gather_set_data(1:2) %>%

  ggplot(aes(x, id = id, split = y, value = 1))  +

  geom_parallel_sets(aes(fill = engine)) +

  geom_parallel_sets_axes(axis.width = 0.1) +

  geom_parallel_sets_labels()


prep_planes %>%

  gather_set_data(1:2) %>%

  ggplot(aes(x, id = id, split = y, value = 1))  +

  geom_parallel_sets(aes(fill = engine), show.legend = FALSE, alpha = 0.3) +

  geom_parallel_sets_axes(axis.width = 0.1, color = "lightgrey", fill = "white") +

  geom_parallel_sets_labels(angle = 0) +

  theme_no_axes()


planes %>%

  count(engine) 


planes %>%

  count(engine) %>%

  ggplot() +

  geom_arc_bar(aes(x0 = 0, y0 = 0, r0 = 0.7, r = 1, amount = n, fill = engine), alpha = 0.3, stat = "pie") 


planes %>%

  count(engine) %>%

  mutate(focus = ifelse(engine == "Turbo-jet", 0.2, 0)) %>%

  ggplot() +

  geom_arc_bar(aes(x0 = 0, y0 = 0, r0 = 0.7, r = 1, amount = n, fill = engine, explode = focus), alpha = 0.3, stat = "pie") +

  theme_no_axes()


library(ggforce)

ids <- factor(c("1.1", "2.1", "1.2", "2.2", "1.3", "2.3"))

values <- data.frame(

  id = ids,

  value = c(3, 3.1, 3.1, 3.2, 3.15, 3.5)

)

positions <- data.frame(

  id = rep(ids, each = 4),

  x = c(2, 1, 1.1, 2.2, 1, 0, 0.3, 1.1, 2.2, 1.1, 1.2, 2.5, 1.1, 0.3,

        0.5, 1.2, 2.5, 1.2, 1.3, 2.7, 1.2, 0.5, 0.6, 1.3),

  y = c(-0.5, 0, 1, 0.5, 0, 0.5, 1.5, 1, 0.5, 1, 2.1, 1.7, 1, 1.5,

        2.2, 2.1, 1.7, 2.1, 3.2, 2.8, 2.1, 2.2, 3.3, 3.2)

)

datapoly <- merge(values, positions, by = c("id"))

# Standard look

ggplot(datapoly, aes(x = x, y = y)) +

  geom_polygon(aes(fill = value, group = id))


# Contracted and rounded

ggplot(datapoly, aes(x = x, y = y)) +

  geom_shape(aes(fill = value, group = id), 

             expand = unit(-2, 'mm'), radius = unit(5, 'mm'))


# Not an ordinary ellipse — a super-ellipse

ggplot() +

  geom_ellipse(aes(x0 = 0, y0 = 0, a = 6, b = 3, angle = -pi / 3, m1 = 3)) +

  coord_fixed()


# Create 6 random control points

controls <- data.frame(

  x = runif(6),

  y = runif(6)

)

ggplot(controls, aes(x, y)) +

  geom_polygon(fill = NA, colour = 'grey') +

  geom_point(colour = 'red') +

  geom_bspline_closed(alpha = 0.5)


ggplot() +

  geom_regon(aes(x0 = runif(8), y0 = runif(8), sides = sample(3:10, 8),

                 angle = 0, r = runif(8) / 10)) +

  coord_fixed()


data <- data.frame(

  x = c(1, 2, 2, 1, 2, 3, 3, 2),

  y = c(1, 2, 3, 2, 3, 1, 2, 5),

  group = c(1, 1, 1, 1, 2, 2, 2, 2)

)

ggplot(data) +

  geom_diagonal_wide(aes(x, y, group = group))

titanic <- reshape2::melt(Titanic)

# This is how we usually envision data for parallel sets

head(titanic)

titanic <- gather_set_data(titanic, 1:4)

head(titanic)

ggplot(titanic, aes(x, id = id, split = y, value = value)) +

  geom_parallel_sets(aes(fill = Sex), alpha = 0.3, axis.width = 0.1) +

  geom_parallel_sets_axes(axis.width = 0.1) +

  geom_parallel_sets_labels(colour = 'white')


ggplot(iris, aes(Petal.Length, Petal.Width)) +

  geom_mark_ellipse(aes(fill = Species)) +

  geom_point()


ggplot(iris, aes(Petal.Length, Petal.Width)) +

  geom_mark_ellipse(aes(fill = Species, label = Species)) +

  geom_point()


desc <- 'This iris species has a markedly smaller petal than the others.'

ggplot(iris, aes(Petal.Length, Petal.Width)) +

  geom_mark_ellipse(aes(filter = Species == 'setosa', label = 'Setosa', 

                        description = desc)) +  geom_point()


ggplot(iris, aes(Sepal.Length, Sepal.Width)) +

  geom_delaunay_tile(alpha = 0.3) + 

  geom_delaunay_segment2(aes(colour = Species, group = -1), size = 2,

                         lineend = 'round')


ggplot(iris, aes(Sepal.Length, Sepal.Width)) + 

  geom_voronoi_tile(aes(fill = Species, group = -1L)) + 

  geom_voronoi_segment() +

  geom_point()


ggplot(iris, aes(Sepal.Length, Sepal.Width)) + 

  geom_voronoi_tile(aes(fill = Species), colour = 'black')


ggplot(iris, aes(Sepal.Length, Sepal.Width)) + 

  geom_voronoi_tile(aes(fill = Species, group = -1L), max.radius = 0.2,

                    colour = 'black')


ggplot(iris, aes(Sepal.Length, Sepal.Width)) + 

  geom_voronoi_tile(aes(fill = Species, group = -1L), max.radius = 1,

                    colour = 'black', expand = unit(-0.5, 'mm'), 

                    radius = unit(0.5, 'mm'), show.legend = FALSE)




Comments

Popular posts from this blog

How to create Animated 3d chart with R.

Linux/Unix Commands frequently used

R Programming Introduction