I have 3000-element vector populated with mostly zeros (0) and intermixed values of one (1) throughout. I am attempting to visualize the degree of where the ones appear and their degree of sequential runs in the data.
I like the idea of a waffle chart with tiny squares with different colors denoting instances of 0 and 1. Is there a means to tweak a waffle chart to achieve this 2-colored, ordered data, stacked representation?
The code below provides a 200-element vector populated with mostly zeros as an example. A waffle-type chart with width = 20 and height = 10 is something along the lines of what I seek.
This solution is close to what I desire, except I need to retain the original order of the data in the visual.
Create waffle chart in ggplot2
library(tidyverse)
library(waffle)
dabble <- ifelse(runif(200) < 0.8, 0, 1 )
dabble
# [1] 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1
# [70] 1 0 0 0 0 0 0 0 0 1 1 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0
# [139] 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 1 0 0 1 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0 0
CodePudding user response:
You can do this in ggplot directly using geom_tile
and a little data shaping:
library(tidyverse)
dabble <- ifelse(runif(200) < 0.8, 0, 1 )
df <- data.frame(z = dabble, x = rep(1:20, 10), y = rep(10:1, each = 20))
ggplot(df, aes(x, y, fill = factor(z)))
geom_tile(color = "white", size = 2)
scale_fill_manual(values = c("lightblue", "red4"), name = NULL)
coord_equal()
theme_void()
Created on 2022-06-01 by the reprex package (v2.0.1)