Formatting and manipulating a plot from the R package “hexbin”

筅森魡賤 提交于 2019-11-30 14:18:55

Use lattice version of hex bin - hexbinplot(). With panel you can add your line, and with style you can choose different ways of visualizing hexagons. Check help for hexbinplot for more.

library(hexbin)
library(lattice)
x <- rnorm(1e6)
y <- rnorm(1e6)
hexbinplot(x ~ y, aspect = 1, bins=50, 
           xlab = expression(alpha), ylab = expression(beta), 
           style = "nested.centroids",
           panel = function(...) {
             panel.hexbinplot(...)
             panel.abline(h=0)
             })

hexbin uses grid graphics, not base. There is a similar function, grid.abline, which can draw lines on plots by specifying a slope and intercept, but the co-ordinate system used is confusing:

grid.abline(325,0)

gets approximately what you want, but the intercept here was found by eye.

You will have more luck using ggplot2:

library(ggplot2)
ggplot(data,aes(x=alpha,y=beta)) + geom_hex(bins=10) + geom_hline(yintercept=0.5)

I had a lot of trouble finding a lot of basic plot adjustments (axis ranges, labels, etc.) with the hexbin library but I figured out how to export the points into any other plotting function:

hxb<-hexbin(x=c(-15,-15,75,75),
            y=c(-15,-15,75,75),
            xbins=12)

hxb@xcm    #gives the x co-ordinates of each hex tile
hxb@ycm    #gives the y co-ordinates of each hex tile
hxb@count  #gives the cell size for each hex tile

points(x=hxb@xcm, y=hxb@ycm, pch=hxb@count)

You can just feed these three vectors into any plotting tool you normally use.. there is the usual tweaking of size scaling, etc. but it's far better than the stubborn hexplot function. The problem I found with the ggplot2 stat_binhex is that I couldn't get the hexes to be different sizes... just different colors.

if you really want hexagons, plotrix has a hexagon drawing function that i think is fine.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!