R 绘图缩放带有正方形和线条的绘图的插入图

R 绘图缩放带有正方形和线条的绘图的插入图,r,ggplot2,R,Ggplot2,我正在尝试绘制一个缩放的插图,其中的线条和方框显示了绘图的哪个部分被缩放。 这在R中可能吗? 是我想用Python完成的一个示例。 一些示例代码: p <- qplot(1:10, 1:10) g <- p + coord_cartesian(ylim = c(8, 10), xlim = c(8, 10)) + theme(axis.title.x = element_blank(), axis.title.y = element_blank

我正在尝试绘制一个缩放的插图,其中的线条和方框显示了绘图的哪个部分被缩放。 这在R中可能吗?
是我想用Python完成的一个示例。
一些示例代码:

    p <-
  qplot(1:10, 1:10)
g <-
  p +
  coord_cartesian(ylim = c(8, 10), xlim = c(8, 10)) +
  theme(axis.title.x = element_blank(),
        axis.title.y = element_blank())
p +
  annotation_custom(
    grob = ggplotGrob(g),
    xmin = 7,
    xmax = 10,
    ymin = 1,
    ymax = 5
  )

p也许下面的代码满足了问题的要求。或者,至少,它给出了解决问题的方法

放大的区域显示为
geom\u路径
绘制矩形,连接线显示为
geom\u线
。为此,将创建两个新的数据集,提供正方形的顶点和直线的端点

polydata <- data.frame(x = c(7.9, 10.1, 10.1, 7.9, 7.9),
                       y = c(7.9, 7.9, 10.1, 10.1, 7.9))
linedata <- data.frame(x = c(7.9, 7, 10.1, 10),
                       y = c(7.9, 5, 7.9, 5),
                       id = c("a", "a", "b", "b"))
p +
  geom_path(data = polydata, aes(x, y)) +
  geom_line(data = linedata, aes(x, y, group = id),
            linetype = "solid") +
  annotation_custom(
    grob = ggplotGrob(g),
    xmin = 7,
    xmax = 10,
    ymin = 1,
    ymax = 5
  )

g <-
  p +
  coord_cartesian(ylim = c(8, 10), xlim = c(8, 10)) +
  theme(axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        plot.background = element_rect(colour = "black", fill = NA, size = 1))