Display name
Display name

Reputation: 4501

Nested Pareto charts in ggplot

matryoshka pareto plot

I often generate pareto charts that remind me of matryoshka dolls (those Russian nested dolls).

  1. [Top Graph] - The first bar of my first pareto I'll highlight in blue. This is the largest "Count by Manufacturer" which happens to be Dodge automobiles.
  2. [Middle Graph] - This is an explosion of the first highlighted bar above. All bars are highlighted in blue to represent this fact.
  3. [Bottom Graph] - This is an explosion of the first highlighted bar in the middle graph. All bars are highlighted in blue and outlined in red to represent this fact.

If I can criticize my own work this is not visually pleasing, and more importantly, not intuitively easy to understand.

The solutins I get with R and ggplot2 tags tend to be brilliant and elegant (in my opinion). There's gotta be a better way. Do you have it?

library(tidyverse)
library(cowplot)

p1 <- mpg %>% 
  count(manufacturer) %>% 
  top_n(5) %>% 
  ggplot(aes(reorder(manufacturer, -n), n)) + 
  geom_col() + 
  geom_col(data = mpg %>% count(manufacturer) %>% top_n(1), fill = "blue") + 
  labs(x = NULL, y = NULL, title = "Count by Manufacturer")

p2 <- mpg %>% 
  filter(manufacturer == "dodge") %>% 
  count(cyl) %>% 
  ggplot(aes(reorder(cyl, -n), n)) + 
  geom_col(fill = "blue") + 
  geom_col(data = mpg %>% 
             filter(manufacturer == "dodge", cyl == 8) %>% 
             count(cyl),
           fill = "blue",
           color = "red",
           size = 2) + 
  labs(x = NULL, y = NULL, title = "Dodge cylinder count")

p3 <- mpg %>% 
  filter(manufacturer == "dodge", cyl == 8) %>% 
  count(cty) %>% 
  mutate(cyt = as.character(cty)) %>% 
  ggplot(aes(reorder(cty, -n), n)) + 
  geom_col(fill = "blue", color = "red", size = 2) + 
  labs(x = NULL, y = NULL, title = "Dodge V8 cty mpg count")

plot_grid(p1, p2, p3, ncol = 1)

Upvotes: 0

Views: 187

Answers (1)

MKa
MKa

Reputation: 2318

It involves little bit of fiddling but you could use the drilldown in highcharter. And this will give you interactivity.

library(tidyverse)
library(highcharter)
library(purrr)

# DATA for the 3 plots
d1 <- mpg %>% 
  count(manufacturer) %>% 
  top_n(5) %>%
  arrange(desc(n)) %>%
  mutate(drilldown = manufacturer)

d2 <- mpg %>% 
  filter(manufacturer == "dodge") %>% 
  count(cyl) %>% 
  mutate(cyl = as.character(cyl),
         # Required for the second drilldown
         drilldown = as.character(cyl),
         name = as.character(cyl), 
         y = n) %>% 
  arrange(desc(n))

d3 <- mpg %>% 
  filter(manufacturer == "dodge", cyl == 8) %>% 
  count(cty) %>% 
  mutate(cty = as.character(cty),
         cyt = as.character(cty)) %>%
  arrange(desc(n))

hc <- highchart() %>%
  hc_add_series(type = "column", data = d1, hcaes(y = n, x = manufacturer)) %>%
  hc_xAxis(type = "category")


hc2 <- hc %>%
  hc_drilldown(
    allowPointDrilldown = TRUE,
    series = list(
      list(id = "dodge", 
           data = purrr::transpose(d2),
           type = "column"),
      list(id = "8",
           data = list_parse2(d3),
      type = "column")))

hc2

Upvotes: 1

Related Questions