Adjust hexbin legend breaks

2019-08-13 19:36发布

问题:

In this example of a hexbin plot, the legend on the right has 10 levels/classes/breaks. Does anyone know how to change the number of levels? Say I want to change it to 5 or something.

  library(hexbin)
  x=rnorm(1000, mean = 50, sd = 1)
  y=rnorm(1000, mean = 30, sd = 0.5)
  df <- data.frame(x,y)
  #plot(df)

  hb <- hexbin(x=df$x, df$y)
  #hb <- hexbin(x=df$x, df$y,xbins=30)
  #plot(hb)
  gplot.hexbin(hb)

回答1:

Like this?

gplot.hexbin(hb,colorcut=5)

And here's approximately the same thing using ggplot.

library(ggplot2)
ggplot(df, aes(x,y))+
  geom_hex(aes(fill=cut(..value..,breaks=pretty(..value..,n=5))),bins=15)+
  scale_fill_manual("Count",values=grey((5:0)/6))



标签: r plot r-grid