It is possible to create inset graphs?

后端 未结 5 2038
北海茫月
北海茫月 2020-11-29 01:06

I know that when you use par( fig=c( ... ), new=T ), you can create inset graphs. However, I was wondering if it is possible to use ggplot2 library to create \

5条回答
  •  既然无缘
    2020-11-29 01:35

    Alternatively, can use the cowplot R package by Claus O. Wilke (cowplot is a powerful extension of ggplot2). The author has an example about plotting an inset inside a larger graph in this intro vignette. Here is some adapted code:

    library(cowplot)
    
    main.plot <- 
      ggplot(data = mpg, aes(x = cty, y = hwy, colour = factor(cyl))) + 
      geom_point(size = 2.5)
    
    inset.plot <- main.plot + theme(legend.position = "none")
    
    plot.with.inset <-
      ggdraw() +
      draw_plot(main.plot) +
      draw_plot(inset.plot, x = 0.07, y = .7, width = .3, height = .3)
    
    # Can save the plot with ggsave()
    ggsave(filename = "plot.with.inset.png", 
           plot = plot.with.inset,
           width = 17, 
           height = 12,
           units = "cm",
           dpi = 300)
    

提交回复
热议问题