Same data, different charts

Author

Michiel Duvekot

This demonstrates that it is possible to create the five different charts from the same dataset as claimed in this post:

https://www.linkedin.com/posts/ebertini_same-data-different-charts-one-of-the-activity-7161365578916478977-bQpe

Load required libraries

library(ggplot2, quietly = TRUE)
library(dplyr, quietly = TRUE, warn.conflicts = FALSE)
library(ggforce, quietly = TRUE)
library(lemon, quietly = TRUE)

Create a dataframe

df <- data.frame(
  value = c(0.2, 0.4, 0.3, 0.1),
  colour = c("#4387ed", "#fdad36", "#4387ed", "#fdad36"),
  category = c("a", "a", "b", "b")
)

print(df)
  value  colour category
1   0.2 #4387ed        a
2   0.4 #fdad36        a
3   0.3 #4387ed        b
4   0.1 #fdad36        b

Aligned Measures

ggplot(df, aes(x = category, y = value, fill = colour))+
  scale_fill_identity()+
  geom_col(position = position_dodge2(width = .8))+
  theme_void()+
  labs(title = "Aligned Measures")+
  scale_y_continuous(expand = c(0,0))+
  theme(
    plot.margin = margin(12,12,12,12, "pt"),
    plot.title = element_text(hjust = .5,
                              margin = margin(0, 0, 12, 0, "pt")),
    axis.line.x.bottom = element_line(linewidth = 1)
  )

Aligned Categories

library(lemon)

ggplot(df, aes(x = 0, y = value, fill = colour))+
  scale_fill_identity()+
  geom_col(position = position_dodge2(width = .8, reverse = TRUE))+
  coord_flip()+
  facet_rep_wrap(~category, as.table = TRUE)+
  theme_void()+
  labs(title = "Aligned Categories")+
  scale_y_continuous(expand = c(0,0))+
  theme(
    aspect.ratio = 1/2,
    plot.margin = margin(12,12,12,12, "pt"),
    panel.spacing = unit(48, "pt"),
    plot.title = element_text(hjust = .5,
                              margin = margin(0, 0, 12, 0, "pt")),
    axis.line.y = element_line(linewidth = 1),
    strip.text.x = element_blank()
    
  )

Stacked

ggplot(df, aes(x = category, y = value, fill = colour))+
  scale_fill_identity()+
  geom_col(position = position_stack(reverse = TRUE))+
  theme_void()+
  labs(title = "Stacked")+
  scale_y_continuous(expand = c(0,0))+
  theme(
    aspect.ratio = 2/1,
    plot.margin = margin(12,12,12,12, "pt"),
    plot.title = element_text(hjust = .5,
                              margin = margin(0, 0, 12, 0, "pt")),
    axis.line.x = element_line(linewidth = 1)
  )

Dots

ggplot(df, aes(x = value, y = rev(category), colour = colour))+
  scale_color_identity()+
  geom_point(size = 10)+
  scale_x_continuous(limits = c(0,1))+
  theme_void()+
  labs(title = "Dots")+
  theme(
    panel.grid.major.y = element_line(linewidth = 1),
    plot.margin = margin(12,12,12,12, "pt"),
    plot.title = element_text(hjust = .5,
                              margin = margin(0, 0, 12, 0, "pt")),
  )

Slope

ggplot()+
  scale_colour_identity()+
  geom_point(data = df,  
             aes(x = category,  
                     y = value,  
                     colour = colour
                 ), 
             size = 10
             )+
  geom_segment(data = df |> 
  summarize(.by = colour, 
            x = first(category), xend = last(category),
            y = first(value), yend = last(value)
            ), 
               aes(x = x, 
                   xend = xend, 
                   y = y, 
                   yend = yend,
                   colour = colour
                   )
               ) +
  labs(title = "Slope")+
  scale_y_continuous(limits = c(0,1))+
  theme_void()+
  theme(
    plot.margin = margin(12,12,12,12, "pt"),
    plot.title = element_text(hjust = .5,
                              margin = margin(0, 0, 12, 0, "pt")),
   panel.grid.major.x = element_line(linewidth = 1)
  )

Bubbles

ggplot(df)+
  coord_fixed(clip = "off")+
  scale_fill_identity()+
  scale_colour_identity()+
  geom_circle(
    aes(
      r = sqrt(value)/2, 
      fill = colour, 
      colour = colour,
      x0 = as.integer(factor(colour)), 
      y0 = rev(as.integer(factor(category)))
    )
    )+
  labs(title = "Bubbles")+
  theme_void()+
  scale_x_continuous(limits = c(0.5,2.5), breaks = c(.5, 1.5, 2.5), expand = c(0,0))+
  scale_y_continuous(limits = c(.5, 2.5), breaks = c(.5, 1.5, 2.5), expand = c(0,0))+
  theme(
    plot.margin = margin(12,12,12,12, "pt"),
    panel.spacing = unit(2, "pt"),
    plot.title = element_text(hjust = .5,
                              margin = margin(0, 0, 12, 0, "pt")),
    panel.grid.major = element_line(linewidth = 1),
    legend.position = "none",
  )