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

雨燕双飞 提交于 2019-12-18 16:50:28

问题


I generate a plot using the package hexbin:

# install.packages("hexbin", dependencies=T)
library(hexbin)


set.seed(1234)
x <- rnorm(1e6)
y <- rnorm(1e6)
hbin <- hexbin(
  x   = x
  , y = y
  , xbin = 50
  , xlab = expression(alpha)
  , ylab = expression(beta)
)


## Using plot method for hexbin objects:
plot(hbin, style = "nested.lattice")

abline(h=0)

This seems to generate an S4 object (hbin), which I then plot using plot. Now I'd like to add a horizontal line to that plot using abline, but unfortunately this gives the error:

plot.new has not yet been called

I have also no idea, how I can manipulate e.g. the position of the axis labels (alpha and beta are within the numbers), change the position of the legend, etc.

I'm familiar with OOP, but so far I could not find out how plot() handles the object (does it call certain methods of the object?) and how I can manipulate the resulting plot.

Why can't I simply draw a line onto the plot?

How can I manipulate axis labels?


回答1:


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)
             })




回答2:


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)




回答3:


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.



来源:https://stackoverflow.com/questions/15504983/formatting-and-manipulating-a-plot-from-the-r-package-hexbin

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