R在stat_binhex处记录比例

时间:2015-10-09 23:17:01

标签: r plot ggplot2 scale loglog

我想使用点在ggplot中绘制我的数据。它创建了这个情节: enter image description here

正如你所看到的那样,它并不好,所以我决定使用对数比例来获得更好的结果,我的数据有0,创造无限。我使用这个脚本将无限转换为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), ]  

和我的数据(test.data)看起来像这样;

                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

您可以从此处下载示例数据: https://drive.google.com/open?id=0B1HBIov_NABWWXRobmZwV0Z2Tmc

我用这个剧本绘图;

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()

它创造了这个情节; enter image description here

正如您所看到的,它为左上角的一个点创建了一个六边形,而不是数据的正确表示。

我的问题是我可以在此代码中的scale_x_log10()函数内进行inf清理;

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() 

1 个答案:

答案 0 :(得分:2)

将我的评论转到答案,您可以使用日志刻度来填充透明度

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

指定范围从0开始将使最小的bin完全透明,这意味着您不会看到少量点的六边形。