Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 205399

Plot some points in contour curve from ggplot2

$
0
0

I'd like to plot a specific number of points of z in the contour curve, for example, 8 or 10 points. Below I show an example, but with all points.

library(ggplot2)
library(tidyverse)

rosenbrock <- function(x){
    d <- length(x)
    out <- 0
    for(i in 1 : (d - 1)){
        out <- out + 100 * ( x[i]^2 - x[i + 1] )^2 + (x[i] - 1)^2
    }
    out
}

set.seed(2020)
coord <- matrix(runif(2000, -100, 100), ncol = 2)
graph <- apply(coord, 1, rosenbrock)    

results <- data.frame(x = coord[, 1], y = coord[, 2], z = graph)
results <- results %>% arrange(desc(z))    

results %>% 
    ggplot(aes(x = x, y = y, z = z)) + 
        geom_point(aes(colour = z)) + 
        stat_density2d() +
        theme_light()

Viewing all articles
Browse latest Browse all 205399

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>