Search code examples
ggplot2mappingsurface

Surface plotting with ggplot2


Is it possible to plot with ggplot2 3D surface which is presented as (x, y, z)-vector with labeled countour lines?

Desired result is presented below Surface map with countour lines


Solution

  • This is exactly what the geomtextpath package was built for.

    Example copied from ?geomtextpath::geom_textcontour

    library(geomtextpath)
    #> Loading required package: ggplot2
    df <- expand.grid(x = seq(nrow(volcano)), y = seq(ncol(volcano)))
    df$z <- as.vector(volcano)
    
    ggplot(df, aes(x, y, z = z)) +
      geom_contour_filled(bins = 6, alpha = 0.6) +
      geom_textcontour(bins = 6, size = 2.5, padding = unit(0.05, "in")) +
      scale_fill_manual(values = terrain.colors(11)) +
      theme_classic() +
      theme(legend.position = "none")
    

    Created on 2023-01-26 with reprex v2.0.2