2015-10-09 44 views
1

我想繪製我的數據在ggplot與使用點。它創建該地塊: enter image description here在stat_binhex R日誌規模

正如你可以看到,這是不好的,所以我決定用對數刻度,以獲得更好的結果,我的數據爲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() 
+0

我很困惑。我不明白你對第一個情節不喜歡什麼。它是頂部和底部的點嗎?將無限值設置爲0似乎是一個非常奇怪的選擇。你可以只是你的數據的子集?這可以在ggplot中輕鬆完成:'ggplot(data = subset(test.data,is.finite(d.log)&is.finite(f.log)),...' – Gregor

+0

如果您想要最小的垃圾箱爲了完全透明,你可以添加'scale_alpha_continuous(range = c(0,1))'(默認最小值爲0.1),也可以對該變量進行對數轉換:scale_alpha_continuous(range = c(0,1 ),trans =「log」)' – Gregor

+0

對不起,不好的解釋。我需要聚合點清楚地看到數據,是的,他們是頂部和底部的點,所以這是使用日誌規模的另一個原因。但它減少了所有數據中的幾乎75%,所以它沒有給出有關數據的任何想法。 – eabanoz

回答

2

談到我的評論一個答案,你可以使用對數刻度用於填充透明度

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

指定的範圍從0開始,將使得最小倉完全透明的,這意味着你不會看到少量點的六角形。

+0

我想問一下,在繪圖中應用scale_alpha_continuous(range = c(0,1),trans =「log」)後,計數值變爲1; 20.085,403.428和8103.083,而不是20K,40K和60K。有沒有一種方法可以保留舊計數的相同字母設置? – eabanoz

+0

@eabanoz請參閱csgillespie的答案:http://stackoverflow.com/a/14258838/903061 – Gregor