我有一个栅格地图,我想使用连续比例使用ggplot2绘制,并在其上标记为等值线.
为此,我使用直接标签包,并接近得到我想要的但我无法在同一地图上同时获得图例和标记的等值线
以下代码重现了我的问题:
install.packages(c('ggplot2', 'directlabels'))
library('ggplot2')
library('directlabels')
df <- expand.grid(x=1:100, y=1:100)
df$z <- df$x * df$y
# Plot 1: this plot is fine but without contours
p <- ggplot(aes(x=x, y=y, z=z), data = df) +
geom_raster(data=df, aes(fill=z)) +
scale_fill_gradient(limits=range(df$z), high = 'white', low = 'red')
p
# Plot 2: This plot adds the isolines but no labels and it also adds a second legend for level which I don't want
p <- p + geom_contour(aes(colour = ..level..), color='gray30', na.rm=T, show.legend=T) …Run Code Online (Sandbox Code Playgroud)