在R中创建连续热力图

12
我有一系列带有距离的x和y坐标。我想创建一个热力图,显示在x和y范围内每个点的平均距离作为热力图。由于这些点不是在一个类似于晶格的形状中均匀分布,所以该方法需要一种平滑函数来聚类数据并计算每个点的平均值,并用颜色表示该平均值。目前,使用ggplot2,我只能找到像stat_density2d和geom_tile这样的方法,它们仅适用于显示点密度和表示均匀间隔的点(据我所知)。理想情况下,它应该遵循与此图像相同的原理: enter image description here 即使点的密度和位置不均匀,也会根据给定点的邻近区域分配颜色。我不想创建像这张图片那样的矩阵形式的热力图: enter image description here

其中一个表格是按颜色编码的。相反,我想使用非均匀分布的x和y坐标创建连续的热力图,实际上显示数据被分成无限多个矩形的极限。这可能不是函数使用的实际方法,但它提供了我所寻找的一般想法。

以下是一些示例数据:

data=data.frame(x=c(1,1,2,2,3,4,5,6,7,7,8,9),
  y=c(2,4,5,1,3,8,4,8,1,1,6,9),
  distance=c(66,84,93,76,104,29,70,19,60,50,46,36))

如何制作一个热力图,以距离作为颜色标尺,覆盖整个数字范围,就像第一个链接中提供的图表一样?

非常感谢您的帮助!


请查看此链接:https://dev59.com/tWgu5IYBdhLWcg3w6rM9。 - MYaseen208
在这个例子中,你必须手动将数据切成离散的矩形。我希望函数能够根据需要切割数据并创建连续的热力图。 - user1532996
我认为image2D可以帮助您实现您想要的目标。http://www.rforscience.com/portfolio/volcano/ 而且它会为您进行插值。 - Herman Toothrot
4个回答

11
为了生成带有不规则间隔坐标的连续地图,首先需要插值出一个常规网格(这里使用包akima中的函数interp):
require(akima)
data <- data.frame(x=c(1,1,2,2,3,4,5,6,7,7,8,9),
                   y=c(2,4,5,1,3,8,4,8,1,1,6,9),
                   distance=c(66,84,93,76,104,29,70,19,60,50,46,36))
resolution <- 0.1 # you can increase the resolution by decreasing this number (warning: the resulting dataframe size increase very quickly)
a <- interp(x=data$x, y=data$y, z=data$distance, 
            xo=seq(min(data$x),max(data$x),by=resolution), 
            yo=seq(min(data$y),max(data$y),by=resolution), duplicate="mean")
image(a) #you can of course modify the color palette and the color categories. See ?image for more explanation

这里输入图片描述

或者你可以使用 filled.contour 函数进行绘图:

filled.contour(a, color.palette=heat.colors)

在此输入图像描述


2

1
哎呀,你的回答出现在“低质量帖子”审核中。也许你可以添加一些关于文章内容的描述?http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers. - GSee
@baptiste 我们只需要一个简短的总结,概括一下在链接中可能找到的内容,以防止链接失效。 - joran
1
已经注意并修复了。感谢提醒。 - Ari B. Friedman
我不想创建一个彩色编码的表格,因为我的数据包含非均匀分布的x和y坐标。 - user1532996
请按照“如何创建一个R语言可重现示例”的指南提出问题,这样我们才有希望回答您的问题:https://dev59.com/eG025IYBdhLWcg3whGSx - Ari B. Friedman
显示剩余2条评论

0

使用@plannapus建议的akima::interp解决方案,您可以将其转换为ggplot2热图。

这种ggplot2解决方案的优点是您可以轻松地使用geom_point()添加初始点或使用geom_density2d()添加密度曲线(尽管在这里,由于您只有12个点,密度将不可靠)。

library(akima)
library(tidyverse)
data <- data.frame(x=c(1,1,2,2,3,4,5,6,7,7,8,9),
                   y=c(2,4,5,1,3,8,4,8,1,1,6,9),
                   distance=c(66,84,93,76,104,29,70,19,60,50,46,36))
resolution <- 0.1 # you can increase the resolution by decreasing this number (warning: the resulting dataframe size increase very quickly)
a <- interp(x=data$x, y=data$y, z=data$distance, 
            xo=seq(min(data$x),max(data$x),by=resolution), 
            yo=seq(min(data$y),max(data$y),by=resolution), duplicate="mean")


res <- a$z %>% 
  magrittr::set_colnames(a$y) %>% 
  as_tibble() %>% 
  mutate(x=a$x) %>% 
  gather(y, z, -x, convert=TRUE)

res %>% 
  ggplot(aes(x, y)) +
  geom_tile(aes(fill=z)) +
  geom_point(data=data) +
  scale_fill_viridis_c()

reprex package (v0.3.0.9001) 于 2020-01-29 创建


-2
ggplot2::ggfluctuation(data, type="colour")

我不能公开所有这些数据,但是 dput 结构中的 head 如下所示。

