filled.contour vs. ggplot2 + stat_contour

后端 未结 1 1637
后悔当初
后悔当初 2021-01-05 04:04

I am new to ggplot2, and I am trying to replicate a graph that I created using filled.contour with ggplot2.

below is my code:

require(gg         


        
相关标签:
1条回答
  • 2021-01-05 04:44

    To create a ggplot version of the filled.contour plot you'll need to have a larger data.frame than the df object in your example and using geom_tile will produce the plot you are looking for. Consider the following:

    # a larger data set
    scl <- 10
    dat <- expand.grid(x = scl * seq(0, 1, by = 0.01), 
                       y = scl * seq(0, 1, by = 0.01))
    dat$z <- ((scl - dat$x) * dat$y) / ((scl - dat$x) * dat$y + 1)
    
    # create the plot, the geom_contour may not be needed, but I find it helpful
    ggplot(dat) + 
    aes(x = x, y = y, z = z, fill = z) + 
    geom_tile() + 
    geom_contour(color = "white", alpha = 0.5) + 
    scale_fill_gradient(low = "lightblue", high = "magenta") + 
    theme_bw()
    

    enter image description here

    0 讨论(0)
提交回复
热议问题