Bubble Chart

Author

Francisco Zambrano

Published

July 1, 2023

Installation

Development version from GitHub:

GitHub: https://github.com/zpio/ggexplorer

Show/Hide code
remotes::install_github("zpio/ggexplorer")
Show/Hide code
library(ggexplorer)
library(dplyr)

Examples

Gapminder dataset

Show/Hide code
data <- gapminder::gapminder %>% 
  filter(year=="2002") %>% dplyr::select(-year)

data_bubble <- data %>%
  mutate(pop=pop/1000000) %>%
  arrange(desc(pop)) %>%
  mutate(country = factor(country, country))

data_bubble

Basic Chart

Show/Hide code
bubble_chart(
  data = data_bubble, 
  x = gdpPercap, 
  y = lifeExp, 
  size_var = pop, 
  fill_var = continent,
  title = "GDPpercap vs Life Exp"
)

Scale size

Show/Hide code
bubble_chart(
  data = data_bubble, 
  x = gdpPercap, 
  y = lifeExp, 
  size_var = pop, 
  fill_var = continent,
  title = "GDPpercap vs Life Exp",
  scale_size = c(1, 30)
)

Opacity

Show/Hide code
bubble_chart(
  data = data_bubble, 
  x = gdpPercap, 
  y = lifeExp, 
  size_var = pop, 
  fill_var = continent,
  title = "GDPpercap vs Life Exp",
  alpha = 0.5
)

Outline

Show/Hide code
bubble_chart(
  data = data_bubble, 
  x = gdpPercap, 
  y = lifeExp, 
  size_var = pop, 
  fill_var = continent,
  title = "GDPpercap vs Life Exp",
  stroke = 1.5
)

Legend Position

Show/Hide code
bubble_chart(
  data = data_bubble, 
  x = gdpPercap, 
  y = lifeExp, 
  size_var = pop, 
  fill_var = continent,
  title = "GDPpercap vs Life Exp",
  legend_position = "top"
)

Colour scales

Show/Hide code
bubble_chart(
  data = data_bubble, 
  x = gdpPercap, 
  y = lifeExp, 
  size_var = pop, 
  fill_var = continent,
  title = "GDPpercap vs Life Exp"
) +
  ggplot2::scale_fill_hue() +
  ggplot2::scale_color_hue()

Show/Hide code
pal <- c("deepskyblue4", "deeppink2", "seagreen4", "darkgoldenrod4", "brown4")

bubble_chart(
  data = data_bubble, 
  x = gdpPercap, 
  y = lifeExp, 
  size_var = pop, 
  fill_var = continent,
  title = "GDPpercap vs Life Exp"
) +
  ggplot2::scale_fill_manual(values = pal) +
  ggplot2::scale_color_manual(values = pal)

Axis transformation

Show/Hide code
bubble_chart(
  data = data_bubble, 
  x = gdpPercap, 
  y = lifeExp, 
  size_var = pop, 
  fill_var = continent,
  title = "GDPpercap vs Life Exp",
  trans_x = "log10"
) 

Facet

Show/Hide code
bubble_chart(
  data = data_bubble, 
  x = gdpPercap, 
  y = lifeExp, 
  size_var = pop, 
  fill_var = continent,
  title = "GDPpercap vs Life Exp",
  facet_var = continent
) 

Annotations

Show/Hide code
data_annotation <- data_bubble %>% 
  mutate(annotation = case_when(gdpPercap > 5000 & lifeExp < 60 ~ "yes",
                                lifeExp < 30 ~ "yes", gdpPercap > 40000 ~ "yes")
  ) %>% filter(annotation=="yes")

bubble_chart(
  data = data_bubble, 
  x = gdpPercap, 
  y = lifeExp, 
  size_var = pop, 
  fill_var = continent,
  title = "GDPpercap vs Life Exp"
) +
  ggrepel::geom_text_repel(
    data_annotation, mapping = ggplot2::aes(label = country), size=3
  )

Interactive

Show/Hide code
bubble_chart(
  data = data_bubble, 
  x = gdpPercap, 
  y = lifeExp, 
  size_var = pop, 
  fill_var = continent,
  title = "GDPpercap vs Life Exp",
  trans_x = "log10",
  interactive = TRUE
)