本学习[R博客文章展示了如何使用GGPLOT2篮球统计的热图。 完成后的热图如下所示:
我的问题(灵感杰克谁在学习[R博客文章评论)是:将有可能使用不同的渐变颜色为不同类别统计的(攻击,防御,其他)?
本学习[R博客文章展示了如何使用GGPLOT2篮球统计的热图。 完成后的热图如下所示:
我的问题(灵感杰克谁在学习[R博客文章评论)是:将有可能使用不同的渐变颜色为不同类别统计的(攻击,防御,其他)?
首先,从重建后的图形,它更新为更高版本(0.9.2.1)版的ggplot2
具有不同的主题系统及附加减少套餐:
nba <- read.csv("http://datasets.flowingdata.com/ppg2008.csv")
nba$Name <- with(nba, reorder(Name, PTS))
library("ggplot2")
library("plyr")
library("reshape2")
library("scales")
nba.m <- melt(nba)
nba.s <- ddply(nba.m, .(variable), transform,
rescale = scale(value))
ggplot(nba.s, aes(variable, Name)) +
geom_tile(aes(fill = rescale), colour = "white") +
scale_fill_gradient(low = "white", high = "steelblue") +
scale_x_discrete("", expand = c(0, 0)) +
scale_y_discrete("", expand = c(0, 0)) +
theme_grey(base_size = 9) +
theme(legend.position = "none",
axis.ticks = element_blank(),
axis.text.x = element_text(angle = 330, hjust = 0))
使用不同的渐变颜色为不同的类别是不是所有的简单。 的概念方法,向映射fill
到interaction(rescale, Category)
(其中, Category
,因为相互作用的因子和连续变量提供一个离散变量,其不工作;参见下面是进攻/防守/其他) fill
不能被映射到。
来解决这个问题的方法是人工执行此相互作用,映射rescale
到不重叠的范围为不同的值Category
,然后使用scale_fill_gradientn
到这些区域的每个映射到不同的颜色梯度。
首先创建的类别。 我想,这些地图中的那些评论,但我不知道; 改变其变量是在哪个类别是容易的。
nba.s$Category <- nba.s$variable
levels(nba.s$Category) <-
list("Offensive" = c("PTS", "FGM", "FGA", "X3PM", "X3PA", "AST"),
"Defensive" = c("DRB", "ORB", "STL"),
"Other" = c("G", "MIN", "FGP", "FTM", "FTA", "FTP", "X3PP",
"TRB", "BLK", "TO", "PF"))
因为rescale
是在数(3或4)的0时,不同的类别可以由一百偏移,以保持它们分开。 同时,确定每个色彩渐变的端点应,在这两种重新缩放值和颜色方面。
nba.s$rescaleoffset <- nba.s$rescale + 100*(as.numeric(nba.s$Category)-1)
scalerange <- range(nba.s$rescale)
gradientends <- scalerange + rep(c(0,100,200), each=2)
colorends <- c("white", "red", "white", "green", "white", "blue")
现在更换fill
可变rescaleoffset
,改变fill
规模使用scale_fill_gradientn
(记得要重新调整值):
ggplot(nba.s, aes(variable, Name)) +
geom_tile(aes(fill = rescaleoffset), colour = "white") +
scale_fill_gradientn(colours = colorends, values = rescale(gradientends)) +
scale_x_discrete("", expand = c(0, 0)) +
scale_y_discrete("", expand = c(0, 0)) +
theme_grey(base_size = 9) +
theme(legend.position = "none",
axis.ticks = element_blank(),
axis.text.x = element_text(angle = 330, hjust = 0))
重新排序,以获得相关的统计信息集中在一起是另一个应用程序reorder
上的各种变量函数:
nba.s$variable2 <- reorder(nba.s$variable, as.numeric(nba.s$Category))
ggplot(nba.s, aes(variable2, Name)) +
geom_tile(aes(fill = rescaleoffset), colour = "white") +
scale_fill_gradientn(colours = colorends, values = rescale(gradientends)) +
scale_x_discrete("", expand = c(0, 0)) +
scale_y_discrete("", expand = c(0, 0)) +
theme_grey(base_size = 9) +
theme(legend.position = "none",
axis.ticks = element_blank(),
axis.text.x = element_text(angle = 330, hjust = 0))
下面是一个使用GGPLOT2美学到两个梯度以及颜色类别映射一个简单的建议。 简单地使用的α-审美以产生梯度,并且填充审美的类别。
下面是代码这样做,重构布赖恩·迪格斯的回应:
nba <- read.csv("http://datasets.flowingdata.com/ppg2008.csv")
nba$Name <- with(nba, reorder(Name, PTS))
library("ggplot2")
library("plyr")
library("reshape2")
library("scales")
nba.m <- melt(nba)
nba.s <- ddply(nba.m, .(variable), transform,
rescale = scale(value))
nba.s$Category <- nba.s$variable
levels(nba.s$Category) <- list("Offensive" = c("PTS", "FGM", "FGA", "X3PM", "X3PA", "AST"),
"Defensive" = c("DRB", "ORB", "STL"),
"Other" = c("G", "MIN", "FGP", "FTM", "FTA", "FTP", "X3PP", "TRB", "BLK", "TO", "PF"))
然后,归一化rescale
变量到0和1之间:
nba.s$rescale = (nba.s$rescale-min(nba.s$rescale))/(max(nba.s$rescale)-min(nba.s$rescale))
而现在,做绘图:
ggplot(nba.s, aes(variable, Name)) +
geom_tile(aes(alpha = rescale, fill=Category), colour = "white") +
scale_alpha(range=c(0,1)) +
scale_x_discrete("", expand = c(0, 0)) +
scale_y_discrete("", expand = c(0, 0)) +
theme_grey(base_size = 9) +
theme(legend.position = "none",
axis.ticks = element_blank(),
axis.text.x = element_text(angle = 330, hjust = 0)) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
注意使用的alpha=rescale
使用,然后将阿尔法范围的缩放scale_alpha(range=c(0,1))
其可适合于适当地改变范围为您的曲线图。