The x.wanted
in the MWE uses x.wanted
calculated on the row above and then adds values from actual row. How can that be done in data.table? I thought shift
was the way, but gives wrong result.
x.wanted
is a way to separate bars in a plot that have different bar-widths.
library(data.table)
library(ggplot2)
dt <- data.table(x.group=c(rep(1L, 4L), rep(2L, 5L)),
x.width=c(2L, 4L, 2L, 6L, 4L, 2L, 4L, 6L, 2L),
x.sep=c(0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L),
x.wanted=c(1, 5, 9, 14, 2, 6, 10, 16, 21))
dt[, x.try.1:=x.width/2]
dt[, x.try.1:=shift(x=x.try.1, fill=0, type="lag") + x.sep + x.width/2, by=x.group]
p <- ggplot(dt, aes(x=x.wanted, y=5))
p <- p + geom_bar(aes(width=x.width), stat="identity", position="stack")
p <- p + facet_grid(x.group~., scales="free_x")
p
Plot is added to visualise wanted result. x.try.1 is my failed attempt.