简体   繁体   中英

R Log scale at stat_binhex

I would like to plot my data in ggplot with using points. It creates this plot: 在此处输入图片说明

As you can see that it isn't good so I decided to use log scale to get better outcome and my data has 0's that create infinite. I used this script to convert infinite to 0;

test.data$d.log[is.infinite(test.data$d.log)] <- 0
test.data$f.log[is.infinite(test.data$f.log)] <- 0 
test.data=test.data[complete.cases(test.data), ]  

and my data (test.data) look like this;

                friend_ratio degree_ratio       f.log    d.log
oncevatan81        0.7763884     23.66667 -0.25310235 3.164068
hatunkotu          0.4991004      0.00000 -0.69494803 0.000000
TwitineGeldim      0.9838102     45.00000 -0.01632226 3.806662
Kralice_Hanim      0.9278909      0.00000 -0.07484108 0.000000
buguzelmi          0.7362599   2302.00000 -0.30617214 7.741534
DogrulariYaziyo    0.8489903      0.00000 -0.16370754 0.000000

You can download sample data from here: https://drive.google.com/open?id=0B1HBIov_NABWWXRobmZwV0Z2Tmc

I use this script to plot;

p<-ggplot(data=test.data, aes(x=f.log, y=d.log)) +
        stat_binhex(aes(x= f.log, y=d.log,alpha=..count..),fill="#000000" )+ 
        guides(fill=FALSE,colour=FALSE) +
        geom_hline(yintercept = 0, size = 0.5,color="red",linetype = 2) +
        geom_vline(xintercept = 0, size = 0.5,color="red",linetype = 2) +
        theme_bw()

and it creates this plot; 在此处输入图片说明

As you can see that it creates one hexagon for one dot on upper left corner and it not a right representation of data.

My question is that can I do inf cleaning inside of the scale_x_log10( ) function in this code;

p<-ggplot(data=test.data, aes(x=friend_ratio, y=degree_ratio)) +
        scale_x_log10(breaks = trans_breaks("log10", function(x) 10^x),
                      labels = trans_format("log10", math_format(10^.x)))+
        scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),
                      labels = trans_format("log10", math_format(10^.x)))+
        geom_hex(aes(x= friend_ratio, y=degree_ratio))+
        geom_hline(yintercept = 1, size = 0.5,color="red",linetype = 2)+
        geom_vline(xintercept = 1, size = 0.5,color="red",linetype = 2)+
        theme_bw() 

Turning my comment to an answer, you can use a log scale for the fill transparency with

scale_alpha_continuous(range = c(0, 1), trans = "log")

Specifying that the range starts at 0 will make the smallest bin completely transparent, which means you won't see hexagons for small numbers of points.

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