Search code examples
rplot

R plotDomain(), plotPoints()


The code then shows the plotDomain methods for different resolutions. Code of any resolution separately. Question: How to plot all resolutions so that they are all visible on one graph

Packages: any from CRAN (but without having to install additional things, e.g. Java or Python)

Code:

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)

My result: enter image description here

Expected result: enter image description here


Solution

  • In theory, you should be able to pass a newpage argument through to the plot method for hexbin, but this doesn't seem to work here. An alternative is to specify add = TRUE to add the hexagons to an existing plot, though this means you need to calculate and draw the hexagons in base 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)
          }
        }
      )
    )
    

    You haven't included any data, but it seems any bunch of x, y co-ordinates should suffice for a demo:

    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)
    

    enter image description here