4

我在 x 轴上有一个对数刻度的图形。尝试创建插图不起作用,但如果将比例更改为线性,则似乎很好。有没有办法解决这个问题,还是 ggplot 的限制?

这有效:

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

这不会:

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)
4

3 回答 3

7

使用对数刻度,x 被解释为 0 到 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)

所以就按比例吧

在此处输入图像描述

于 2013-11-29T04:55:43.933 回答
6

使用对数刻度,只需使用指数而不是绝对值来指定坐标。因此,在此示例中,使用 0 < x < 1,因为比例从 1e0 运行到 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)
于 2013-11-29T09:59:05.900 回答
3

First, I also have problem using ggplot2 to draw inset plot for log scale.

However, I have done some work before using viewport from grid package.

The description of viewport:

These functions create viewports, which describe rectangular regions on a graphics device and define a number of coordinate systems within those regions.

Basically you can overlap one plot upon another and one upon another...

(1) You can uncomment the command so you can output to a png easily or use dev.copy2** etc.

(2) x,y,width,height can be specified as unit object, more info about grid::unit, click here

require(grid)
require(ggplot2)
p = qplot(1:10, 1:10, log="x")
g = qplot(0.1 , 0.1)
vp1 <- viewport(width = 0.3, 
               height = 0.3, 
               x = 0.4, 
               y = 0.7)
vp2 <- viewport(width = 0.3, 
               height = 0.3, 
               x = 0.8, 
               y = 0.3)
#png("text.png")
print(p)
print(g, vp = vp1)
print(g, vp = vp2)
#dev.off()

viewport

于 2013-11-28T17:08:42.467 回答