5

I discovered few weeks ago ggforce, which has a great features to plot ellipse. But I don't manage to use it in log plots. Here is an example:

I would like to use the ellipse to circle this group

library(ggforce)
library(ggplot2)

ggplot(mtcars)+
  geom_point(aes(hp,disp))+
  geom_ellipse(aes(x0 = 230, y0 = 450, a = 80, b = 30, angle = -10))

enter image description here

But I would like to do this in a log plot. If I naively do

ggplot(mtcars)+
  geom_point(aes(hp,disp))+
  geom_ellipse(aes(x0 = 230, y0 = 450, a = 80, b = 30, angle = -10))+
  scale_y_log10()

I obtain a giant ellipse:

enter image description here

It looks like the ellipse parameters are not log transformed. I could try to reduce the parameter axis to get the good size on the log axis, something like:

ggplot(mtcars)+
  geom_point(aes(hp,disp))+
  scale_y_log10()+
  geom_ellipse(aes(x0 = 230, y0 = 450, a = 80, b = 0.05, angle =0))

which works:

enter image description here

But only if the angle is 0. If not, the two wxis are mixed and I can't get the ellipse I want:

ggplot(mtcars)+
  geom_point(aes(hp,disp))+
  scale_y_log10()+
  geom_ellipse(aes(x0 = 230, y0 = 450, a = 80, b = 0.05, angle = -10))

enter image description here

How can I plot an ellipse in a log or log-log plot in ggplot ? Is there any feasible workaround with ggforce ? Is there any other "simple" solution (other than coding the ellipse in semi-log coordinates) ?

denis
  • 5,580
  • 1
  • 13
  • 40
  • ggplot(data = mtcars,aes(hp,disp))+geom_point()+scale_y_continuous(trans = log10_trans(),breaks = c(1,10^10))+geom_ellipse(aes(x0 = 230, y0 = log10(450), a = 40, b = 12, angle = -10)) – Shirin Yavari Dec 05 '19 at 00:29
  • @ShirinYavari nope this does not work at all, it produces a graph similar to my second graph – denis Dec 05 '19 at 12:30
  • how about this one? ggplot(data = mtcars,aes(hp,disp))+geom_point()+scale_y_continuous(trans = log10_trans(),breaks = c(1,10^10))+geom_ellipse(aes(x0 = 230, y0 = log10(450), a = log10(80), b = 30, angle = -10)) – Shirin Yavari Dec 05 '19 at 17:01
  • @ShirinYavari nope. Please try your answers before letting it as a comment, and if you think you have a solution, propose an explained answer – denis Dec 06 '19 at 20:53
  • Have you seen these two questions? https://stackoverflow.com/questions/2397097/how-can-a-data-ellipse-be-superimposed-on-a-ggplot2-scatterplot https://stackoverflow.com/questions/36609476/ggplot2-draw-individual-ellipses-but-color-by-group, maybe some of it can be of help here – RLave Jan 09 '20 at 10:01
  • @RLave no I did not see these question. Super interesting, but not sure it gives an answer to the question. Thanks ! – denis Jan 09 '20 at 15:45

1 Answers1

6

What actually works for me is to transform the coordinate system instead of the y scale.

ggplot(mtcars) +
  geom_point(aes(hp,disp)) +
  geom_ellipse(aes(x0 = 230, y0 = 450, a = 80, b = 30, angle = -10)) +
  coord_trans(y = "log10")

plot with transformed coordinate system

To be honest it intuitively makes sense to me to use the coord transformation - it resembles coord_map where you're also transforming the coordinates when plotting polygons in different shapes - but I don't know enough internals to explain why scale transformation does not work.

liborm
  • 2,634
  • 20
  • 32
  • So simple, but so much working. Bravo. I let the question open a tiny moment just for possible other answers – denis Jan 09 '20 at 15:44