所以我想用这样的颜色可视化矩阵
library(RColorBrewer)
vec = rbinom(10000,1,0.1)
n = sum(vec)
vec = ifelse(vec == 1, rnorm(n), 0)
mat = matrix(vec,100,100)
image(t(mat)[,nrow(mat):1],
col=brewer.pal(8,"RdBu"),
xaxt= "n", yaxt= "n", frame.plot=T,
useRaster = TRUE
)
这给了我情节
但我希望颜色“以0为中心”。我的意思是我希望零值为白色,正/负值为红色/蓝色(或蓝色/红色无关紧要)。如果可能的话,有什么想法吗?
答案 0 :(得分:2)
bluered
包中的函数gplots
执行此操作。您可以将调色板设为:
library(gplots) # not to be confused with `ggplot2`, which is a very different package
color_palette <- bluered(9) # change the number to adjust how many shades of blue/red you have. Even numbers will assign white to two bins in the middle.
要强制它们在中间居中,您可以使用heatmap.2
功能,也可以使用gplots
- 只是不要让它进行任何群集:
heatmap.2(mat,
Rowv = FALSE,
Colv = FALSE,
dendrogram = 'none',
trace = 'none',
col = bluered, # this can take a function
symbreaks = TRUE, # this is the key value for symmetric breaks
)
要坚持使用image
功能,您需要手动设置中断。以下代码将为您提供:
pos_breaks <- quantile(abs(mat), probs = seq(0, 1, length.out = 5))
centered_breaks <- c(rev(-pos_breaks[-1]), pos_breaks)
答案 1 :(得分:1)
除heatmap2
之外,您可以使用pheatmap
:
library(pheatmap)
pheatmap(mat,
color = brewer.pal(7,"RdBu"),
border_color = NA,
cluster_rows = FALSE,
cluster_cols = FALSE)
如果您希望使用legend = FALSE
,也可以隐藏图例,这会产生与图像调用类似的结果,但白色为0。
答案 2 :(得分:1)
这是一个使用ggplot2
软件包进行手动缩放(Ref)的解决方案
library(RColorBrewer)
library(ggplot2)
library(reshape2)
set.seed(2020)
vec <- rbinom(10000, 1, 0.1)
n <- sum(vec)
vec <- ifelse(vec == 1, rnorm(n), 0)
mat <- matrix(vec, 100, 100)
# convert to long format
df <- melt(mat)
summary(df)
#> Var1 Var2 value
#> Min. : 1.00 Min. : 1.00 Min. :-2.916137
#> 1st Qu.: 25.75 1st Qu.: 25.75 1st Qu.: 0.000000
#> Median : 50.50 Median : 50.50 Median : 0.000000
#> Mean : 50.50 Mean : 50.50 Mean : 0.000772
#> 3rd Qu.: 75.25 3rd Qu.: 75.25 3rd Qu.: 0.000000
#> Max. :100.00 Max. :100.00 Max. : 3.214787
### default
p1 <- ggplot(df, aes(x = Var1, y = Var2, fill = value)) +
geom_raster() +
theme_minimal(base_size = 16)
重新缩放
# set the limits of the palette so that zero is in the middle of the range.
limit <- max(abs(df$value)) * c(-1, 1)
p1 +
scale_fill_distiller(palette = 'RdBu', limit = limit)
# test with the scico package
# https://github.com/thomasp85/scico
library(scico)
p1 +
scale_fill_scico(palette = "roma", limit = limit)
# test with the rcartocolor package
# https://github.com/Nowosad/rcartocolor
library(rcartocolor)
p1 +
scale_fill_carto_c(palette = 'Earth', limit = limit)
由reprex package(v0.3.0)于2020-02-07创建
答案 3 :(得分:0)
这是一个没有任何额外包的解决方案。在您的代码中,您没有将vec变量中的值分配给八个颜色区中的任何一个。您需要将vec数组切割成八个分区,然后将每个分区分配给一个颜色,然后绘制:
library(RColorBrewer)
vec = rbinom(10000,1,0.1)
n = sum(vec)
vec = ifelse(vec == 1, rnorm(n), 0)
mat = matrix(vec,100,100)
#cut the original data into 9 groups
cutcol<-cut(vec, 9)
#Create color palette with white as the center color
colorpal<-brewer.pal(8,"RdBu")
colorpal<-c(colorpal[1:4], "#FFFFFF", colorpal[5:8])
#assign the data to the 9 color groups
color<-colorpal[cutcol]
#create the color matrix to match the original data
colormat<-matrix(color,100,100)
#plot with the assigned colors
image(t(mat)[,nrow(mat):1],
col=colormat,
xaxt= "n", yaxt= "n", frame.plot=T,
useRaster = TRUE
)
#check color assignment
#hist(vec)
#hist(as.numeric(cutcol), breaks=8)