Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 731
Andrew Steele Avatar asked Dec 05 '22 08:12

Andrew Steele


2 Answers

With a log scale, x is interpreted as 0 to 1:

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)

so just make it pro-rata

enter image description here

like image 165
Troy Avatar answered Feb 16 '23 01:02

Troy


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)
like image 27
Andrew Steele Avatar answered Feb 15 '23 23:02

Andrew Steele