Insert PNG Image Between Certain ggplot2 Axis Positions in R (Examples) | grid & patchwork Packages
How to add an image at a certain axis location in the R programming language. More details: https://statisticsglobe.com/insert-png-image-between-ggplot2-axis-positions-in-r R code of this video: set.seed(769534) # Create example data data <- data.frame(x = rnorm(100), y = rnorm(100)) install.packages("ggplot2") # Install ggplot2 package library("ggplot2") # Load ggplot2 package my_plot <- ggplot(data, aes(x, y)) + # Create example ggplot2 plot geom_point() + xlim(- 5, 5) + ylim(- 5, 5) my_plot # Draw example ggplot2 plot download.file("https://upload.wikimedia.org/wikipedia/commons/thumb/f/f3/Flag_of_Switzerland.svg/1280px-Flag_of_Switzerland.svg.png", "Switzerland.png", mode = "wb") install.packages("png") # Install & load png library("png") my_image1 <- readPNG("Switzerland.png") # Read image file as PNG install.packages("grid") # Install grid package library("grid") # Load grid my_image_mod1 <- ggplot(data, aes(x, y)) + # Modify image file geom_point(col = "white") + xlim(- 5, 5) + ylim(- 5, 5) + theme(axis.line = element_line(), axis.line.x = element_line(color = "white"), axis.title.x = element_text(color = "white"), axis.text.x = element_text(color = "white"), axis.ticks.x = element_line(color = "white"), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.border = element_blank(), panel.background = element_blank()) + annotation_custom(rasterGrob(my_image1, width = 1, height = 1), xmin = - 4.5, xmax = Inf, ymin = - Inf, ymax = Inf) my_image_mod1 my_plot_mod <- my_plot + # Modify ggplot2 plot theme(axis.title.y = element_blank(), axis.text.y = element_blank(), axis.ticks.y = element_blank()) my_plot_mod install.packages("patchwork") # Install & load patchwork package library("patchwork") my_image_mod1 + my_plot_mod # Draw image and plot side-by-side my_image_mod2 <- ggplot(data, aes(x, y)) + # Modify y-axis positions of image geom_point(col = "white") + xlim(- 5, 5) + ylim(- 5, 5) + theme(axis.line = element_line(), axis.line.x = element_line(color = "white"), axis.title.x = element_text(color = "white"), axis.text.x = element_text(color = "white"), axis.ticks.x = element_line(color = "white"), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.border = element_blank(), panel.background = element_blank()) + annotation_custom(rasterGrob(my_image1, width = 1, height = 1), xmin = - 4.5, xmax = Inf, ymin = - 2.5, ymax = 4) my_image_mod2 my_image_mod2 + my_plot_mod # Draw image and plot side-by-side download.file("https://upload.wikimedia.org/wikipedia/en/thumb/b/ba/Flag_of_Germany.svg/1280px-Flag_of_Germany.svg.png", "Germany.png", mode = "wb") my_image2 <- readPNG("Germany.png") # Read image file as PNG my_image_mod3 <- ggplot(data, aes(x, y)) + # Modify y-axis positions of image geom_point(col = "white") + xlim(- 5, 5) + ylim(- 5, 5) + theme(axis.line = element_line(), axis.line.x = element_line(color = "white"), axis.title.x = element_text(color = "white"), axis.text.x = element_text(color = "white"), axis.ticks.x = element_line(color = "white"), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.border = element_blank(), panel.background = element_blank()) + annotation_custom(rasterGrob(my_image1, width = 1, height = 1), xmin = - 4.5, xmax = Inf, ymin = - 2.5, ymax = 4) + annotation_custom(rasterGrob(my_image2, width = 1, height = 1), xmin = - 4.5, xmax = Inf, ymin = - 4.5, ymax = - 3) my_image_mod3 my_image_mod3 + my_plot_mod # Draw image and plot side-by-side Follow me on Social Media: Facebook – Statistics Globe Page: https://www.facebook.com/statisticsglobecom/ Facebook – Group for Discussions & Questions: https://www.facebook.com/groups/statisticsglobe LinkedIn – Statistics Globe Page: https://www.linkedin.com/company/statisticsglobe/ LinkedIn – Group for Discussions & Questions: https://www.linkedin.com/groups/12555223/ Twitter: https://twitter.com/JoachimSchork Music by bensound.com
Download
0 formatsNo download links available.