【问题标题】:How to modify the legend of a heat map generated with ggplot's geom_tile?如何修改 ggplot 的 geom_tile 生成的热图的图例?
【发布时间】:2012-04-15 07:25:23
【问题描述】:

有没有办法修改使用 ggplot2 包中的geom_tile 生成的热图的图例?我想增加图例中的图块数量,并在那里设置显示值的最小值和最大值。

在手册页的这个示例中,图例包含五个彩色图块,代表从 -0.4 到 0.4 的值。我怎么能让例如改为显示 9 块?

library (ggplot2)

pp <- function (n,r=4) {
   x <- seq(-r*pi, r*pi, len=n)
   df <- expand.grid(x=x, y=x)
   df$r <- sqrt(df$x^2 + df$y^2)
   df$z <- cos(df$r^2)*exp(-df$r/6)
   df
}

p <- ggplot(pp(20), aes(x=x,y=y))
p + geom_tile(aes(fill=z))

【问题讨论】:

  • 一个可重现的例子可以帮助回答你的问题。
  • 感谢您的提示。我添加了一个示例。

标签: r data-visualization ggplot2


【解决方案1】:

我想有几种可能的方法来归档它。一种解决方案是手动指定图例的中断。

d = pp(20)
ggplot(d, aes(x=x,y=y,fill=z)) + geom_tile() + 
    scale_fill_continuous( breaks = round( seq(-.4, .4, length.out = 10 ), 1) )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-11
    • 2021-04-25
    • 1970-01-01
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    • 2019-02-11
    • 1970-01-01
    相关资源
    最近更新 更多