【问题标题】:Plot series of filled hexagons in ggplot2在ggplot2中绘制一系列填充六边形
【发布时间】:2014-07-23 06:27:29
【问题描述】:

我正在尝试在 ggplot2 中创建填充六边形(以六边形格子为中心的多边形)的镶嵌。我已经使用“plot”命令完成了这项工作,但正在努力将其转换为 ggplot。

这是设置的代码:

# Generate a lattice of points equally spaced in the centers of a hexagonal lattice
dist = 1     # distance between the centers of hexagons
nx = dist*15 # horizontal extent
ny = dist*15 # vertical extent      

MakeHexLattice = function(nx, ny, dist, origin=c(0,0)) {
  locations = cbind(location = 1:(nx*ny),
                    x = sort(c(rep(seq(from=0, by=dist, length.out=nx),each=ceiling(ny/2)),
                               rep(seq(from=dist/2, by=dist, length.out=nx),
                                   each=floor(ny/2)))) + origin[1],
                    y = rep(c(seq(from=0, by = dist*sqrt(3), length.out=ceiling(ny/2)),
                              seq(from=dist*sqrt(3)/2, by=dist*sqrt(3),
                                  length.out=floor(ny/2))) + origin[2], times=nx))
  class(locations) = c(class(locations), "lattice")
  attr(locations, "gridsize") = dist
  return(locations)
}

下面是使用'plot'创建图像的代码,看起来很不错:

landscape = MakeHexLattice(nx=nx,ny=ny,dist=dist,origin=c(0,0))

# Plot hexagonal lattice as points
plot(x=landscape[,2],y=landscape[,3], pch=19, col="black", cex=0.5, asp=1/1)

# Separate x and y coordinates
lx = landscape[,2] # x-coordinates
ly = landscape[,3] # y-coordinates      

# Plot hexagonal lattice as filled hexagons
hex.x = cbind(lx + 0, lx + 0.5, lx + 0.5, lx + 0, lx - 0.5, lx - 0.5) 
hex.y = cbind(ly - 1/(sqrt(3)), ly - 1/(2*sqrt(3)), ly + 1/(2*sqrt(3)), ly + 1/(sqrt(3)), ly + 1/(2*sqrt(3)), ly - 1/(2*sqrt(3)))
hex.vectors = cbind(hex.x, hex.y)

for(i in 1:(length(hex.vectors)/12)){
  polygon(x=hex.vectors[i,1:6], y=hex.vectors[i,7:12], angle = 120, border=NULL, col="wheat", 
          lty = par("lty"), fillOddEven = FALSE)
}

关于如何使用 ggplot2(我正在过渡到使用)来完成同样的事情的任何提示?我曾尝试使用 geom_polygon 但似乎无法解决 for 循环。 (另外,请不要告诉我使用 'hexbin' -- 这不是我想要实现的目标!)

感谢您的帮助!

【问题讨论】:

    标签: r plot ggplot2


    【解决方案1】:

    与 ggplot 中的大多数东西一样,绘图实际上非常简单,大部分工作是让您的数据具有正确的形状,以使其有意义。 for 循环是完全没有必要的,geom_polygon() 只需要一个带有 x 和 y 坐标的数据框,以及一个定义它们所属组的变量。使用您的数据:

    library(ggplot2)
    library(reshape2)
    
    #Get your coordinates in long format with an id
    hexdat.x <- melt(cbind(id = 1:length(hex.x), as.data.frame(hex.x)), id.vars = "id", value.name = "x")
    hexdat.y <- melt(cbind(id = 1:length(hex.y), as.data.frame(hex.y)), id.vars = "id", value.name = "y")
    
    #Merge them into the same dataframe
    hexdat <- merge(hexdat.x, hexdat.y)
    
    head(hexdat)
    #   id variable    x          y
    # 1  1       V1  0.0 -0.5773503
    # 2  1       V2  0.5 -0.2886751
    # 3  1       V3  0.5  0.2886751
    # 4  1       V4  0.0  0.5773503
    # 5  1       V5 -0.5  0.2886751
    # 6  1       V6 -0.5 -0.2886751
    

    现在要绘制六边形,您只需给ggplot x 和 y 坐标,并指定每个六边形所属的组:

    ggplot(hexdat, aes(x, y)) + 
      geom_polygon(aes(group = id), fill = "wheat", colour = "black")
    

    【讨论】:

    • 太棒了! 非常感谢您的快速反馈。完美运行!
    • 我不知道你可以用 ggplot 做到这一点!谢谢@alexwhan!
    猜你喜欢
    • 2015-01-22
    • 2015-01-12
    • 2016-06-08
    • 1970-01-01
    • 1970-01-01
    • 2011-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多