Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exporting admixture maps to .png or .pdf #17

Closed
Tom-Jenkins opened this issue May 8, 2024 · 3 comments
Closed

Exporting admixture maps to .png or .pdf #17

Tom-Jenkins opened this issue May 8, 2024 · 3 comments
Assignees

Comments

@Tom-Jenkins
Copy link
Owner

Hi Tom,

I had a follow up question for you. I have been trying different functions to export the plots to a .png. When I tried ggsave, the pies end up with the black lines delineating the percentages of admixture extending beyond the pies (see attached). Or trying to save it through Rstudio as a .png, the pies end up a bit fuzzy in larger format (see attached). I am wondering what function you use to save the plots as they look much crisper on your github.

Thanks,
Samantha

@Tom-Jenkins Tom-Jenkins self-assigned this May 8, 2024
@skannry
Copy link

skannry commented May 8, 2024

Here are the plots mentioned above and the code used to create them.

Here is the code for the first one. I used the RStudio export function to save it as a .png. When I try to enlarge the image, the clarity is relatively poor.

# Load package
library(mapmixture)
library(ggplot2)
library(tidyverse)
library(sf)

################# KLAMATH TRINITY #####
# Read in admixture file format 1
admixture1 <- read.csv("G1Lcalls_metadata_.95.csv")
#filter for basin and columns
admix <- admixture1%>%
  filter(Basin=="Klamath_River") %>%
  select(9, 3, 15, 16, 17)

# Read in coordinates file
coordinates <- read.csv("Locations.csv")
#filter for basin and desired columns
locs <- coordinates %>% 
  filter(Basin=="Klamath_River") %>% 
  select(3:5)

# Run mapmixture
map3 <- mapmixture(
  admixture_df = admix,
  coords_df = locs,
  #  basemap = rnaturalearthhires::countries10[, c("geometry")],
  cluster_cols = c("#f1a340","#998ec3","blue"),
  cluster_names = c("Winter-run","Heterozygote","Summer-run"),
  crs = 4326,
  boundary = c(xmin=-123.9, xmax=-122.3, ymin=40.7, ymax=41.8),
  pie_size = .16,
  pie_border = .2,
  pie_opacity = 1,
  land_colour = "#d9d9d9",
  sea_colour = "#deebf7",
  expand = TRUE,
  arrow = TRUE,
  arrow_size = 1.5,
  arrow_position = "bl",
  scalebar = TRUE,
  scalebar_size = 1.5,
  scalebar_position = "tl",
  plot_title = "Admixture Map",
  plot_title_size = 12,
  axis_title_size = 10,
  axis_text_size = 8
)+
  # Adjust ggplot theme options
  theme(
    axis.title = element_text(size = 10),
    axis.text = element_text(size = 8),
  )+
  # Adjust the size of the legend keys
  guides(fill = guide_legend(override.aes = list(size = 5, alpha = 1)))
klamath_map

Here is the other one. It is the same code, but instead of using RStudio to export, I used ggsave (below).

ggsave("ggsave_klamath.png",units = "in", width = 8, height = 8, dpi = 600)

ggsave_klamath

@Tom-Jenkins
Copy link
Owner Author

Tom-Jenkins commented May 15, 2024

Hi @skannry,

The reason this happens is to do with the default graphic device currently used by ggsave(), which is the AGG library (through the ragg package, as oppose to grDevices png() which uses the Cairo library. See this post for more detail.

To avoid the issue there are two options:

  1. Export your plots using grDevices::png()
png("../grDevices_klamath.png", type = "cairo", units = "in", width = 8, height = 8, res = 600)
map3
dev.off()
  1. Export your plots using ggsave() but specifically use the Cairo library
ggsave(plot = map3, filename = "../ggsave_klamath.png", device = png, type = "cairo", units = "in", width = 8, height = 8, dpi = 600)
ggsave(plot = map3, filename = "../ggsave_klamath.pdf", device = cairo_pdf, units = "in", width = 8, height = 8)

@skannry
Copy link

skannry commented May 16, 2024 via email

@Tom-Jenkins Tom-Jenkins pinned this issue May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants