2

I'm trying to plot time series data for each "Type" in a dataset, and I want to include an inset of a zoomed in portion of the data for each plot. I can get the inset to work on each individual plot, but the issue comes when I try to use faceting to produce all of the plots at once.

The method I used to add the inset plot is based on this answer: https://stackoverflow.com/a/55635330/11985177

And the method I used to facet the plots across multiple pages in a pdf is based on this answer: https://stackoverflow.com/a/48544261/11985177

require(ggplot2)
require(ggforce)
require(ggpmisc)

#create example dataframe
exampledf<- data.frame("Time"= rep(1:50, 10), "Type"= rep(1:10, each=50), "Var1"= rnorm(1000), "Var2"= rnorm(1000))


graph_inset_facet<- function(df){

  mainplot<- ggplot(df,
                 aes(x=Time, y=value))+
    geom_point(aes(y=Var1, color= "Var1"))+
    geom_point(aes(y= Var2, color="Var2"))


  insetdf<-tibble(x = 0.9, y = 0.9,
              plot = list(mainplot +
                            coord_cartesian(xlim = c(40, 45)) +
                            labs(x = NULL, y = NULL)))

  mainplot +
    expand_limits(x = 0, y = 0) +
    geom_plot_npc(data = insetdf, aes(npcx = x, npcy = y, label = plot))

  #wd is working directory
  pdf(file =  paste(wd, "facet inset.pdf"))
  for(i in 1:2){
    print(mainplot+
            facet_grid_paginate(df$Type, nrow = 5, ncol = 1, page = i))

  }
  dev.off()
}

This code does print a pdf with the main plots faceted by "Type", but the inset plots don't show up. I tried running the code with the expand_limits and geom_plot_npc functions inside the print for loop, but the inset graphs still didn't show up.

GeochemKat
  • 21
  • 1
  • 1
    You are not assigning when running inset plot, so pdf plot uses first assignment of *mainplot* at top of function. Before pdf build, adjust line to `mainplot <- mainplot + expand_limit(...) + geom_plot_npc(...)` – Parfait Aug 27 '19 at 18:10
  • Unfortunately, that didn't work. When I tried it, I got this error: Error in `$<-.data.frame`(`*tmp*`, "PANEL", value = c(1L, 1L, 1L, 1L, : replacement has 1000 rows, data has 1 – GeochemKat Aug 27 '19 at 18:27
  • Outside of PDF, do the insets ever appear? – Parfait Aug 27 '19 at 18:35
  • Yes, the code for the insets does work, but only if I don't use faceting. – GeochemKat Aug 27 '19 at 18:57
  • I would start by making the code work without pagination, using facet_wrap() or facet_grid(). You are passing new data for the geom_plot_npc(), the plot you are saving in insetdf is not faceted, and insetdf has no column called type. So once you fix the assignment to mainplot as told in the first comment above, and replace df$Type by Type in the call to facet_wrap() (this is what I tried) each panel gets an inset, but the same inset. To get a different inset plot you need to supply them as separate rows in insetdf and add a column Type in insetdf. – Pedro J. Aphalo Sep 04 '19 at 22:06
  • @PedroAphalo thanks for your response. Could you please show me what your code looked like when you were done? I'm not sure where to start with adding the insets as separate rows or adding a Type column to insetdf. – GeochemKat Sep 09 '19 at 16:43
  • @PedroAphalo, I was able to figure out how to add the other column, so thank you! Your comment was very helpful and everything worked. I was even able to switch back to the facet_grid_paginate function without any problems. – GeochemKat Sep 10 '19 at 14:04
  • @GeochemKat It would be useful to others if you post as an answer a working code example. (In cases like this the best practice is to provide a working answer to one's own question.) – Pedro J. Aphalo Sep 13 '19 at 08:24

0 Answers0