| title |
descriptionMeta |
descriptionTop |
sectionText |
sectionLink |
DataToVizText |
DataToVizLink |
url |
output |
Customized Circle packing with R and ggraph |
Learn how to use the ggraph R package to build customized circular packing with specific colors, sizes, labels and more. |
This page follows the previous [introduction](313-basic-circle-packing-with-several-levels.html) that explained the basis of circle packing with R and the ggraph library. It describes how to customize color, size, labels and more. |
Circle Packing section |
circle-packing.html |
Data to Viz |
data-to-viz.com/graph/circularpacking.html |
314-custom-circle-packing-with-several-levels |
| html_document |
| self_contained |
mathjax |
lib_dir |
template |
css |
toc |
toc_float |
toc_depth |
df_print |
false |
default |
libs |
template_rgg.html |
style.css |
true |
true |
2 |
paged |
|
|
```{r global options, include = FALSE}
knitr::opts_chunk$set( warning=FALSE, message=FALSE)
```
# Bubble size proportionnal to a variable
***
Mapping the bubble size to a numeric variable allows to add an additionnal layer of information to the chart.
Here, the `vertices` data frame has a `size` column that is used for the bubble size. Basically, it just needs to be passed to the `weight` argument of the `ggraph()` function.
```{r thecode2, echo=FALSE, out.width = "100%", fig.height=7}
# Libraries
library(ggraph)
library(igraph)
library(tidyverse)
library(viridis)
# We need a data frame giving a hierarchical structure. Let's consider the flare dataset:
edges
```{r thecode2, eval=FALSE}
```
# Map color to hierarchy depth
***
```{r thecode3a, echo=FALSE, out.width = "100%", fig.height=7}
# Left: color depends of depth
p
```{r thecode3b, echo=FALSE, out.width = "100%", fig.height=7}
# Adjust color palette: viridis
p + scale_fill_viridis()
```
```{r thecode3c, echo=FALSE, out.width = "100%", fig.height=7}
# Adjust color palette: colorBrewer
p + scale_fill_distiller(palette = "RdPu")
```
Adding color to circular packing definitely makes sense. The first option is to map color to depth: the origin of every node will have a color, the level 1 another one, and so on..
As usual, you can play with the colour palette to fit your needs. Here are 2 examples with the `viridis` and the `RColorBrewer` palettes:
```{r thecode3a, eval=FALSE}
```
```{r thecode3b, eval=FALSE}
```
```{r thecode3c, eval=FALSE}
```
# Map color to hierarchy depth
***
```{r thecode4a, echo=FALSE, out.width = "100%", fig.height=7}
# Create a subset of the dataset (I remove 1 level)
edges %
filter(to %in% from) %>%
droplevels()
vertices %
filter(name %in% c(edges$from, edges$to)) %>%
droplevels()
vertices$size
```{r thecode4b, echo=FALSE, out.width = "100%", fig.height=7}
# Right
ggraph(mygraph, layout = 'circlepack', weight="size" ) +
geom_node_circle(aes(fill = depth)) +
geom_node_label( aes(label=shortName, filter=leaf, size=size)) +
theme_void() +
theme(legend.position="FALSE") +
scale_fill_viridis()
```
To add more insight to the plot, we often need to add labels to the circles. However you can do it only if the number of circle is not to big. Note that you can use `geom_node_text` (left) or `geom_node_label` to annotate leaves of the circle packing:
```{r thecode4a, eval=FALSE}
```
```{r thecode4b, eval=FALSE}
```
```{r, echo=FALSE}
htmltools::includeHTML("htmlChunkRelatedPartOfWhole.html")
```