I am making a unit/ symbol chart in R that should look like almost a waffle chart with spacing between each category. In my chart, each bar has a fixed number of rows and an arbitrary number of columns. My main issue is with a spacing between the bars. I could use increments to find starting points for each bar on X axis - see my code below - but this approach only works when each bar has fixed width. In my case, one bar might have 1 column, another 3...How can I solve this issue?
values <- c(5, 4, 3, 5, 2, 7)
bar_height <- 5
ylim <- c(0, 5)
xlim <- c(0, 20)
plot(0, 0,
type="n",
xlab="",
ylab="",
bty="n",
xlim=xlim,
ylim=ylim,
asp=1,
axes=FALSE)
padding <- 1
for (i in 1:length(values)) {
num_cols <- ceiling(values[i] / bar_height)
# main issue is to identify starting points (xleft) for each value.#
xleft <- (i-1)*(num_cols+padding)
#
x <- rep(xleft:(xleft+num_cols-1), each=bar_height)[1:values[i]]
y <- rep((1:bar_height), num_cols)[1:values[i]]
symbols(x, y, squares = rep(1, length(x)), inches = FALSE,
add=TRUE, bg="black", fg="white")
}
Thank you Sergey