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?
hexbin
usesgrid
graphics, notbase
. 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:gets approximately what you want, but the intercept here was found by eye.
You will have more luck using
ggplot2
: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:
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.
Use lattice version of hex bin -
hexbinplot()
. Withpanel
you can add your line, and withstyle
you can choose different ways of visualizing hexagons. Check help forhexbinplot
for more.