r - geom_raster() 没有填充和图例

标签 r ggplot2

这个问题在这里已经有了答案:





When using ggplot in R, how do I remove margins surrounding the plot area?

(4 个回答)


5年前关闭。




我想用 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

最佳答案

您应该使用 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

关于r - geom_raster() 没有填充和图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34356329/

相关文章:

r - tidyr:Pivot_wider 用数据类型替换值

r - ggplot 在 R 中使用多个 data.frame 的 facet_wrap ?

r - R-更改ggplot2图中的颜色

r - ggplot2:从draw_panel返回值

r - 在 ggplot2 中使用填充值绘制经度纬度时出错

r - 来自all.equal.POSIXct的意外输出

r - 构建 R 包期间的警告消息 : invalid uid value replaced by that for user 'nobody'

r - 基于多个字符串的部分匹配的 R 数据帧中的子集行

r - 将日期转换为数字

r - 如何在 R 中保存图并让它们看起来很漂亮