7

I generate a plot using the package hexbin:

enter image description here

# 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?

R_User
  • 10,682
  • 25
  • 79
  • 120

3 Answers3

4

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

enter image description here

Geek On Acid
  • 6,330
  • 4
  • 44
  • 64
  • How can I change the size of the axis labels and the values shown along the axis? I tried `cex.*` in all variations, but the only thing I could change was the size of the legend-labels,... I was also not able to change the parameters (e.g. `par(mar=c(15,15,15,15))`) – R_User May 02 '13 at 12:44
  • 1
    Look at help for `xyplot` and arguments for `scales`. For example, changing size of values along x axis would be by adding `scales = list(x = list(cex=1.5))` to you `hexbinplot`. To change axis labels you need to add `par.settings = list(par.xlab.text=list(cex=1.5))` to your plot. I know it's not very intuitive, but that's how it works in lattice. Let me know if that solves it. – Geek On Acid May 02 '13 at 15:23
  • I still don't understand why this work, and where I can find it in the help. For changing the size of `xlab` I tried `xlab = list( expression(alpha) ,cex = 2 )` It works, but there is no hint in the manual. Maybe my way is this `grob`??? I also still did not understand how `plot`, `trellis`, `lattice`, `ggplot`, `grid`,... are related. – R_User May 03 '13 at 09:50
  • It's quite broad and complex topic, `base` graphics, `ggplot2` and `lattice/trellis` are three different systems for graphical display in R. There are some major differences there. You have to google a bit to get a better idea how it works. – Geek On Acid May 03 '13 at 16:27
3

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)

hexbin

James
  • 65,548
  • 14
  • 155
  • 193
  • 3
    So, its the package that is not good? To be honest, I have absolutely no idea how different packages plot things different. So far I did not use ggplot, since the output is ugly, and ?ggplot does not give any information on how to change e.g. axis titles (the same is true for hexbin). Is the a good documentation of how different plot mechanisms work? (`plot()` uses `base`, `abline()` can only make changes to `base` plots, `hexbin` uses `grid` although it is used within `plot()`, which itself uses `base` (but not in this case, since it uses `grid`), ggplot uses ???,...) – R_User Mar 19 '13 at 17:22
  • @Sven See http://docs.ggplot2.org/current/ and http://ggplot2.org/ for documentation. Axis titles can be changed here with `scale_x_continuous` and `scale_y_continuous`. `ggplot2` used the `grid` system as well, but provides an easier interface. – James Mar 20 '13 at 09:42
0

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.