ASH
ASH

Reputation: 20302

Trying to create heatmap from DataFrame, or Matrix

I have sample data like this.

product <- c('Credit')
startdate <- c('12/30/2018','12/30/2018','12/30/2018','12/30/2018','12/30/2018')
reporting_amount <- c('29918501.83','50000000','40000000','13766666.67','75000000')
mydata <- data.frame(product, startdate, reporting_amount)

It all comes from SQL Server; dumped out as a CSV file. I want to create a heatmap from this data set. Does this need to be converted to a matrix, or can I feed a data frame into a heatmap?

I tried this:

heat_matrix <- data.matrix(heat)
heat_heatmap <- heatmap(heat_matrix, Rowv=NA, Colv=NA, col = cm.colors(256), scale="column", margins=c(5,10))

Then I ended up with this:

enter image description here

I feel like I need several dimensions to make this work right. I have multiple products per date and multiple reporting_amount values per product. The data set is basically a top 10 revenue, by product by date, from SQL Server.

Ultimately, I would like to see something like this!

enter image description here

But instead of tickers and percent up/down, list products and reporting_amount, either for one date or all dates. One date is fine if that's easier. Obviously this is R-code, but I can easily switch to Python if that is a better tool for this kind of job.

Upvotes: 1

Views: 103

Answers (1)

Tyelcie
Tyelcie

Reputation: 48

Your final example dose not look like a heatmap, but treemap. Maybe you could try this:

library(treemapify)
product <- c('Credit')
startdate <- c('12/30/2018','12/30/2018','12/30/2018','12/31/2018','12/31/2018')
reporting_amount <- c(29918501.83,50000000,40000000,13766666.67,75000000)
mydata <- data.frame(product, startdate, reporting_amount)
mydata$product <- as.character(product)

The reporting_amount used to define areas or color(fill) should be numeric but not character, so I deleted the quotes. And the label (here I used Product) should be character.

ggplot(mydata,aes(area = reporting_amount,fill = reporting_amount,subgroup = startdate,label = product)) +
  geom_treemap() +
  geom_treemap_subgroup_border(size = 10)+
  geom_treemap_text(color = 'white',grow = T,place = 'center') +
  geom_treemap_subgroup_text()

Then I got this picture:

enter image description here

I'm not sure whether this is what you're looking for, just that the area and color can change by some value looks quite similar to your final example. Maybe when you have more dimensions in the dataset, more features can be defined by the treemap.

Upvotes: 1

Related Questions