Search code examples
rgraphggplot2insets

How can you get ggplot2 to display an inset figure when the main one has a log scale?


I have a figure with a log scale on the x-axis. Trying to create an inset figure doesn't work, but seems fine if the scale is changed to linear. Is there a way around this, or is it a limitation of ggplot?

This works:

p = qplot(1:10, 1:10)
g = ggplotGrob(qplot(1, 1))
p + annotation_custom(grob = g, xmin = 3, xmax = 6, ymin = 6, ymax = 10)

This doesn't:

p = qplot(1:10, 1:10, log='x')
g = ggplotGrob(qplot(1, 1))
p + annotation_custom(grob = g, xmin = 3, xmax = 6, ymin = 6, ymax = 10)

Solution

  • With a log scale, simply use the exponent rather than the absolute value to specify coordinates. So, in this example, use 0 < x < 1 since the scale runs from 1e0 to 1e1:

    p = qplot(1:10, 1:10, log='x')
    g = ggplotGrob(qplot(1, 1))
    p + annotation_custom(grob = g, xmin = 0.3, xmax = 0.9, ymin = 6, ymax = 10)