rggplot2data-visualizationpareto-chartpareto-optimality

ggplot display Pareto front only for the points that belong to a specific level


Libraries being used

install.packages("rPref")
install.packages("dplyr")
install.packages("igraph")
install.packages("ggplot") 

Taking mtcars dataset from R as an example.

In order to find the Pareto frontier and level value one establish the preferences

p <- high(mpg) * high(hp) 

Then calculate the level-value w.r.t. p by using top-all

res <- psel(mtcars, p, top = nrow(mtcars))

When one creates the visualization as follows

gp <- ggplot(res, aes(x = mpg, y = hp, color = factor(.level))) + geom_point(size = 3)

It will show a plot with the mpg and hp values of mtcars with colors, and each of the color represents the level (proximity to optimum). As follows

enter image description here

If one wants to plot the Pareto front line for every area of all points of a lower level, one can run

gp + geom_step(direction = "vh") 

That results in

enter image description here

However, as for one's case only the 1st level is relevant, how does one display only the dots and the specific pareto front for level 1?

Changing the factor(.level) to factor(1) as

gp <- ggplot(res, aes(x = mpg, y = hp, color = factor(1))) + geom_point(size = 3)

Gives the following, which is ignoring the previous levels but some that would not belong to Level 1 seem to be featured in it

enter image description here


Solution

  • Is this what you're looking for?

    data(mtcars)
    p <- high(mtcars$mpg) * high(mtcars$hp) 
    
    res <- psel(mtcars, p, top = nrow(mtcars))
    res1 <- res %>% filter(.level == "1")
    gp <- ggplot() + 
      geom_point(data = res, aes(x = mpg, y = hp, color = factor(.level)), size = 3) + 
      geom_step(data = res1 , aes(x=mpg, y=hp, color=factor(.level)))
    
    

    enter image description here


    Edit to resolve comments

    Here's the same graph without the other points:

    res %>% filter(.level == "1") %>% 
      ggplot() + 
        geom_point(aes(x = mpg, y = hp), size = 3) + 
        geom_step(aes(x=mpg, y=hp))
    

    enter image description here