R/plot_hexbin.R

Defines functions .plot_hexbin

.plot_hexbin <- function(drhex,
                         colour_by = "Cluster_majority",
                         colors = NULL,
                         title = NULL,
                         xlab = NULL,
                         ylab = NULL) {
  if (any(!c("x", "y", colour_by) %in% colnames(drhex))) {
    stop("The dataframe must contain columns named 'x', 'y' and label.")
  }

  if (is.null(title)) {
    title <- colour_by
  }

  if (is.null(xlab)) {
    xlab <- "x"
  }

  if (is.null(ylab)) {
    ylab <- "y"
  }

  if (grepl("majority", colour_by)) {
    if (is.null(colors)) {
      ggplot(drhex, aes_string("x", "y", fill = colour_by)) +
        geom_hex(stat = "identity") +
        theme_classic() + theme(legend.position = "bottom") +
        ggtitle(title) +
        labs(x = xlab, y = ylab) + theme(legend.title = element_blank())
    } else {
      ggplot(drhex, aes_string("x", "y", fill = colour_by)) +
        geom_hex(stat = "identity") + scale_fill_manual(values = colors) +
        theme_classic() + theme(legend.position = "bottom") +
        ggtitle(title) +
        labs(x = xlab, y = ylab) + theme(legend.title = element_blank())
    }
  } else {
    ggplot(drhex, aes_string("x", "y", fill = colour_by)) +
      geom_hex(stat = "identity") +
      theme_classic() + scale_fill_viridis_c() + ggtitle(title) +
      labs(x = xlab, y = ylab) + theme(legend.title = element_blank())
  }
}

Try the schex package in your browser

Any scripts or data that you put into this service are public.

schex documentation built on Nov. 8, 2020, 5:56 p.m.