ggplot2热图:对类别使用不同的渐变 [英] ggplot2 heatmaps: using different gradients for categories
问题描述
对Learning R博客文章发表评论)是:是否可以针对不同类型的统计数据(攻击性,防御性和其他)使用不同的渐变颜色?
首先,从帖子重新创建图形,更新为<$ c $的较新版本(0.9.2.1)
nba< - read ggplot2
它具有不同的主题系统, .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)< - ddply(nba.m,。(variable),transform,
rescale = scale(value))
ggplot(nba.s, aes(variable,Name))+
geom_tile(aes(fill = rescale),color =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
映射到交互(rescale,Category)
(其中 Category)的概念方法是Offensive / Defensive / Other;见下文)不起作用,因为交互因子和连续变量给出了一个离散变量,
fill
不能是映射到。
解决此问题的方法是人为地执行此交互,将 rescale
映射到非重叠范围为类别
的不同值,然后使用 scale_fill_gradientn
将每个区域映射到不同的颜色渐变。
首先创建类别。我认为这些映射到评论中的那些人,但我不确定;
nba.s $类别< - nba.s $变量
级别(nba.s $ Category)< -
列表(Offensive= c(PTS,FGM,FGA,X3PM,X3PA,AST),
Defensive= c(DRB,ORB,STL),
其他= c(G,MIN,FGP,FTM,FTA ,FTP,X3PP,
TRB,BLK,TO,PF))
因为 rescale
在0的几个(3或4)之内,所以不同的类别可以偏移100以保持它们分离。同时,根据重新缩放的值和颜色,确定每个颜色渐变的端点应该在哪里。
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
变量c $ c> rescaleoffset 并更改 fill
比例以使用 scale_fill_gradientn
(记住要重新缩放
$ b $ pre $ g $ p $ ggplot(nba.s,aes(variable,Name))+
geom_tile(aes(aes fill = rescaleoffset),color =white)+
scale_fill_gradientn(colors = colorends,values = rescale(gradientends))+
scale_x_discrete(,expand = c(0,0))+
scale_y_discrete(,expand = c(0,0))+
theme_grey(base_size = 9)+
主题(le gend.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),color =white)+
scale_fill_gradientn(colors = colorends,values = scalecale(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))
This Learning R blog post shows how to make a heatmap of basketball stats using ggplot2. The finished heatmap looks like this:
My question (inspired by Jake who commented on the Learning R blog post) is: would it be possible to use different gradient colors for different categories of stats (offensive, defensive, other)?
First, recreate the graph from the post, updating it for the newer (0.9.2.1) version of ggplot2
which has a different theme system and attaches fewer packages:
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))
Using different gradient colors for different categories is not all that straightforward. The conceptual approach, to map the fill
to interaction(rescale, Category)
(where Category
is Offensive/Defensive/Other; see below) doesn't work because interacting a factor and continuous variable gives a discrete variable which fill
can not be mapped to.
The way to get around this is to artificially do this interaction, mapping rescale
to non-overlapping ranges for different values of Category
and then use scale_fill_gradientn
to map each of these regions to different color gradients.
First create the categories. I think these map to those in the comment, but I'm not sure; changing which variable is in which category is easy.
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"))
Since rescale
is within a few (3 or 4) of 0, the different categories can be offset by a hundred to keep them separate. At the same time, determine where the endpoints of each color gradient should be, in terms of both rescaled values and colors.
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")
Now replace the fill
variable with rescaleoffset
and change the fill
scale to use scale_fill_gradientn
(remembering to rescale the values):
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))
Reordering to get related stats together is another application of the reorder
function on the various variables:
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热图:对类别使用不同的渐变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!