in Education by
I have a list of ggplots of simple ggplot line graphs based on a location. I created a list of ggplot maps to identify each of my locations and would like to overlay maps within the plot in the corner. Here is a simple example: library(ggplot2) library(rnaturalearth) library(rnaturalearthdata) library(dplyr) library(ggthemes) #create line plot x <- c(1:10) y <- c(10:19) df <- data.frame(x=x,y=y) p <- ggplot(data=df,aes(x=x,y=y))+geom_line()+ylim(0,20)+theme_bw() #create map world <- ne_countries(scale = "medium", returnclass = "sf") India <- world%>%filter(admin=='India') map <- ggplot()+ geom_sf(data=India)+ theme_map() p map And here is what I would like to get: How would I do this within R and using my two lists of ggplots? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
One approach to achieve this using ggplot2 would be via annotation_custom. To this end you have to convert your map ggplot to a grob using ggplotGrob. To position the map you have to set the coordinates as absolute values using data coordinates: library(ggplot2) library(rnaturalearth) library(rnaturalearthdata) library(dplyr) library(ggthemes) p + annotation_custom(grob = ggplotGrob(map), xmin = 7.5, xmax = 10, ymax = 10) A second approach would be to make use of patchwork::inset_element which allows to position the map via relative coordinates: library(patchwork) p + inset_element(map, left = unit(.75, "npc"), top = unit(.5, "npc"), bottom = unit(0, "npc") + unit(2, "mm"), right = unit(1, "npc") - unit(2, "mm") ) Finally, both approaches could be combined with e.g. purrr::map2 to loop over your lists of plots and corresponding maps like so: plist <- list(p, p) maplist <- list(map, map) pmaplist <- purrr::map2(plist, maplist, ~ .x + inset_element(.y, left = unit(.75, "npc"), top = unit(.5, "npc"), bottom = unit(0, "npc") + unit(2, "mm"), right = unit(1, "npc") - unit(2, "mm") ))

Related questions

0 votes
    To bind a row onto an already existing matrix, the ______ function can be used. (a) Rbind (b) Sbnd (c) ... and Out of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    I am running the code and it works ggplot(data_df, aes(x= RR, y= PPW, col = year)) + ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    I'm trying to plot a geom_rect(). Why do I receive an Error in FUN(X[[i]], ...) : ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    ________ modify a ggplot or theme object by adding on new components. (a) +.gg (b) -.gg (c) /.gg ... Data Analysis of R Programming Select the correct answer from above options...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    Which of the following creates a new ggplot plot from a data frame? (a) qplot_ggplot (b) ggplot.data. ... Analysis of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    __________ create a complete ggplot appropriate to a particular data type. (a) autoplot (b) is.ggplot (c) ... of R Programming Select the correct answer from above options...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I am not sure whether this is just a simple mistake of my code. But I just cannot figure out ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    The background purpose is that I want to divide a country into different regions. For example, divide USA ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    What is Distribution and map overlay in Google Analytics?...
asked Oct 3, 2020 in Technology by JackTerrance
0 votes
    What is Map overlay?...
asked Oct 3, 2020 in Technology by JackTerrance
0 votes
    The reason I ask is that Stack Overflow has been Slashdotted, and Redditted. First, what kinds of effect ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 1, 2022 in Education by JackTerrance
0 votes
    It is a peripheral device which will print anything created on a computer onto a paper, whether it be text or ...  Projector  Monitor Select the correct answer from above options...
asked Dec 24, 2021 in Education by JackTerrance
0 votes
    io 3. When you switch on the computer, the Windows OS gets loaded onto the computer's memory, this process is ... (d) None of these Select the correct answer from above options...
asked Nov 30, 2021 in Education by JackTerrance
0 votes
0 votes
    What is the command used to copy data from the local system onto HDFS?...
asked Aug 5, 2021 in Technology by JackTerrance
...