Bubble Chart
Installation
Development version from GitHub:
GitHub: https://github.com/zpio/ggexplorer
Show/Hide code
::install_github("zpio/ggexplorer") remotes
Show/Hide code
library(ggexplorer)
library(dplyr)
Examples
Gapminder dataset
Show/Hide code
<- gapminder::gapminder %>%
data filter(year=="2002") %>% dplyr::select(-year)
<- data %>%
data_bubble 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"
+
) ::scale_fill_hue() +
ggplot2::scale_color_hue() ggplot2
Show/Hide code
<- c("deepskyblue4", "deeppink2", "seagreen4", "darkgoldenrod4", "brown4")
pal
bubble_chart(
data = data_bubble,
x = gdpPercap,
y = lifeExp,
size_var = pop,
fill_var = continent,
title = "GDPpercap vs Life Exp"
+
) ::scale_fill_manual(values = pal) +
ggplot2::scale_color_manual(values = pal) ggplot2
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_bubble %>%
data_annotation mutate(annotation = case_when(gdpPercap > 5000 & lifeExp < 60 ~ "yes",
< 30 ~ "yes", gdpPercap > 40000 ~ "yes")
lifeExp %>% filter(annotation=="yes")
)
bubble_chart(
data = data_bubble,
x = gdpPercap,
y = lifeExp,
size_var = pop,
fill_var = continent,
title = "GDPpercap vs Life Exp"
+
) ::geom_text_repel(
ggrepelmapping = ggplot2::aes(label = country), size=3
data_annotation, )
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
)