【问题标题】:2d Kernal Density plot where points with 0 density are ignored (using ggplot2) in R2d 核密度图,其中在 R 中忽略密度为 0 的点(使用 ggplot2)
【发布时间】:2018-04-13 19:59:55
【问题描述】:

我正在尝试在篮球场的图像上绘制投篮密度;但是,我无法弄清楚如何制作它,因此忽略密度 = 0 的点。

我目前收到以下信息:

我使用的代码如下:

## LIBRARIES 
library(grid)
library(jpeg)
library(RCurl)
library(ggplot2)
library(gridExtra)
library(viridis)

courtImg.URL = "https://thedatagame.files.wordpress.com/2016/03/nba_court.jpg"

court = rasterGrob(readJPEG(getURLContent(courtImg.URL)),
                   width=unit(1,"npc"), height=unit(1,"npc"))


ggplot(df, aes(x = df$Baseline, y=df$Side)) +
     ylim(0, 100) +
     ylab(" ") +
     xlim(-50, 50) +
     xlab("Baseline") +
     annotation_custom(court, -50, 50, 0, 100) +
     coord_fixed() +
     scale_fill_viridis(option='inferno', end=1) +
     stat_density_2d(aes(fill =..density..), geom = "raster", contour=FALSE) +
     guides(fill = guide_colorbar(title = "% of Shots Taken"))

我想要的输出如下所示:

归根结底,我只是想制作一个热图版本的镜头图。如果有人对我如何能够以完全不同的方式做到这一点有任何建议,我绝对愿意接受!

提前致谢!

【问题讨论】:

    标签: r ggplot2 kernel-density density-plot


    【解决方案1】:

    问题非常简单:您放置在图像上的图块是不透明的,因此无法看到球场。降低 alpha,您可以看到下面绘制的栅格和球场。

    library(grid)
    library(jpeg)
    library(RCurl)
    
    library(ggplot2)
    library(gridExtra)
    library(viridis)
    
    
    courtImg.URL = "https://thedatagame.files.wordpress.com/2016/03/nba_court.jpg"
    
    court = rasterGrob(readJPEG(getURLContent(courtImg.URL)),
                                         width=unit(1,"npc"), height=unit(1,"npc"))
    
    df <- data.frame(Baseline = rnorm(100, 0, 30), Side = rnorm(100, 50, 25))
    
    ggplot(df, aes(x = df$Baseline, y=df$Side)) +
        ylim(0, 100) +
        ylab(" ") +
        xlim(-50, 50) +
        xlab("Baseline") +
        annotation_custom(court, -50, 50, 0, 100) +
        coord_fixed() +
        scale_fill_viridis(option='inferno', end=1) +
        stat_density_2d(aes(fill =..density..), geom = "raster", contour=F, alpha = 0.5) +
        guides(fill = guide_colorbar(title = "% of Shots Taken"))
    

    reprex package (v0.2.0) 于 2018 年 4 月 14 日创建。

    【讨论】:

      猜你喜欢
      • 2015-09-12
      • 2018-01-23
      • 2013-12-05
      • 1970-01-01
      • 2014-03-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-01
      • 1970-01-01
      相关资源
      最近更新 更多