2017-06-04 48 views
0

我想繪製一些六邊形與ggplot2不同顏色。 當我嘗試,因爲下面的代碼,我得到壓扁的六邊形如何避免geom_hex六邊形變平

library(ggplot2) 
coords <- 
data.frame(x=c(1.11803398874989,0.559016994374948,-0.559016994374947,-1.11803398874989,-0.559016994374948,0.559016994374947,2.23606797749979,1.1180339887499,-1.11803398874989,-2.23606797749979,-1.1180339887499,1.11803398874989,1.73205080756888,1.22464679914735e-16), 
      y=c(0,0.968245836551854,0.968245836551854,1.36919674566051e-16,-0.968245836551854,-0.968245836551855,0,1.93649167310371,1.93649167310371,2.1e-16,-1.93649167310371,-1.93649167310371,1,2), 
      kind=c(rep("blue",12),"red","blue")) 

ggplot (data = coords, aes (x = round(x,digits =13), y = round(y,digits=13), fill = kind , group = 1)) + 
    geom_hex (colour = "red", stat = StatIdentity) + 
    scale_x_continuous(expand = 0 : 1) + 
    scale_y_continuous(expand = c (0, 2)) + 
    coord_equal() 
然而

,當我嘗試繪圖僅前12個六邊形,我沒有任何問題

# fine result 
ggplot (data = coords[1:12,], aes (x = round(x,digits =13), y = round(y,digits=13), fill = kind , group = 1)) + 
    geom_hex (colour = "red", stat = StatIdentity) + 
    scale_x_continuous(expand = 0 : 1) + 
    scale_y_continuous(expand = c (0, 2)) + 
    coord_equal() 

還當我不四捨五入的價值,我得到一個空的陰謀。 可能是什麼問題?

謝謝

回答

0

我只是想繪製19個不重疊的六邊形。爲此,我可以畫出如下圖:

coords <- 
    data.frame(
    x = c(2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 3, 5, 7), 
    y = c(2, 1, 2, 1, 2, 1, 2, 3, 4, 3, 4, 3, 4, 3, 4, 3, 5, 5, 5), 
    kind=c(rep("blue",12),"red","blue","red","blue",rep("blue",3)) 
) 

ggplot (data = coords, aes (x = x, y = y, fill = kind , group = 1)) + 
    geom_hex (colour = "red", stat = StatIdentity) + 
    scale_x_continuous(expand = c(0:1)) + 
    scale_y_continuous(expand = c(0,2)) 

看來最好避免浮動。