【问题标题】:Create density plots out of aggregated data根据聚合数据创建密度图
【发布时间】:2017-01-10 22:29:38
【问题描述】:

我有一个包含 3 列聚合数据的数据框:CreditScore、Count、Month。

所以一行有 550、3、3 表示 3 月份有 3 个人的信用评分为 550。

我正在尝试创建叠加的密度图,以比较两个月之间信用分布的差异。

我觉得这应该很简单,但在谷歌上找不到任何东西。

尝试在 R 中执行此操作。

欢迎提出任何建议。

数据示例:

structure(list(CrScore = c(0L, 2L, 3L, 530L, 535L, 544L, 549L, 
551L, 554L, 558L, 560L, 561L, 563L, 565L, 567L, 568L, 569L, 577L, 
579L, 580L), Count.of.MFSAccount = c(2L, 9L, 2L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 1L, 1L, 3L, 1L, 1L, 2L, 1L, 2L, 1L, 1L), EnterDate.Month = structure(c(17136, 
17136, 17136, 17136, 17136, 17136, 17136, 17136, 17136, 17136, 
17136, 17136, 17136, 17136, 17136, 17136, 17136, 17136, 17136, 
17136), class = "Date")), .Names = c("CrScore", "Count.of.MFSAccount", 
"EnterDate.Month"), row.names = c(10L, 28L, 42L, 80L, 113L, 174L, 
212L, 231L, 259L, 299L, 320L, 331L, 359L, 382L, 409L, 421L, 432L, 
540L, 573L, 593L), class = "data.frame")

【问题讨论】:

  • 如果您make a reproducible example 并概述您尝试过的任何内容,我相信您会得到一些很大的帮助。
  • 您是否愿意分解您的数据,即将每个值复制必要的次数?如果您没有庞大的数据集或需要超高效率,那可能是最简单的方法...
  • 分解数据将是我最后的手段,我认为必须有一种方法来处理聚合数据?这个想法似乎很简单。
  • 基本函数没有一个简单的方法。密度通常用于连续随机变量,通常无法在不丢失信息的情况下进行聚合。

标签: r


【解决方案1】:

ggplot2 使用标准化版本的Count.of.MFSAccount 作为权重:

library(ggplot2)
library(dplyr)

# Create weights that are normalized within each date
df <- df %>%
        group_by(EnterDate.Month) %>%
        mutate(w = Count.of.MFSAccount / sum(Count.of.MFSAccount))

# Plot with constructed weights
ggplot(df, aes(CrScore, weight=w, color=factor(EnterDate.Month))) + geom_density()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-23
    • 1970-01-01
    • 1970-01-01
    • 2017-09-07
    • 2022-01-12
    • 2021-12-01
    • 2019-02-11
    相关资源
    最近更新 更多