簡體   English   中英

嘗試從DataFrame或Matrix創建熱圖

[英]Trying to create heatmap from DataFrame, or Matrix

我有這樣的示例數據。

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)

所有這些都來自SQL Server。 轉儲為CSV文件。 我想從該數據集創建一個熱圖。 是否需要將其轉換為矩陣,還是可以將數據幀輸入熱圖?

我嘗試了這個:

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

然后我結束了:

在此處輸入圖片說明

我覺得我需要幾個方面來使這項工作正確進行。 我每個日期有多個產品,每個產品有多個report_amount值。 數據集基本上是按日期划分的SQL Server收入排名前10位的收入。

最終,我希望看到這樣的東西!

在此處輸入圖片說明

但要列出一個日期或所有日期的產品和reporting_amount而不是股票代碼和上/下百分比。 如果比較容易,一次約會就可以了。 顯然,這是R代碼,但是如果這對於這種工作而言是更好的工具,那么我可以輕松地切換到Python。

您的最后一個示例看起來並不像熱圖,而是樹形圖。 也許您可以嘗試以下方法:

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)

用於定義區域或顏色(填充)的reporting_amount應該為數字而不是字符,因此我刪除了引號。 並且標簽(在這里我使用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()

然后我得到了這張照片:

在此處輸入圖片說明

我不確定這是否是您要尋找的,只是面積和顏色可以改變一些值看起來與最終示例非常相似。 也許當數據集中有更多維時,樹圖可以定義更多特征。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM