geom_raster()没有填充,也没有图例

时间:2015-12-18 13:01:48

标签: r ggplot2

我想用ggplot2绘制一个像这样的混淆矩阵:

# Original data
samples <- t(rmultinom(50, size = 7, prob = rep(0.1,10)))

# Co-ocurrence matrix
coincidences <- sapply(1:ncol(samples), function(i){ colSums(samples[,i]==samples) })

如果我使用geom_roster:

p <- ggplot(melt(coincidences), aes(Var1,Var2, fill=value)) + geom_raster()

我明白了: enter image description here

我怎么能得到这个? (没有传说,没有填充) enter image description here

1 个答案:

答案 0 :(得分:8)

您应该使用scale_fill_continuous(guide = FALSE)删除图例。然后,为了摆脱所有填充(轴,标签等),您可以使用这个长theme()命令:

require(ggplot2)
# Original data
samples <- t(rmultinom(50, size = 7, prob = rep(0.1,10)))

# Co-ocurrence matrix
coincidences <- sapply(1:ncol(samples), function(i) {
  colSums(samples[,i]==samples)
})

p <- ggplot(melt(coincidences), aes(Var1, Var2, fill = value)) +
  geom_raster() +
  scale_fill_continuous(guide = FALSE) +
  theme(axis.text        = element_blank(),
        axis.ticks       = element_blank(),
        axis.title       = element_blank(),
        panel.background = element_blank())

enter image description here