简体   繁体   English

尝试从DataFrame或Matrix创建热图

[英]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; 所有这些都来自SQL Server。 dumped out as a CSV file. 转储为CSV文件。 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: 然后我结束了:

在此处输入图片说明

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. 我每个日期有多个产品,每个产品有多个report_amount值。 The data set is basically a top 10 revenue, by product by date, from SQL Server. 数据集基本上是按日期划分的SQL Server收入排名前10位的收入。

Ultimately, I would like to see something like this! 最终,我希望看到这样的东西!

在此处输入图片说明

But instead of tickers and percent up/down, list products and reporting_amount, either for one date or all dates. 但要列出一个日期或所有日期的产品和reporting_amount而不是股票代码和上/下百分比。 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. 显然,这是R代码,但是如果这对于这种工作而言是更好的工具,那么我可以轻松地切换到Python。

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. 用于定义区域或颜色(填充)的reporting_amount应该为数字而不是字符,因此我删除了引号。 And the label (here I used Product ) should be character. 并且标签(在这里我使用Product )应该是字符。

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: 然后我得到了这张照片:

在此处输入图片说明

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. 也许当数据集中有更多维时,树图可以定义更多特征。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM