R plotDomain(),plotPoints()

klr1opcd  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(72)

然后代码显示了不同分辨率的plotDomain方法。任何决议的单独代码。问:如何绘制所有分辨率,以便在一个图形上全部可见
软件包:CRAN中的任何软件包(但无需安装其他东西,例如Java或Python)
代码:

rm(list = ls())

library(R6)
library(hexbin)

Domain <- R6Class(
  "Domain",
  public = list(
    data = NULL,
    resolution = NULL,
    
    initialize = function(data, resolution) {
      self$data <- data
      self$resolution <- resolution
    },
    
    plotDomain = function(resolutionIndex, border = rgb(0, 1, 1, .3), col = "#9ecae1", alpha = 0.01) {
      x <- self$data$x
      y <- self$data$y
      
      # Utworzenie mapy heksagonalnej
      hex <- hexbin(x, y, xbins = self$resolution[resolutionIndex])
      
      # Ustawienie parametrów dla linii
      par(lwd = 1.5, lend = 2)
      
      # Wyświetlenie mapy z przeźroczystym kolorem
      plot(hex, colramp = colorRampPalette(rgb(t(col2rgb(col)/255), alpha = 0.05)), 
           border = border, legend = FALSE)
    }
  )
)

data <- read.csv("C:/Users/jerzy/OneDrive/Dokumenty/R Studio/test_data_00.csv") 
#data <- read.csv(file = "./test_data_00.csv")

# Przygotowanie danych x i y
x <- data$x
y <- data$y

# Inicjalizacja obiektu klasy Domain
a <- Domain$new(data.frame(x = x, y = y), c(10, 20, 30))

graphics.off()

# Wywołanie metody plotDomain dla różnych rozdzielczości
a$plotDomain(resolutionIndex = 1)
a$plotDomain(resolutionIndex = 2)
a$plotDomain(resolutionIndex = 3)

我的结果:

预期结果:

zf9nrax1

zf9nrax11#

理论上,您应该能够将newpage参数传递给hexbin的plot方法,但在这里似乎不起作用。另一种方法是指定add = TRUE将六边形添加到现有图中,但这意味着您需要以R为底计算和绘制六边形:

Domain <- R6Class("Domain",
  public = list(
    data = NULL,
    resolution = NULL,
    initialize = function(data, resolution) {
      self$data <- data
      self$resolution <- resolution
    },
    
    plotDomain = function(resolutionIndex, border = rgb(0, 1, 1, .3), 
                          col = "#9ecae1", alpha = 0.2, add = FALSE) {
      x <- self$data$x
      y <- self$data$y
      hex <- hexbin(x, y, xbins = self$resolution[resolutionIndex])
      center_coords <- hcell2xy(hex)
      grid <- hexcoords(diff(hex@xbnds)/hex@xbins/2,
                        min(diff(sort(unique(hcell2xy(hex)$y))))/3)
                        
      polys <- Map(function(x, y, i) {
        data.frame(x = x + c(grid$x, grid$x[1]),
                   y = y + c(grid$y, grid$y[1]))
        }, center_coords$x, center_coords$y, seq_along(center_coords$x))
      
      if(add == FALSE) plot(center_coords, type = "n", xlim = hex@xbnds,
                            ylim = hex@ybnds)
      
      for(i in seq_along(polys)) {
        polygon(polys[[i]]$x, polys[[i]]$y, col = scales::alpha(col, alpha),
                border = border, lwd = 1.5)
      }
    }
  )
)

您没有包含任何数据,但似乎任何一组x,y坐标都足以用于演示:

set.seed(1)

a <- Domain$new(data.frame(x = rnorm(1000), y = rnorm(1000)), c(10, 20, 30))

a$plotDomain(resolutionIndex = 1)
a$plotDomain(resolutionIndex = 2, col = "gold", border = rgb(1, 1, 0, .3),
             add = TRUE)
a$plotDomain(resolutionIndex = 3, col = "tomato", border = rgb(1, 0, 0, .3),
             add = TRUE)

相关问题