Skip to content

Instantly share code, notes, and snippets.

@janeshdev
Created June 13, 2013 19:15
Show Gist options
  • Save janeshdev/5776505 to your computer and use it in GitHub Desktop.
Save janeshdev/5776505 to your computer and use it in GitHub Desktop.
Create inset graphs in ggplot2
library(ggplot2)
library(grid)
# A viewport taking up a fraction of the plot area
vp <- viewport(width = 0.4, height = 0.4, x = 0.1, y = 0.9,just=c("left","top"))
b_plot <- ggplot(cars,aes(speed,dist))+geom_line()+xlim(10,15)
b_plot
# Just draw the plot twice
# First approach
png("test.png")
print(b_plot)
print(a_plot, vp = vp)
dev.off()
# Second approach
# A function to plot both graphs on the same windows
full <- function() {
print(b_plot)
theme_set(theme_bw(base_size = 8))
theme_white()
print(a_plot, vp = vp)
theme_set(theme_bw())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment