【问题标题】:Trying to create heatmap from DataFrame, or Matrix尝试从 DataFrame 或 Matrix 创建热图
【发布时间】:2018-04-24 21:35:03
【问题描述】:

我有这样的样本数据。

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))

然后我就这样结束了:

我觉得我需要几个维度才能使这项工作正常进行。我每个日期有多个产品,每个产品有多个 reports_amount 值。该数据集基本上是来自 SQL Server 的按产品分类的前 10 名收入。

最终,我希望看到这样的东西!

但不是代码和百分比上/下,列出产品和reporting_amount,无论是一个日期或所有日期。如果这更容易,约会就可以了。 显然这是 R 代码,但如果 Python 是这种工作的更好工具,我可以轻松切换到 Python。

【问题讨论】:

    标签: python r python-3.x


    【解决方案1】:

    您的最后一个示例看起来不像热图,而是树状图。也许你可以试试这个:

    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()
    

    然后我得到了这张照片:

    我不确定这是否是您要查找的内容,只是区域和颜色可以更改一些值,看起来与您的最终示例非常相似。也许当数据集中有更多维度时,树形图可以定义更多特征。

    【讨论】:

    • 啊!我认为这会奏效!感谢您为我指明正确的方向!!!!
    猜你喜欢
    • 2019-10-30
    • 2018-10-05
    • 1970-01-01
    • 2012-08-30
    • 1970-01-01
    • 2018-06-16
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    相关资源
    最近更新 更多