[R] Plotting sum rather than count in hexbin

Camilo Mora cmora at Dal.Ca
Sun Oct 19 20:11:47 CEST 2014


Thanks Jim,

Sorry for the confusion. Is there a way to plot the hexbin using the colors generated based on the sums of the values of the points at each hexbin? My problem is not so much about the use of a color scale as it is for Hexbin to display the sum of the bins rather than the count.

Thanks again,

Camilo

________________________________________
From: Jim Lemon <jim at bitwrit.com.au>
Sent: Sunday, October 19, 2014 10:27 AM
To: r-help at r-project.org
Cc: Camilo Mora
Subject: Re: [R] Plotting sum rather than count in hexbin

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