如何在 R 中的 "stat_contour" 图中手动设置图例间隔?

How to manually set legend intervals in a "stat_contour" plot in R?

我想在 "stat_contour" 图中手动设置图例间隔,我尝试了以下代码:

library(ggplot2)
library(reshape2)
volcano3d <- melt(volcano)
names(volcano3d) <- c("x", "y", "z")

v <- ggplot(volcano3d, aes(x, y, z = z)) + stat_contour(geom="polygon", aes(fill=..level..), bins=10) + scale_fill_gradientn(name="value", colors=c("green", "blue", "yellow"),breaks=c(100,150,200))

如图所示,图例只包含150,但我想在图例中显示数字100、150、200。我想知道该怎么做?感谢您的帮助。

只需在 scale_fill_gradientn

中添加一个 limits 参数
ggplot(volcano3d, aes(x, y, z = z)) +
    stat_contour(geom="polygon", aes(fill=..level..), bins=10) +
    scale_fill_gradientn(
        name="value",
        colors=c("green", "blue", "yellow"),
        breaks=c(100,150,200),
        limits = c(100, 200))