Scatterplot with hexbin
2
0
Entering edit mode
7.0 years ago
elisheva ▴ 120

Hi everyone!
I have to plot a Scatterplot.
I tried to use the hexbin library, but I can't the axis's limits.
And I also tried to add abbline with no success.
Here is my code:

bin<-hexbin(levelsTable$AA.TT,levelsTable$Expression, xbins=50)
cols <- colorRampPalette(c("blue","purple","green","yellow", "red") )
hb<-plot(bin, main="Hexagonal Binning",colorcut = seq(0,1,length.out=24),
     colramp = function(n) cols(24), xlab = "AA/TT ratio", ylab = "Expression level"))
pushHexport(nb$plot.vp)
hexVP.abline(hb$plot.vp,v = 1,col = gray(.6))

And this is the result: https://ibb.co/k7XRbb
I have some additional issues with the labels. but I guess it's much simple.
Thanks!!

R Plot hexbin • 4.5k views
ADD COMMENT
1
Entering edit mode
7.0 years ago

I also had difficulty when first generating hexbin plots for high dimensional mass cytometry data a couple of years ago. Essentially, the approach of using hexbin() followed by plot() and then hexVP.abline() doesn't work. It's as if the viewport in which hexVP.abline() is attempting to place the line is not 'speaking' with the output from the plot() function (or something along these lines...). It's possible to place it after much hassle and time-wasting.

A better approach is to use a combination of hexbinplot() and a function from the lattice package. This will allow you to place the ablines with precision.

require(hexbin)
require(lattice)

require(RColorBrewer)
rf <- colorRampPalette(rev(brewer.pal(9,"BuPu")))

hexbinplot(CD28 ~ CD39, data=temp, aspect="1", xbins=500, cex.labels=1.0, cex.title=1.0, colramp=rf,
    panel=function(x, y, ...)
    {
        panel.hexbinplot(x, y, ...)
        panel.abline(v=c(0,4), h=c(0,4), col="black", lwd=4, lty=5)
    }
)

c

------------------------------------

Note that you can also set x- and y-axis limits, and fit a linear regression line:

rf <- colorRampPalette(rev(brewer.pal(9,"Spectral")))
hexbinplot(CD28 ~ CD39, data=temp, aspect="1", xbins=500, type="r", cex.labels=1.0, cex.title=1.0, colramp=rf, xlim=c(-3,8), ylim=c(-3,8))

d

-----------------------

Like the lattice package functions, which are great and flexible or plotting data in various ways, it's probably worth an entire day exploring hexbinplot() (if you can convince your supervisor...).

Kevin

ADD COMMENT
1
Entering edit mode
7.0 years ago

You could use geom_hex from ggplot2 : http://ggplot2.tidyverse.org/reference/geom_hex.html

and add geom_hline and geom_vline where you want some vertical and horizontal lines.

ADD COMMENT

Login before adding your answer.

Traffic: 1598 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6