bit-question
bit-question

Reputation: 3843

Controlling the display in the hexbin plot

I used the following code to generate the attached plot. But how can I control the two distances: 1) the distance between the main plot on the left and legend on the right ( I marked this distance with double arrow line ). I would like to make this distance shorter. 2) the current x-axis label is so close to the lower border line of the plot.I would like to make this x-axis label lower. Similarly, I would like to move the y-axis label to the left.

par(mar=c(10,10,1,0),mgp=c(10,1,0))
plot(matrix.3.numeric.hexbin)

Here the matrix.3.numeric.hexbin is a hexbin object. It looks to me whatever I change the parameter in par, the generated plot is always the same.

enter image description here

Upvotes: 1

Views: 2044

Answers (2)

Manuel F.
Manuel F.

Reputation: 255

Probably you're looking for this... although I used hexbinplot: You can control the displacement of the legend via the viewport (vp) argument, like this:

require(lattice)
require(hexbin)

some.plot = hexbinplot(d.frame$X ~ d.frame$Y
#   ,par.settings = list(fontsize=list(text=16))
   ,aspect=...,cex.lab=...,xbins=...
   ,vp = viewport(x=0.3,y=0.5,default.units='npc',angle=0,height=0.9)
   ,etc...)

all other spacing (even further legend space to right) can be controlled by the settings "embedded" in lattice graphs. I used them like this (but I assume you can directly set them up in the "par.settings" argument:

up.image = update(some.plot,par.settings = list(
   layout.widths=list(left.padding = 0,right.padding = 0
      ,key.ylab.padding = 0,ylab.axis.padding = 0
      ,ylab.right = 0,axis.key.padding = 0)
   ,layout.heights=list(top.padding = 0,bottom.padding = 0
      ,main.key.padding = 0,key.axis.padding = 0
      ,axis.xlab.padding = 0,xlab.key.padding = 0
      ,key.sub.padding = 0)
   ))

The idea came from: https://stat.ethz.ch/pipermail/r-sig-geo/2011-August/012612.html

Cheers!

Upvotes: 0

IRTFM
IRTFM

Reputation: 263471

You are trying to mix two completely different plotting paradigms. Despite appearing to be a base graphics function, plot, the hexbin-plot method is actually a grid-based plotting function. Furthermore it's an S4 method which means you need to use showMethods to actually see it. ( I cannot tell if your par had any effect or not. (It didn't affect the example I used.)

showMethods('plot', class="hexbin", includeDefs=TRUE)

This is the argument list:

.local <- function (x, style = "colorscale", legend = 1.2, 
    lcex = 1, minarea = 0.04, maxarea = 0.8, mincnt = 1, 
    maxcnt = max(x@count), trans = NULL, inv = NULL, colorcut = seq(0, 
        1, length = min(17, maxcnt)), border = NULL, density = NULL, 
    pen = NULL, colramp = function(n) LinGray(n, beg = 90, 
        end = 15), xlab = NULL, ylab = NULL, main = "", newpage = TRUE, 
    type = c("p", "l", "n"), xaxt = c("s", "n"), yaxt = c("s", 
        "n"), clip = "on", verbose = getOption("verbose")) 

If you wanted to modify its layout you should be working with what is returned as a value:

 plot.vp    the hexViewport constructed and used.
 legend.vp  if a legend has been produced, its viewport.

If you look at the code it appears that it will accept an argument "legend" which is the width of the legend viewport in inches, so try modifying that:

 plot(bin, legend=1.0)

By the way; the confusion about ggplot comes from one of the plotting methods being named gplot.hexbin, but that "g" is a reference to "grid" rather than to "ggplot"

Upvotes: 3

Related Questions