【问题标题】:ggplot - How to create a heatmap that calculates and displays ratios?ggplot - 如何创建计算和显示比率的热图?
【发布时间】:2018-01-31 10:56:34
【问题描述】:

我正在尝试使用 ggplot 创建一个热图,其中填充表示某些比率的大小。我目前有一个看起来像这样的表:

 company survey  type        value
* <chr>   <chr>   <chr>       <dbl>
1 Raps    S1_2014 Revenue 282576375
2 Raps    S2_2014 Revenue 654413143
3 Raps    S3_2014 Revenue 365753902

我的ggplot代码是:

ggplot(df1, aes(x=survey, y=survey)) +
   geom_tile(aes(fill=value/value))

结果图如下所示:

如您所见,图中存在调查不与自身交互的空白(即 S1_2014 和 S3_2014)。我可以将这些交叉点编码到我的 ggplot 调用中吗?还是我需要整理数据?如果我确实需要争吵,我会怎么做?

谢谢。

【问题讨论】:

    标签: r ggplot2 dplyr


    【解决方案1】:

    是的 - ggplot 只会绘制您提供的数据。我真的很惊讶value / value 给你的不是 1。你可以交叉加入你的数据然后绘图:

    dd = merge(df1[c(2, 4)], df1[c(2, 4)], all = T, by = NULL)
    
    ggplot(dd, aes(survey.x, survey.y, fill = value.x / value.y)) + 
      geom_tile()
    

    通过这种方式,您还可以控制(并判断)比率的走向。


    使用这个作为输入:

    df1 = read.table(text = "company survey  type        value
    1 Raps    S1_2014 Revenue 282576375
    2 Raps    S2_2014 Revenue 654413143
    3 Raps    S3_2014 Revenue 365753902", header = T)
    

    【讨论】:

    • 谢谢!至于我图表中的值,似乎我上传了错误的文件。在显示的填充=值中,这就是为什么没有任何 1。
    猜你喜欢
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多