structure(list(X1 = 236:241, HomeTeam = structure(c(8L, 19L, 
37L, 4L, 6L, 15L), .Label = c("Arizona Cardinals", "Atlanta Falcons", 
"Baltimore Ravens", "Buffalo Bills", "Carolina Panthers", "Chicago Bears", 
"Cincinnati Bengals", "Cleveland Browns", "Dallas Cowboys", "Denver Broncos", 
"Detroit Lions", "Green Bay Packers", "Houston Oilers", "Houston Texans", 
"Indianapolis Colts", "Jacksonville Jaguars", "Kansas City Chiefs", 
"Los Angeles Raiders", "Los Angeles Rams", "Miami Dolphins", 
"Minnesota Vikings", "New England Patriots", "New Orleans Saints", 
"New York Giants", "New York Jets", "Oakland Raiders", "Philadelphia Eagles", 
"Phoenix Cardinals", "Pittsburgh Steelers", "San Diego Chargers", 
"San Francisco 49ers", "Seattle Seahawks", "St. Louis Rams", 
"Tampa Bay Buccaneers", "Tennessee Oilers", "Tennessee Titans", 
"Washington Redskins"), class = "factor"), AwayTeam = structure(c(9L, 
28L, 11L, 20L, 21L, 22L), .Label = c("Arizona Cardinals", "Atlanta Falcons", 
"Baltimore Ravens", "Buffalo Bills", "Carolina Panthers", "Chicago Bears", 
"Cincinnati Bengals", "Cleveland Browns", "Dallas Cowboys", "Denver Broncos", 
"Detroit Lions", "Green Bay Packers", "Houston Oilers", "Houston Texans", 
"Indianapolis Colts", "Jacksonville Jaguars", "Kansas City Chiefs", 
"Los Angeles Raiders", "Los Angeles Rams", "Miami Dolphins", 
"Minnesota Vikings", "New England Patriots", "New Orleans Saints", 
"New York Giants", "New York Jets", "Oakland Raiders", "Philadelphia Eagles", 
"Phoenix Cardinals", "Pittsburgh Steelers", "San Diego Chargers", 
"San Francisco 49ers", "Seattle Seahawks", "St. Louis Rams", 
"Tampa Bay Buccaneers", "Tennessee Oilers", "Tennessee Titans", 
"Washington Redskins"), class = "factor"), Date = structure(c(45L, 
45L, 45L, 45L, 45L, 45L), .Label = c("1990-09-09", "1990-09-10", 
"1990-09-16", "1990-09-17", "1990-09-23", "1990-09-24", "1990-09-30", 
"1990-10-01", "1990-10-07", "1990-10-08", "1990-10-14", "1990-10-15", 
"1990-10-18", "1990-10-21", "1990-10-22", "1990-10-28", "1990-10-29", 
"1990-11-04", "1990-11-05", "1990-11-11", "1990-11-12", "1990-11-18", 
"1990-11-19", "1990-11-22", "1990-11-25", "1990-11-26", "1990-12-02", 
"1990-12-03", "1990-12-09", "1990-12-10", "1990-12-15", "1990-12-16", 
"1990-12-17", "1990-12-22", "1990-12-23", "1990-12-29", "1990-12-30", 
"1990-12-31", "1991-01-05", "1991-01-06", "1991-01-12", "1991-01-13", 
"1991-01-20", "1991-01-27", "1991-09-01", "1991-09-02", "1991-09-08", 
"1991-09-09", "1991-09-15", "1991-09-16", "1991-09-22", "1991-09-23", 
"1991-09-29", "1991-09-30", "1991-10-06", "1991-10-07", "1991-10-13", 
"1991-10-14", "1991-10-17", "1991-10-20", "1991-10-21", "1991-10-27", 
"1991-10-28", "1991-11-03", "1991-11-04", "1991-11-10", "1991-11-11", 
"1991-11-17", "1991-11-18", "1991-11-24", "1991-11-25", "1991-11-28", 
"1991-12-01", "1991-12-02", "1991-12-08", "1991-12-09", "1991-12-14", 
"1991-12-15", "1991-12-16", "1991-12-21", "1991-12-22", "1991-12-23", 
"1991-12-28", "1991-12-29", "1992-01-04", "1992-01-05", "1992-01-12", 
"1992-01-26", "1992-09-06", "1992-09-07", "1992-09-13", "1992-09-14", 
"1992-09-20", "1992-09-21", "1992-09-27", "1992-09-28", "1992-10-04", 
"1992-10-05", "1992-10-11", "1992-10-12", "1992-10-15", "1992-10-18", 
"1992-10-19", "1992-10-25", "1992-10-26", "1992-11-01", "1992-11-02", 
"1992-11-08", "1992-11-09", "1992-11-15", "1992-11-16", "1992-11-22", 
"1992-11-23", "1992-11-26", "1992-11-29", "1992-11-30", "1992-12-03", 
"1992-12-06", "1992-12-07", "1992-12-12", "1992-12-13", "1992-12-14", 
"1992-12-19", "1992-12-20", "1992-12-21", "1992-12-26", "1992-12-27", 
"1992-12-28", "1993-01-02", "1993-01-03", "1993-01-09", "1993-01-10", 
"1993-01-17", "1993-01-31", "1993-09-05", "1993-09-06", "1993-09-12", 
"1993-09-13", "1993-09-19", "1993-09-20", "1993-09-26", "1993-09-27", 
"1993-10-03", "1993-10-04", "1993-10-10", "1993-10-11", "1993-10-14", 
"1993-10-17", "1993-10-18", "1993-10-24", "1993-10-25", "1993-10-31", 
"1993-11-01", "1993-11-07", "1993-11-08", "1993-11-14", "1993-11-15", 
"1993-11-21", "1993-11-22", "1993-11-25", "1993-11-28", "1993-11-29", 
"1993-12-05", "1993-12-06", "1993-12-11", "1993-12-12", "1993-12-13", 
"1993-12-18", "1993-12-19", "1993-12-20", "1993-12-25", "1993-12-26", 
"1993-12-27", "1993-12-31", "1994-01-02", "1994-01-03", "1994-01-08", 
"1994-01-09", "1994-01-15", "1994-01-16", "1994-01-23", "1994-01-30", 
"1994-09-04", "1994-09-05", "1994-09-11", "1994-09-12", "1994-09-18", 
"1994-09-19", "1994-09-25", "1994-09-26", "1994-10-02", "1994-10-03", 
"1994-10-09", "1994-10-10", "1994-10-13", "1994-10-16", "1994-10-17", 
"1994-10-20", "1994-10-23", "1994-10-24", "1994-10-30", "1994-10-31", 
"1994-11-06", "1994-11-07", "1994-11-13", "1994-11-14", "1994-11-20", 
"1994-11-21", "1994-11-24", "1994-11-27", "1994-11-28", "1994-12-01", 
"1994-12-04", "1994-12-05", "1994-12-10", "1994-12-11", "1994-12-12", 
"1994-12-17", "1994-12-18", "1994-12-19", "1994-12-24", "1994-12-25", 
"1994-12-26", "1994-12-31", "1995-01-01", "1995-01-07", "1995-01-08", 
"1995-01-15", "1995-01-29", "1995-09-03", "1995-09-04", "1995-09-10", 
"1995-09-11", "1995-09-17", "1995-09-18", "1995-09-24", "1995-09-25", 
"1995-10-01", "1995-10-02", "1995-10-08", "1995-10-09", "1995-10-12", 
"1995-10-15", "1995-10-16", "1995-10-19", "1995-10-22", "1995-10-23", 
"1995-10-29", "1995-10-30", "1995-11-05", "1995-11-06", "1995-11-12", 
"1995-11-13", "1995-11-19", "1995-11-20", "1995-11-23", "1995-11-26", 
"1995-11-27", "1995-11-30", "1995-12-03", "1995-12-04", "1995-12-09", 
"1995-12-10", "1995-12-11", "1995-12-16", "1995-12-17", "1995-12-18", 
"1995-12-23", "1995-12-24", "1995-12-25", "1995-12-30", "1995-12-31", 
"1996-01-06", "1996-01-07", "1996-01-14", "1996-01-28", "1996-09-01", 
"1996-09-02", "1996-09-08", "1996-09-09", "1996-09-15", "1996-09-16", 
"1996-09-22", "1996-09-23", "1996-09-29", "1996-09-30", "1996-10-06", 
"1996-10-07", "1996-10-13", "1996-10-14", "1996-10-17", "1996-10-20", 
"1996-10-21", "1996-10-27", "1996-10-28", "1996-11-03", "1996-11-04", 
"1996-11-10", "1996-11-11", "1996-11-17", "1996-11-18", "1996-11-24", 
"1996-11-25", "1996-11-28", "1996-12-01", "1996-12-02", "1996-12-05", 
"1996-12-08", "1996-12-09", "1996-12-14", "1996-12-15", "1996-12-16", 
"1996-12-21", "1996-12-22", "1996-12-23", "1996-12-28", "1996-12-29", 
"1997-01-04", "1997-01-05", "1997-01-12", "1997-01-26", "1997-08-31", 
"1997-09-01", "1997-09-07", "1997-09-08", "1997-09-14", "1997-09-15", 
"1997-09-21", "1997-09-22", "1997-09-28", "1997-09-29", "1997-10-05", 
"1997-10-06", "1997-10-12", "1997-10-13", "1997-10-16", "1997-10-19", 
"1997-10-20", "1997-10-26", "1997-10-27", "1997-11-02", "1997-11-03", 
"1997-11-09", "1997-11-10", "1997-11-16", "1997-11-17", "1997-11-23", 
"1997-11-24", "1997-11-27", "1997-11-30", "1997-12-01", "1997-12-04", 
"1997-12-07", "1997-12-08", "1997-12-13", "1997-12-14", "1997-12-15", 
"1997-12-20", "1997-12-21", "1997-12-22", "1997-12-27", "1997-12-28", 
"1998-01-03", "1998-01-04", "1998-01-11", "1998-01-25", "1998-09-06", 
"1998-09-07", "1998-09-13", "1998-09-14", "1998-09-20", "1998-09-21", 
"1998-09-27", "1998-09-28", "1998-10-04", "1998-10-05", "1998-10-11", 
"1998-10-12", "1998-10-15", "1998-10-18", "1998-10-19", "1998-10-25", 
"1998-10-26", "1998-11-01", "1998-11-02", "1998-11-08", "1998-11-09", 
"1998-11-15", "1998-11-16", "1998-11-22", "1998-11-23", "1998-11-26", 
"1998-11-29", "1998-11-30", "1998-12-03", "1998-12-06", "1998-12-07", 
"1998-12-13", "1998-12-14", "1998-12-19", "1998-12-20", "1998-12-21", 
"1998-12-26", "1998-12-27", "1998-12-28", "1999-01-02", "1999-01-03", 
"1999-01-09", "1999-01-10", "1999-01-17", "1999-01-31", "1999-09-12", 
"1999-09-13", "1999-09-19", "1999-09-20", "1999-09-26", "1999-09-27", 
"1999-10-03", "1999-10-04", "1999-10-10", "1999-10-11", "1999-10-17", 
"1999-10-18", "1999-10-21", "1999-10-24", "1999-10-25", "1999-10-31", 
"1999-11-01", "1999-11-07", "1999-11-08", "1999-11-14", "1999-11-15", 
"1999-11-21", "1999-11-22", "1999-11-25", "1999-11-28", "1999-11-29", 
"1999-12-02", "1999-12-05", "1999-12-06", "1999-12-09", "1999-12-12", 
"1999-12-13", "1999-12-18", "1999-12-19", "1999-12-20", "1999-12-24", 
"1999-12-25", "1999-12-26", "1999-12-27", "2000-01-02", "2000-01-03", 
"2000-01-08", "2000-01-09", "2000-01-15", "2000-01-16", "2000-01-23", 
"2000-01-30", "2000-09-03", "2000-09-04", "2000-09-10", "2000-09-11", 
"2000-09-17", "2000-09-18", "2000-09-24", "2000-09-25", "2000-10-01", 
"2000-10-02", "2000-10-08", "2000-10-09", "2000-10-15", "2000-10-16", 
"2000-10-19", "2000-10-22", "2000-10-23", "2000-10-29", "2000-10-30", 
"2000-11-05", "2000-11-06", "2000-11-12", "2000-11-13", "2000-11-19", 
"2000-11-20", "2000-11-23", "2000-11-26", "2000-11-27", "2000-11-30", 
"2000-12-03", "2000-12-04", "2000-12-10", "2000-12-11", "2000-12-16", 
"2000-12-17", "2000-12-18", "2000-12-23", "2000-12-24", "2000-12-25", 
"2000-12-30", "2000-12-31", "2001-01-06", "2001-01-07", "2001-01-14", 
"2001-01-28", "2001-09-09", "2001-09-10", "2001-09-23", "2001-09-24", 
"2001-09-30", "2001-10-01", "2001-10-07", "2001-10-08", "2001-10-14", 
"2001-10-15", "2001-10-18", "2001-10-21", "2001-10-22", "2001-10-25", 
"2001-10-28", "2001-10-29", "2001-11-04", "2001-11-05", "2001-11-11", 
"2001-11-12", "2001-11-18", "2001-11-19", "2001-11-22", "2001-11-25", 
"2001-11-26", "2001-11-29", "2001-12-02", "2001-12-03", "2001-12-09", 
"2001-12-10", "2001-12-15", "2001-12-16", "2001-12-17", "2001-12-22", 
"2001-12-23", "2001-12-29", "2001-12-30", "2002-01-06", "2002-01-07", 
"2002-01-12", "2002-01-13", "2002-01-19", "2002-01-20", "2002-01-27", 
"2002-02-03", "2002-09-05", "2002-09-08", "2002-09-09", "2002-09-15", 
"2002-09-16", "2002-09-22", "2002-09-23", "2002-09-29", "2002-09-30", 
"2002-10-06", "2002-10-07", "2002-10-13", "2002-10-14", "2002-10-20", 
"2002-10-21", "2002-10-27", "2002-10-28", "2002-11-03", "2002-11-04", 
"2002-11-10", "2002-11-11", "2002-11-17", "2002-11-18", "2002-11-24", 
"2002-11-25", "2002-11-28", "2002-12-01", "2002-12-02", "2002-12-08", 
"2002-12-09", "2002-12-15", "2002-12-16", "2002-12-21", "2002-12-22", 
"2002-12-23", "2002-12-28", "2002-12-29", "2002-12-30", "2003-01-04", 
"2003-01-05", "2003-01-11", "2003-01-12", "2003-01-19", "2003-01-26", 
"2003-09-04", "2003-09-07", "2003-09-08", "2003-09-14", "2003-09-15", 
"2003-09-21", "2003-09-22", "2003-09-28", "2003-09-29", "2003-10-05", 
"2003-10-06", "2003-10-12", "2003-10-13", "2003-10-19", "2003-10-20", 
"2003-10-26", "2003-10-27", "2003-11-02", "2003-11-03", "2003-11-09", 
"2003-11-10", "2003-11-16", "2003-11-17", "2003-11-23", "2003-11-24", 
"2003-11-27", "2003-11-30", "2003-12-01", "2003-12-07", "2003-12-08", 
"2003-12-14", "2003-12-15", "2003-12-20", "2003-12-21", "2003-12-22", 
"2003-12-27", "2003-12-28", "2004-01-03", "2004-01-04", "2004-01-10", 
"2004-01-11", "2004-01-18", "2004-02-01", "2004-09-09", "2004-09-11", 
"2004-09-12", "2004-09-13", "2004-09-19", "2004-09-20", "2004-09-26", 
"2004-09-27", "2004-10-03", "2004-10-04", "2004-10-10", "2004-10-11", 
"2004-10-17", "2004-10-18", "2004-10-24", "2004-10-25", "2004-10-31", 
"2004-11-01", "2004-11-07", "2004-11-08", "2004-11-14", "2004-11-15", 
"2004-11-21", "2004-11-22", "2004-11-25", "2004-11-28", "2004-11-29", 
"2004-12-05", "2004-12-06", "2004-12-12", "2004-12-13", "2004-12-18", 
"2004-12-19", "2004-12-20", "2004-12-24", "2004-12-25", "2004-12-26", 
"2004-12-27", "2005-01-02", "2005-01-08", "2005-01-09", "2005-01-15", 
"2005-01-16", "2005-01-23", "2005-02-06", "2005-09-08", "2005-09-11", 
"2005-09-12", "2005-09-18", "2005-09-19", "2005-09-25", "2005-09-26", 
"2005-10-02", "2005-10-03", "2005-10-09", "2005-10-10", "2005-10-16", 
"2005-10-17", "2005-10-21", "2005-10-23", "2005-10-24", "2005-10-30", 
"2005-10-31", "2005-11-06", "2005-11-07", "2005-11-13", "2005-11-14", 
"2005-11-20", "2005-11-21", "2005-11-24", "2005-11-27", "2005-11-28", 
"2005-12-04", "2005-12-05", "2005-12-11", "2005-12-12", "2005-12-17", 
"2005-12-18", "2005-12-19", "2005-12-24", "2005-12-25", "2005-12-26", 
"2005-12-31", "2006-01-01", "2006-01-07", "2006-01-08", "2006-01-14", 
"2006-01-15", "2006-01-22", "2006-02-05", "2006-09-07", "2006-09-10", 
"2006-09-11", "2006-09-17", "2006-09-18", "2006-09-24", "2006-09-25", 
"2006-10-01", "2006-10-02", "2006-10-08", "2006-10-09", "2006-10-15", 
"2006-10-16", "2006-10-22", "2006-10-23", "2006-10-29", "2006-10-30", 
"2006-11-05", "2006-11-06", "2006-11-12", "2006-11-13", "2006-11-19", 
"2006-11-20", "2006-11-23", "2006-11-26", "2006-11-27", "2006-11-30", 
"2006-12-03", "2006-12-04", "2006-12-07", "2006-12-10", "2006-12-11", 
"2006-12-14", "2006-12-16", "2006-12-17", "2006-12-18", "2006-12-21", 
"2006-12-23", "2006-12-24", "2006-12-25", "2006-12-30", "2006-12-31", 
"2007-01-06", "2007-01-07", "2007-01-13", "2007-01-14", "2007-01-21", 
"2007-02-04", "2007-09-06", "2007-09-09", "2007-09-10", "2007-09-16", 
"2007-09-17", "2007-09-23", "2007-09-24", "2007-09-30", "2007-10-01", 
"2007-10-07", "2007-10-08", "2007-10-14", "2007-10-15", "2007-10-21", 
"2007-10-22", "2007-10-28", "2007-10-29", "2007-11-04", "2007-11-05", 
"2007-11-11", "2007-11-12", "2007-11-18", "2007-11-19", "2007-11-22", 
"2007-11-25", "2007-11-26", "2007-11-29", "2007-12-02", "2007-12-03", 
"2007-12-06", "2007-12-09", "2007-12-10", "2007-12-13", "2007-12-15", 
"2007-12-16", "2007-12-17", "2007-12-20", "2007-12-22", "2007-12-23", 
"2007-12-24", "2007-12-29", "2007-12-30", "2008-01-05", "2008-01-06", 
"2008-01-12", "2008-01-13", "2008-01-20", "2008-02-03", "2008-09-04", 
"2008-09-07", "2008-09-08", "2008-09-14", "2008-09-15", "2008-09-21", 
"2008-09-22", "2008-09-28", "2008-09-29", "2008-10-05", "2008-10-06", 
"2008-10-12", "2008-10-13", "2008-10-19", "2008-10-20", "2008-10-26", 
"2008-10-27", "2008-11-02", "2008-11-03", "2008-11-06", "2008-11-09", 
"2008-11-10", "2008-11-13", "2008-11-16", "2008-11-17", "2008-11-20", 
"2008-11-23", "2008-11-24", "2008-11-27", "2008-11-30", "2008-12-01", 
"2008-12-04", "2008-12-07", "2008-12-08", "2008-12-11", "2008-12-14", 
"2008-12-15", "2008-12-18", "2008-12-20", "2008-12-21", "2008-12-22", 
"2008-12-28", "2009-01-03", "2009-01-04", "2009-01-10", "2009-01-11", 
"2009-01-18", "2009-02-01", "2009-09-10", "2009-09-13", "2009-09-14", 
"2009-09-20", "2009-09-21", "2009-09-27", "2009-09-28", "2009-10-04", 
"2009-10-05", "2009-10-11", "2009-10-12", "2009-10-18", "2009-10-19", 
"2009-10-25", "2009-10-26", "2009-11-01", "2009-11-02", "2009-11-08", 
"2009-11-09", "2009-11-12", "2009-11-15", "2009-11-16", "2009-11-19", 
"2009-11-22", "2009-11-23", "2009-11-26", "2009-11-29", "2009-11-30", 
"2009-12-03", "2009-12-06", "2009-12-07", "2009-12-10", "2009-12-13", 
"2009-12-14", "2009-12-17", "2009-12-19", "2009-12-20", "2009-12-21", 
"2009-12-25", "2009-12-27", "2009-12-28", "2010-01-03", "2010-01-09", 
"2010-01-10", "2010-01-16", "2010-01-17", "2010-01-24", "2010-02-07", 
"2010-09-09", "2010-09-12", "2010-09-13", "2010-09-19", "2010-09-20", 
"2010-09-26", "2010-09-27", "2010-10-03", "2010-10-04", "2010-10-10", 
"2010-10-11", "2010-10-17", "2010-10-18", "2010-10-24", "2010-10-25", 
"2010-10-31", "2010-11-01", "2010-11-07", "2010-11-08", "2010-11-11", 
"2010-11-14", "2010-11-15", "2010-11-18", "2010-11-21", "2010-11-22", 
"2010-11-25", "2010-11-28", "2010-11-29", "2010-12-02", "2010-12-05", 
"2010-12-06", "2010-12-09", "2010-12-12", "2010-12-13", "2010-12-16", 
"2010-12-19", "2010-12-20", "2010-12-23", "2010-12-25", "2010-12-26", 
"2010-12-27", "2010-12-28", "2011-01-02", "2011-01-08", "2011-01-09", 
"2011-01-15", "2011-01-16", "2011-01-23", "2011-02-06"), class = "factor"), 
    Season = c(1991, 1991, 1991, 1991, 1991, 1991), HomeRecord = structure(c(1L, 
    1L, 17L, 17L, 17L, 1L), .Label = c("(0-1-0)", "(0-10-0)", 
    "(0-11-0)", "(0-12-0)", "(0-13-0)", "(0-14-0)", "(0-15-0)", 
    "(0-16-0)", "(0-2-0)", "(0-3-0)", "(0-4-0)", "(0-5-0)", "(0-6-0)", 
    "(0-7-0)", "(0-8-0)", "(0-9-0)", "(1-0-0)", "(1-1-0)", "(1-10-0)", 
    "(1-10-1)", "(1-11-0)", "(1-11-1)", "(1-12-0)", "(1-13-0)", 
    "(1-14-0)", "(1-15-0)", "(1-2-0)", "(1-3-0)", "(1-4-0)", 
    "(1-5-0)", "(1-6-0)", "(1-7-0)", "(1-8-0)", "(1-8-1)", "(1-9-0)", 
    "(1-9-1)", "(10-0-0)", "(10-1-0)", "(10-2-0)", "(10-3-0)", 
    "(10-4-0)", "(10-5-0)", "(10-5-1)", "(10-6-0)", "(10-6-1)", 
    "(10-7-0)", "(10-7-1)", "(10-8-0)", "(11-0-0)", "(11-1-0)", 
    "(11-2-0)", "(11-3-0)", "(11-4-0)", "(11-5-0)", "(11-5-1)", 
    "(11-6-0)", "(11-6-1)", "(11-7-0)", "(11-7-1)", "(11-8-0)", 
    "(12-0-0)", "(12-1-0)", "(12-2-0)", "(12-3-0)", "(12-4-0)", 
    "(12-5-0)", "(12-6-0)", "(12-7-0)", "(12-8-0)", "(13-0-0)", 
    "(13-1-0)", "(13-2-0)", "(13-3-0)", "(13-4-0)", "(13-5-0)", 
    "(13-6-0)", "(14-0-0)", "(14-1-0)", "(14-2-0)", "(14-3-0)", 
    "(14-4-0)", "(14-5-0)", "(14-6-0)", "(15-0-0)", "(15-1-0)", 
    "(15-2-0)", "(15-3-0)", "(15-4-0)", "(15-5-0)", "(16-0-0)", 
    "(16-1-0)", "(16-2-0)", "(16-3-0)", "(16-4-0)", "(17-0-0)", 
    "(17-2-0)", "(18-0-0)", "(18-1-0)", "(2-0-0)", "(2-1-0)", 
    "(2-10-0)", "(2-11-0)", "(2-11-1)", "(2-12-0)", "(2-13-0)", 
    "(2-14-0)", "(2-2-0)", "(2-3-0)", "(2-4-0)", "(2-5-0)", "(2-6-0)", 
    "(2-7-0)", "(2-8-0)", "(2-9-0)", "(3-0-0)", "(3-1-0)", "(3-10-0)", 
    "(3-11-0)", "(3-11-1)", "(3-12-0)", "(3-13-0)", "(3-2-0)", 
    "(3-3-0)", "(3-4-0)", "(3-5-0)", "(3-6-0)", "(3-7-0)", "(3-8-0)", 
    "(3-9-0)", "(4-0-0)", "(4-1-0)", "(4-10-0)", "(4-11-0)", 
    "(4-11-1)", "(4-12-0)", "(4-2-0)", "(4-3-0)", "(4-4-0)", 
    "(4-5-0)", "(4-6-0)", "(4-6-1)", "(4-7-0)", "(4-7-1)", "(4-8-0)", 
    "(4-8-1)", "(4-9-0)", "(5-0-0)", "(5-1-0)", "(5-10-0)", "(5-11-0)", 
    "(5-2-0)", "(5-3-0)", "(5-3-1)", "(5-4-0)", "(5-4-1)", "(5-5-0)", 
    "(5-5-1)", "(5-6-0)", "(5-6-1)", "(5-7-0)", "(5-8-0)", "(5-8-1)", 
    "(5-9-0)", "(6-0-0)", "(6-1-0)", "(6-10-0)", "(6-2-0)", "(6-3-0)", 
    "(6-3-1)", "(6-4-0)", "(6-4-1)", "(6-5-0)", "(6-5-1)", "(6-6-0)", 
    "(6-6-1)", "(6-7-0)", "(6-7-1)", "(6-8-0)", "(6-8-1)", "(6-9-0)", 
    "(6-9-1)", "(7-0-0)", "(7-1-0)", "(7-2-0)", "(7-3-0)", "(7-3-1)", 
    "(7-4-0)", "(7-4-1)", "(7-5-0)", "(7-5-1)", "(7-6-0)", "(7-6-1)", 
    "(7-7-0)", "(7-7-1)", "(7-8-0)", "(7-9-0)", "(8-0-0)", "(8-1-0)", 
    "(8-10-0)", "(8-2-0)", "(8-3-0)", "(8-3-1)", "(8-4-0)", "(8-4-1)", 
    "(8-5-0)", "(8-5-1)", "(8-6-0)", "(8-6-1)", "(8-7-0)", "(8-7-1)", 
    "(8-8-0)", "(8-9-0)", "(9-0-0)", "(9-1-0)", "(9-2-0)", "(9-3-0)", 
    "(9-4-0)", "(9-5-0)", "(9-5-1)", "(9-6-0)", "(9-6-1)", "(9-7-0)", 
    "(9-8-0)", "(9-9-0)"), class = "factor"), AwayRecord = structure(c(17L, 
    17L, 1L, 1L, 1L, 17L), .Label = c("(0-1-0)", "(0-10-0)", 
    "(0-11-0)", "(0-12-0)", "(0-13-0)", "(0-14-0)", "(0-15-0)", 
    "(0-16-0)", "(0-2-0)", "(0-3-0)", "(0-4-0)", "(0-5-0)", "(0-6-0)", 
    "(0-7-0)", "(0-8-0)", "(0-9-0)", "(1-0-0)", "(1-1-0)", "(1-10-0)", 
    "(1-10-1)", "(1-11-0)", "(1-11-1)", "(1-12-0)", "(1-13-0)", 
    "(1-14-0)", "(1-15-0)", "(1-2-0)", "(1-3-0)", "(1-4-0)", 
    "(1-5-0)", "(1-6-0)", "(1-7-0)", "(1-8-0)", "(1-8-1)", "(1-9-0)", 
    "(1-9-1)", "(10-0-0)", "(10-1-0)", "(10-2-0)", "(10-3-0)", 
    "(10-4-0)", "(10-5-0)", "(10-5-1)", "(10-6-0)", "(10-6-1)", 
    "(10-7-0)", "(10-7-1)", "(10-8-0)", "(11-0-0)", "(11-1-0)", 
    "(11-2-0)", "(11-3-0)", "(11-4-0)", "(11-5-0)", "(11-5-1)", 
    "(11-6-0)", "(11-6-1)", "(11-7-0)", "(11-7-1)", "(11-8-0)", 
    "(12-0-0)", "(12-1-0)", "(12-2-0)", "(12-3-0)", "(12-4-0)", 
    "(12-5-0)", "(12-6-0)", "(12-7-0)", "(12-8-0)", "(13-0-0)", 
    "(13-1-0)", "(13-2-0)", "(13-3-0)", "(13-4-0)", "(13-5-0)", 
    "(13-6-0)", "(14-0-0)", "(14-1-0)", "(14-2-0)", "(14-3-0)", 
    "(14-4-0)", "(14-5-0)", "(14-6-0)", "(15-0-0)", "(15-1-0)", 
    "(15-2-0)", "(15-3-0)", "(15-4-0)", "(15-5-0)", "(16-0-0)", 
    "(16-1-0)", "(16-2-0)", "(16-3-0)", "(16-4-0)", "(17-0-0)", 
    "(17-2-0)", "(18-0-0)", "(18-1-0)", "(2-0-0)", "(2-1-0)", 
    "(2-10-0)", "(2-11-0)", "(2-11-1)", "(2-12-0)", "(2-13-0)", 
    "(2-14-0)", "(2-2-0)", "(2-3-0)", "(2-4-0)", "(2-5-0)", "(2-6-0)", 
    "(2-7-0)", "(2-8-0)", "(2-9-0)", "(3-0-0)", "(3-1-0)", "(3-10-0)", 
    "(3-11-0)", "(3-11-1)", "(3-12-0)", "(3-13-0)", "(3-2-0)", 
    "(3-3-0)", "(3-4-0)", "(3-5-0)", "(3-6-0)", "(3-7-0)", "(3-8-0)", 
    "(3-9-0)", "(4-0-0)", "(4-1-0)", "(4-10-0)", "(4-11-0)", 
    "(4-11-1)", "(4-12-0)", "(4-2-0)", "(4-3-0)", "(4-4-0)", 
    "(4-5-0)", "(4-6-0)", "(4-6-1)", "(4-7-0)", "(4-7-1)", "(4-8-0)", 
    "(4-8-1)", "(4-9-0)", "(5-0-0)", "(5-1-0)", "(5-10-0)", "(5-11-0)", 
    "(5-2-0)", "(5-3-0)", "(5-3-1)", "(5-4-0)", "(5-4-1)", "(5-5-0)", 
    "(5-5-1)", "(5-6-0)", "(5-6-1)", "(5-7-0)", "(5-8-0)", "(5-8-1)", 
    "(5-9-0)", "(6-0-0)", "(6-1-0)", "(6-10-0)", "(6-2-0)", "(6-3-0)", 
    "(6-3-1)", "(6-4-0)", "(6-4-1)", "(6-5-0)", "(6-5-1)", "(6-6-0)", 
    "(6-6-1)", "(6-7-0)", "(6-7-1)", "(6-8-0)", "(6-8-1)", "(6-9-0)", 
    "(6-9-1)", "(7-0-0)", "(7-1-0)", "(7-2-0)", "(7-3-0)", "(7-3-1)", 
    "(7-4-0)", "(7-4-1)", "(7-5-0)", "(7-5-1)", "(7-6-0)", "(7-6-1)", 
    "(7-7-0)", "(7-7-1)", "(7-8-0)", "(7-9-0)", "(8-0-0)", "(8-1-0)", 
    "(8-10-0)", "(8-2-0)", "(8-3-0)", "(8-3-1)", "(8-4-0)", "(8-4-1)", 
    "(8-5-0)", "(8-5-1)", "(8-6-0)", "(8-6-1)", "(8-7-0)", "(8-7-1)", 
    "(8-8-0)", "(8-9-0)", "(9-0-0)", "(9-1-0)", "(9-2-0)", "(9-3-0)", 
    "(9-4-0)", "(9-5-0)", "(9-5-1)", "(9-6-0)", "(9-6-1)", "(9-7-0)", 
    "(9-8-0)", "(9-9-0)"), class = "factor"), HomeFinal = c(14L, 
    14L, 45L, 35L, 10L, 7L), AwayFinal = c(26L, 24L, 0L, 31L, 
    6L, 16L), HomeLast = c(4, 4, 5, 5, 0, 7), AwayLast = c(6, 
    4, 0, 1, 6, 6), Winner = c("Away", "Away", "Home", "Home", 
    "Home", "Away")), .Names = c("X1", "HomeTeam", "AwayTeam", 
"Date", "Season", "HomeRecord", "AwayRecord", "HomeFinal", "AwayFinal", 
"HomeLast", "AwayLast", "Winner"), row.names = c(NA, 6L), class = "data.frame")

那么你就会这样做。

ggfluctuation(table(gamesWide$HomeLast, gamesWide$AwayLast), type="colour") + labs(x="Away", y="Home") + opts(title="Distribution of Last Digit of Score")

获取:
当然,该图片是使用完整数据集生成的。这应该进一步扩展到不那么对称和矩形的数据。

1
顺便提一下,这个答案也出现在“低质量帖子”审核中。我运行了?ggfluctuation中的所有示例,但没有一个看起来像热图。你能提供一个例子吗? - GSee
FYI:ggfluctuation已被弃用(最后出现在版本0.9.1中),并由ggplot2中的geom_tile替换。 - sdittmar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接