# inset plot with enlarged detail from a region of the main plot
library(tibble)
p <-
ggplot(data = mtcars, mapping = aes(wt, mpg)) +
geom_point()
df <- tibble(x = 0.01,
y = 0.01,
plot = list(p +
coord_cartesian(xlim = c(3, 4),
ylim = c(13, 16)) +
labs(x = NULL, y = NULL) +
theme_bw(10)))
p +
expand_limits(x = 0, y = 0) +
geom_plot_npc(data = df,
aes(npcx = x, npcy = y, label = plot))
p +
expand_limits(x = 0, y = 0) +
geom_plot_npc(data = df,
aes(npcx = x, npcy = y, label = plot,
vp.width = 1/2, vp.height = 1/4))
p +
expand_limits(x = 0, y = 0) +
geom_plot_npc(data = df,
aes(npcx = x, npcy = y, label = plot),
vp.width = 1/4, vp.height = 1/4)
p +
geom_plot(data = df,
aes(x = x + 3, y = y + 20, label = plot),
nudge_x = -1, nudge_y = - 7,
hjust = 0.5, vjust = 0.5,
arrow = arrow(length = unit(0.5, "lines")),
colour = "red",
vp.width = 1/5, vp.height = 1/5)
Run the code above in your browser using DataLab