R plot filled.contour() output in ggpplot2

前端 未结 3 1717
南旧
南旧 2020-12-15 00:45

I want to plot this figure created with filled.contour(), but in ggplot2, how do I do this?

I want to use ggplot2 because the graphing conventions are easier. The r

相关标签:
3条回答
  • 2020-12-15 01:19

    You can tweak the colors as you need:

    gdat <- interp2xyz(fld, data.frame=TRUE)
    
    ggplot(gdat) + 
      aes(x = x, y = y, z = z, fill = z) + 
      geom_tile() + 
      coord_equal() +
      geom_contour(color = "white", alpha = 0.5) + 
      scale_fill_distiller(palette="Spectral", na.value="white") + 
      theme_bw()
    

    enter image description here

    You can reduce the pixelation at the cost of some processing time by increasing the density of the interpolation:

    fld <- with(df, interp(x = longitude, 
                           y = latitude, 
                           z = d13C,
                           xo = seq(min(longitude), max(longitude), length=400),
                           duplicate="mean"))
    

    and also reducing the bin width:

    ggplot(gdat) + 
      aes(x = x, y = y, z = z) + 
      geom_tile(aes(fill=z)) + 
      coord_equal() +
      stat_contour(aes(fill=..level..), geom="polygon", binwidth=0.005) + 
      geom_contour(color="white", alpha=0.5) +
      scale_fill_distiller(palette="Spectral", na.value="white") + 
      theme_bw()
    

    enter image description here

    NOTE: that is going to crunch for a noticeable few seconds on a decent desktop system. On my fairly beefy MacBook Pro it was:

       user  system elapsed 
      6.931   0.655   8.153 
    
    0 讨论(0)
  • 2020-12-15 01:21

    I took the example from the ggplot2 website.

     # Generate data
     library(reshape2) # for melt
     volcano3d <- melt(volcano)
     names(volcano3d) <- c("x", "y", "z")
    
     # Basic plot
     v <- ggplot(volcano3d, aes(x, y, z = z)) +  
         stat_contour(geom="polygon", aes(fill=..level..))
    

    Where x and y are your Long and Lat and z is d13C

    0 讨论(0)
  • 2020-12-15 01:23

    To follow up on @hrbrmstr's minimal example, you can also have ggplot2 compute "z" for you:

    library(ggplot2)
    ggplot(data = faithful, aes(x = eruptions, y = waiting)) +
      stat_density2d(aes(colour = ..level.., fill = ..level..), geom = "polygon")
    
    0 讨论(0)
提交回复
热议问题