#' Create heatmap from dataset
#'
#' Feed in a dataset and get a heatmap in return
#'
#' @param idata Input dataframe
#' @param selcols Numeric vector of column indices
#' @param axis_txt_lim character limit for label text
#'
#' @return Returns a heatmap object showing number of literature under different categories in user specified \code{selcols}
#' @keywords internal
#' @author Ezgi Tanriver-Ayder and Sanita Dhaubanjar
GenHeatMap <- function(idata, selcols, axis_txt_lim = 60) {
# if df is a shapefile, remove geometry column
if (any(class(idata) == "sf")) {
idata <- sf::st_drop_geometry(idata)
}
# Bind variables locally to function (helps avoid R CMD notes)
listone <- listtwo <- n <- NULL
# Convert columns to factors to allow for categorical classification for both numeric and character data -------
tmp <- as.data.frame(sapply(idata[selcols], function(x) as.factor(x)))
# Plot Heatmap ------
heatmp <- tmp %>%
dplyr::rename(listone = colnames(tmp[1]), listtwo = colnames(tmp[2])) %>%
dplyr::count(listone, listtwo) %>%
tidyr::complete(listone, listtwo, fill = list(n = 0)) %>%
ggplot2::ggplot(ggplot2::aes(x = listone, y = listtwo, fill = n, label = n)) +
ggplot2::geom_tile(alpha = 0.3, color = "grey60") +
ggplot2::geom_text() +
viridis::scale_fill_viridis() +
ggplot2::theme_minimal() +
ggplot2::theme(
axis.text.x = ggplot2::element_text(angle = 45, hjust = 1),
panel.grid = ggplot2::element_blank(),
text = ggplot2::element_text(size = 14),
axis.title = ggplot2::element_text(size = 16),
title = ggplot2::element_text(size = 18)
) +
ggplot2::xlab(paste0(selcols[1])) +
ggplot2::ylab(paste0(selcols[2])) +
ggplot2::labs(fill = "Count") +
# Limit axis text to a certain number of characters, so that long text doesn't ruin the chart display
ggplot2::scale_x_discrete(labels = function(x) substr(x, 1, axis_txt_lim)) +
ggplot2::scale_y_discrete(labels = function(x) substr(x, 1, axis_txt_lim)) +
ggplot2::ggtitle("Study Heatmap", subtitle = paste(selcols[2], "by", selcols[1]))
heatmp
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.