How to plot points on a hexbin graph in R?
I have two datasets that need to be plotted on the same graph. The set is very large (~ 10βΆ) and I want to build with hexbin and the other set is very small (~ 10) and I want to plot the points. How do I draw dots on a hexbine? The closer to success I got this:
bin = hexbin(x, y) plot(bin) pushViewport(dataViewport(x, y)) grid.points(x, y)
I appreciate any help :)
+3
source to share
3 answers
Assuming you are using a package hexbin
...
library(hexbin)
library(grid)
# some data from the ?hexbin help
set.seed(101)
x <- rnorm(10000)
y <- rnorm(10000)
z <- w <- -3:3
# hexbin
bin <- hexbin(x, y)
# plot - look at str(p)
p <- plot(bin)
# push plot viewport
pushHexport(p$plot.vp)
# add points
grid.points(z, w, pch=16, gp=gpar(col="red"))
upViewport()
+4
source to share
You can use the ggplot package for this task, see the code below, just replace the data.frame used in the data parameter for geom_point with one for the points you want to plot.
library(ggplot2)
library(hexbin)
ggplot(diamonds, aes(carat, price)) + stat_binhex() + geom_point(data = diamonds[c(1,10,100,1000), ], aes(carat, price), size=10, color = 'red' )
+1
source to share
Try this ... it should work fine. Just create panel.function in hexbinplot :
hexbinplot(d.frame$X ~ d.frame$Y
,aspect=...,cex.title=...
,panel=function(x, y, ...){
panel.hexbinplot(x,y,...)
# panel.curve(...) # optional stuff
# panel.text(...) # optional stuff
panel.points(x=c(25,50),y=c(100,150),pch=20,cex=3.2)
}
)
take a look for example at: How do I add points to a bwplot multi-pane graph grid?
+1
source to share