r - stat_summary_hex coloured by ratio -
let's have data frame following columns: x, y, num, denom
, , produce hex plot colours of hexagons set sum(num)/sum(denom)
.
i assumed answer involve stat_summary_hex
naively tried:
ggplot(data, aes(x=x, y=y)) + stat_summary_hex(fun=function(d) {sum(d$num)/sum(d$denom) })
but output is:
error: stat_summaryhex requires following missing aesthetics: z
and understand why (i didn't give z
aesthetic), i'm not sure try next: how can pass in 2 z
aesthetics (i.e. num
, denom
)?
i ended finding hack wanted, record here:
ggplot(data, aes(x=x,y=y,z=complex(0,num,denom))) + stat_summary_hex(fun= function(x) { sum(re(x)) / sum(im(x)) })
essentially, did provide z
parameter, column of complex numbers. complex numbers numbers, ggplot lets them through, , have 2 parts, real , imaginary part, aggregation function able compute ratio wanted.
Comments
Post a Comment