1

I used ggfluctuation to plot a correlation matrix with missing values. the matrix range is from -1 to +1. the values are represented by the size of red square so missing values are plain gray.

I wonder if there is a way to color negative values in different color say blue.

enter image description here

and here is the code I have used

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)
4

1 回答 1

4

仅供参考,在您的问题中包含实际数据是个好主意。我制作了一个虚拟的 4x4 矩阵来演示。

我不知道在不修改ggfluctuation函数的情况下更改填充的简单方法,因为它需要一个表格,并将其修改为所需的data.frame结构。ggfluctuation您可以根据数字是正数还是负数来修改绘图以包含一个因子。我已经从ggfluctuationmake中获取了代码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")

在此处输入图像描述

您应该能够从这里弄清楚如何进行任何其他更改

于 2013-06-15T03:14:16.957 回答