如何避免geom_hex六边形变平

时间:2017-06-04 04:46:41

标签: r ggplot2

我想用不同颜色的ggplot2绘制一些六边形。 当我尝试下面的代码时,我得到压扁的六边形

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.73839349132101e-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 ()

此外,当我不对数值进行舍入时,我得到一个空图。 可能是什么问题?

由于

1 个答案:

答案 0 :(得分: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)) 

似乎最好避免花车。

相关问题