简体   繁体   中英

Log scale with stat_binhex

I have these two distributions I want to plot with log scale for both axes:

library(ggplot2)
set.seed(99)
data <- data.frame(vec1 = as.integer(rlnorm(10000, sdlog = 2)),
                   vec2 = as.integer(rlnorm(10000, sdlog = 2)))

I can easily obtain the result with the geom_point()

ggplot(data, aes(vec1, vec2)) + geom_point() + scale_x_log10() + scale_y_log10()

but not with stat_binhex()

ggplot(data, aes(vec1, vec2)) + stat_binhex() + scale_x_log10() + scale_y_log10()

which will generate an error

Error in if (!missing(xbnds) && any(sign(xbnds - range(x)) == c(1, -1))) stop("'xbnds' must encompass range(x)") : 
  missing value where TRUE/FALSE needed

Is is possible?

Following Didzis Elferts' directions to get the question closed (@ Didzis Elferts' , please feel free to recycle my answer if you like). Code below.

在此处输入图片说明

 … 
    ggplot(data, aes(vec1+1, vec2+1)) + stat_binhex() + scale_x_log10() + scale_y_log10()

Alternatively you can remove the 0 values

bar <- subset(data, vec1 != 0 & vec2 != 0)
ggplot(bar, aes(vec1, vec2)) + stat_binhex() + scale_x_log10() + scale_y_log10()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM