dipetkov / eems

Estimating Effective Migration Surfaces
GNU General Public License v2.0
102 stars 28 forks source link

Cropping issue with ggplot maps #57

Closed alex-sandercock closed 2 years ago

alex-sandercock commented 2 years ago

Hi,

I am trying to add a map of the eastern united states with the state lines drawn. It seems like ggplot is only interpreting the coordinates used for the habitat boundary, and cropping out the additional points needed for the full map. The plot object was made using reemsplots2.

Do you have a suggestion for fixing the cropping issues within ggplot or maybe for base R plotting? I attached my R code and output figure. Thank you for your help!

library(ggplot2) library('maps') US <- map_data("state")

plots_500.1$mrates01 + geom_polygon(data=US, aes(x=long, y=lat, group=group),color="black", fill=NA) + xlab("Longitude") + ylab("Latitude") + theme( axis.text.y = element_text(colour = "black",size=10), axis.text.x = element_text(colour = "black",size = 10), panel.border = element_rect(colour = "black", fill=NA, size=0.5), panel.background = element_blank() ) + coord_quickmap()

pruned_deme500 1

dipetkov commented 2 years ago

Hello I agree that the straight segments to the left and on the bottom look "wrong". Which states do you want to have in the plot? Is it possible to share the plots_500.1 eems-generated as well?

alex-sandercock commented 2 years ago

I suppose all of the states east of longitude -94. I attached a figure below of what I was hoping to be able to layer the mrates01 plot on top of.

Is there an email address or link where I can send you the plots_500.1 file directly?

image

dipetkov commented 2 years ago

I think the way to go is do the cropping beforehand of the US states map.

desislavka@gmail.com

alex-sandercock commented 2 years ago

I sent the files. I'll try to crop the map beforehand and then add in the mrates01 plot.

dipetkov commented 2 years ago

Here is one way to fix the cropping: select the states east of longitude -94 and increase the margin.

US <- map_data("state")

states <- c(
  "alabama",
  "connecticut",
  "delaware",
  "district of columbia",
  "georgia",
  "indiana",
  "kentucky",
  "maine",
  "maryland",
  "massachusetts",
  "michigan",
  "mississippi",
  "new hampshire",
  "new jersey",
  "new york",
  "north carolina",
  "ohio",
  "pennsylvania",
  "rhode island",
  "south carolina",
  "tennessee",
  "vermont",
  "virginia",
  "west virginia"
)

US_east_of_94 <- US %>%
  filter(region %in% states)

plots_500.1$mrates01 +
  geom_polygon(
    data = US_east_of_94,
    aes(x = long, y = lat, group = group),
    color = "black", fill = NA
  ) +
  xlab("Longitude") +
  ylab("Latitude") +
  theme(
    axis.text.y = element_text(colour = "black", size = 10),
    axis.text.x = element_text(colour = "black", size = 10),
    panel.border = element_rect(colour = "black", fill = NA, size = 0.5),
    panel.background = element_blank()
  ) +
  scale_x_continuous(
    limits = c(-95, -66)
  ) +
  coord_quickmap()

no-cropping

alex-sandercock commented 2 years ago

This is much better. I'll see if I can get those other states added and then cropped out, but I think this will still work if I can't get the other states in. Thank you.

dipetkov commented 2 years ago

And keep in mind that you can avoid the cropping by expanding the limits with scale_x_continuous (longitude) and scale_y_continuous (latitude).

The cropping happens because ggplot determines the limits from plots_500.1$mrates01. Next when the map is added on top there might not be enough space for all the points in the map. Hence -- cropping.

alex-sandercock commented 2 years ago

This fixed it! I expanded the limits manually and then cropped it in coord_quickmap.

plots_500.1$mrates01 + geom_polygon(data=US, aes(x=long, y=lat, group=group),color="black", fill=NA) + xlab("Longitude") + ylab("Latitude") + scale_x_continuous(limits = c(-100,-60)) + scale_y_continuous(limits =c(25,48)) + theme( axis.text.y = element_text(colour = "black",size=10), axis.text.x = element_text(colour = "black",size = 10), panel.border = element_rect(colour = "black", fill=NA, size=0.5), panel.background = element_blank() ) + coord_quickmap(ylim=c(30,45),xlim=c(-95,-65))

alexkrohn commented 1 year ago

@alex-sandercock I would love to be able to generate the mrates raster plot as something plottable in ggplot. Can you explain how you made plots_500.1? Thanks!

alex-sandercock commented 1 year ago

@alex-sandercock I would love to be able to generate the mrates raster plot as something plottable in ggplot. Can you explain how you made plots_500.1? Thanks!

I used the reemsplots2 package in R after running EEMS. You can add ggplot parameters to the specific plots within the object that you call. Ex:

library("reemsplots2")
library("ggplot2")

mcmcpath = c("./parameter_hyperparameter_500deme_20m_iter_adjust_pruned-chain1.1","./parameter_hyperparameter_500deme_20m_iter_adjust_pruned-chain2.1","./parameter_hyperparameter_500deme_20m_iter_adjust_pruned-chain3.1")

plots_500.1 <- make_eems_plots(mcmcpath, longlat = FALSE)

plots_500.1$mrates01 +
ggplotstuff() +
ggplotstuff2 +
theme()+
coord_quickmap()
alexkrohn commented 1 year ago

Ah! Brilliant! Thanks for that! So simple too!