Tina Van Regenmortel February 2016

Creating bivariate normal distribution ellipse on plot that has log-transformed x and y axis

Although i have found many answers on how to draw a bivariate normal distribution ellipse on an existing plot, i have a question regarding plotting this ellipse onto an existing plot for which the x- and y-axis are log-transformed.

As an example I have following data to which i add an ellipse

library(mixtools)
library(truncnorm)
x<-rtruncnorm(n=100, a=0, b=20)
y=1+.3*x+.3*rnorm(100)
data<-cbind(x,y)
mu <-c(mean(x), mean(y)) 
sigma <- var(data) 
plot(data)
ellipse(mu, sigma, alpha=0.1, npoints = 200, newplot = FALSE)

enter image description here

However, my actual data requires that i use log transformed x- and y-axis, as so

plot(data,log="xy")

When plotting the "ellipse" function, i no longer get an ellipse

ellipse(mu, sigma, alpha=0.1, npoints = 200, newplot = FALSE)

enter image description here

Adding "log" to the ellipse function specifications is also no option

ellipse(mu, sigma, alpha=0.1, npoints = 200, newplot = FALSE,log="xy")

Warning message: In plot.xy(xy.coords(x, y), type = type, ...) : "log" is not a graphical parameter.

Can someone help me out with this one?

Thank you!

Answers


Roman Luštrik February 2016

Package car can handle ellipses in log-transformed space. Here is a taste of what it can do. One line is without the log argument while the almost full ellipse is with log argument.

library(car)
plot.default(data, log = "xy")
car::ellipse(center = log(mu), shape = sigma, radius = 3, center.pch = NULL)

enter image description here

Post Status

Asked in February 2016
Viewed 3,163 times
Voted 10
Answered 1 times

Search




Leave an answer