我使用 ggfluctuation 绘制具有缺失值的相关矩阵。矩阵范围从-1到+1。值由红色方块的大小表示,因此缺失值为纯灰色。
我想知道是否有办法将不同颜色的负值着色为蓝色。
这是我用过的代码
data = as.matrix(tt)
data[data == 100] = NA
cc <- matrix(data, nr=nrow(data))
ggfluctuation(as.table(cc)) + opts(legend.position="none") +
ggfluctuation(cc, type = "size", floor = 0, ceiling = max(table$freq, na.rm = TRUE))
labs(x="", y="") +
opts(axis.text.x=theme_text(size=4)) +
opts(axis.text.y=theme_text(size=4)) +
scale_x_discrete(labels=rownames(data)) +
scale_y_discrete(labels=rownames(data)) +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
scale_fill_discrete(breaks=c("trt1","ctrl","trt2"))
ggsave("cmap2sorted.pdf", width=25, height=25)
答案 0 :(得分:4)
仅供参考,在您的问题中包含实际数据是个好主意。我做了一个虚拟的4x4矩阵来演示。
我不知道在不修改ggfluctuation
函数的情况下更改填充的简单方法,因为它需要一个表,并将其修改为所需的data.frame
结构。您可以修改ggfluctuation
图以包含基于数字是正数还是负数的因子。我已从ggfluctuation
获取代码以生成ggfluctuation2
。我已经评论了变化的位置:
ggfluctuation2 <- function (table, type = "size", floor = 0, ceiling = max(table$freq,
na.rm = TRUE))
{
gg_dep("0.9.1", "ggfluctuation is deprecated.")
if (is.table(table))
table <- as.data.frame(t(table))
oldnames <- names(table)
names(table) <- c("x", "y", "result")
table <- transform(table, x = as.factor(x), y = as.factor(y),
freq = result)
if (type == "size") {
table <- transform(table, freq = sqrt(pmin(freq, ceiling)/ceiling),
border = ifelse(is.na(freq), "grey90", ifelse(freq >
ceiling, "grey30", "grey50")),
##Adding a fill factor based on negative or positive
fill = ifelse(result < 0, "negative", "positive"))
table[is.na(table$freq), "freq"] <- 1
table <- subset(table, freq * ceiling >= floor)
}
if (type == "size") {
nx <- length(levels(table$x))
ny <- length(levels(table$y))
p <- ggplot(table, aes_string(x = "x", y = "y", height = "freq",
width = "freq",
##Change fill from 'border' to 'fill'
fill = "fill")) + geom_tile(colour = "white") +
##Remove scale_fill_identity()
theme(aspect.ratio = ny/nx)
}
else {
p <- ggplot(table, aes_string(x = "x", y = "y", fill = "freq")) +
geom_tile(colour = "grey50") + scale_fill_gradient2(low = "white",
high = "darkgreen")
}
p$xlabel <- oldnames[1]
p$ylabel <- oldnames[2]
p
}
然后你可以这样做:
ggfluctuation2(as.table(cc), type = "size")
您应该能够弄清楚如何从此处进行任何其他更改