Creating a Heatmap using levels of non-distinct factor column in Dataframe

Dear colleagues,
I am learning how to make a heatmap right now from an existing data frame. The data frame is at the link; https://www.dropbox.com/s/rp7e5zbjyowtyor/time_crime_pct.csv?dl=0

The goal is to get Primary.Type on the y-axis and time on the x-axis, whereas crime_percent should be represented by proper intensity color in given cell. As we know, the data frame needs to be converted into a Data matrix before drawing Heatmap. But the Data matrix converts logical/factor columns to internal numeric codes. But that is not so useful for my analysis.

The reason being, if the column Primary.Type is replaced by numeric codes, the rendered heat map will be quite incomprehensible for the audience. Therefore, I am working on retaining the factor column with its original values. One attempt was to set row names same as Primary.Type, but I realized that the column Primary.Type is not distinct hence can't be used as row names.

Can I kindly get help on how to retain the proper values for the factor variable and be able to sketch my heat map?
Advice/help is greatly appreciated.

can you post a small reprex of your problem? no way I am clicking on some random dropbox link :slight_smile:

Are you trying to do something like this?

df <- read_csv("time_crime_pct.csv")
library(tidyverse)
df %>% 
  ggplot(aes(x = time, y = Primary.Type, fill = crime_pct)) +
  geom_raster()

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.