我正在 R 中制作一个单位/符号图表,它看起来几乎像华夫饼图,每个类别之间都有间距。在我的图表中,每个条形图都有固定数量的行和任意数量的列。我的主要问题是条形之间的间距。我可以使用增量来查找 X 轴上每个条的起点 - 请参阅下面的代码 - 但这种方法仅在每个条具有固定宽度时才有效。在我的情况下,一个栏可能有 1 列,另外 3 列......我该如何解决这个问题?
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")
}
谢谢谢尔盖