[R] Plotting sum rather than count in hexbin
Jim Lemon
jim at bitwrit.com.au
Sun Oct 19 12:27:26 CEST 2014
On Sun, 19 Oct 2014 08:09:28 AM Camilo Mora wrote:
> Hi everyone,
>
> This may be a trivial solution but I would appreciate any help.
>
> I have a database with three variables. I would like to plot the first
two
> variables in a xy plot making the color of points proportional to the
> values in the third variable. Given that many points overlap, I
decided to
> use the hexbin package, which allows aggregating the points by a
third
> variable. I figured out how to make the sums by hexbins but I am
falling
> short in how to link the sums back to the hexbins and then plot the
hexbins
> color coded by the sums?. Below is the code so far.
>
> Thanks,
>
> Camilo
>
>
>
> library(hexbin)
>
>
> #generates data for three variables
> dat=data.frame( x = c(rep(1:10,3)),
> y = c(rep(1:10,3)),
> z = c(rep(1:10,3)))
>
> #generates hexbin with the x and y variables
> hbin<-hexbin(dat$x, dat$y, xbins=10, IDs=TRUE)
>
> #sum values of points inside hexbins
> SumHexBin<-hexTapply(hbin, dat$z, sum)
>
>
> #the question is how to link the SumHexBin back to the hbin and
then plot it
> color coding bins by the sums?
>
Hi Camilo,
The color.scale function (plotrix) will convert numeric values into colors
in a number of ways. I think you want the names of SumHexBin as
numbers, so a simple way is:
library(plotrix)
hbincol<-color.scale(as.numeric(names(SumHexBin)),
extremes=c("red","green"))
Jim
More information about the R-help
mailing list