Unit spheres for different distance metrics (2D) in R

Viewed 262

I am trying to plot unit spheres for different distance metrics (2D) in R.

I would like to visualize L_0.1, L_0.5, L_1, L_2, L_4, L_10, L_infinity.

I hope to do something like the following in R but with more metrics.

enter image description here

The following R code is my attempt.

################ Different metrics ##################
fun <- function(x,p=.5){ 
(abs(x)^.5)}
fun1 <- function(x,p=.5){ 
  (2-abs(x)^.5)}

fun2 <- function(x,p=.1){ 
  (abs(x)^.1)}
fun3 <- function(x,p=.1){ 
  (2-abs(x)^.1)}

fun4 <- function(x,p=1){ 
  (abs(x)^1)}
fun5 <- function(x,p=1){ 
  (2-abs(x)^1)}

fun6 <- function(x,p=2){ 
  (abs(x)^2)}
fun7 <- function(x,p=2){ 
  (2-abs(x)^2)}

fun8 <- function(x,p=4){ 
  (abs(x)^4)}
fun9 <- function(x,p=4){ 
  (2-abs(x)^4)}

fun10 <- function(x,p=10){ 
  (abs(x)^10)}
fun11 <- function(x,p=10){ 
  (2-abs(x)^10)}


plot(fun1,1,-1,ylim=c(2,-0),xlim = c(1,-1))   
plot(fun,-1,1,add=T)      
plot(fun2,-1,1,add=T)      
plot(fun3,-1,1,add=T)      
plot(fun4,-1,1,add=T)      
plot(fun5,-1,1,add=T) 
plot(fun6,-1,1,add=T)      
plot(fun7,-1,1,add=T)      
plot(fun8,-1,1,add=T)      
plot(fun9,-1,1,add=T) 
plot(fun10,-1,1,add=T)      
plot(fun11,-1,1,add=T)      
########################################################## 

I am not sure about the function I create. I mean (2-abs(x)). If you notice the original plot I attached in the question centered at zero (y-axis).

Is my code right to reproduce the same plot in the question? and how to give each metrics different colors?

1 Answers

I suggest you create some generic functions and pass them directly to a plotting function through a lapply loop:

For example you can make two generic functions for every p:

## The generic functions for every p and x
fun.generic1 <- function(p, x) {
    return(abs(x)^p)
}
fun.generic2 <- function(p, x) {
    return(2-abs(x)^p)
}

And a plotting function (using curve rather than plot to pass x as a function):

## The curve wrapping function
fun.plot <- function(p) {
    curve(fun.generic1(p, x), y = c(-1, 1), add = TRUE)
    curve(fun.generic2(p, x), y = c(-1, 1), add = TRUE)
}

You can then create a vector of all the values of p you want to plot (including Inf):

## All the p values
p_vector <- c(0.1, 0.5, 1, 2, 4, 10, Inf)

Then plot an empty plot with the x and y limits defined:

## Empty plot
plot(NULL, ylim = c(2, -0), xlim = c(1, -1), xlab = "x", ylab = "y")

And plot all the parameters using a simple lapply (the results are saved in a silent object to not jam your R console):

## Plot all the parameters
silent <- lapply(p_vector, fun.plot)

Bonus: you can also wrap all that into a single function that is more elegant:

## Wrapping function
plot.everything <- function(p_vector) {
    plot(NULL, ylim = c(2, -0), xlim = c(1, -1), xlab = "x", ylab = "y")
    lapply(p_vector, fun.plot)
    return(invisible())
} 

## Plotting the vector directly
plot.everything(c(0.1, 0.5, 1, 2, 4, 10, Inf))

example

Related