ggplotFixedEffects <- function()

in modelVisualizeR/R/ggplotWrappers.R [70:107]


ggplotFixedEffects <- function(model_effects, shp, censusdb = NULL, title='', shape_level = 'residence_census_tract'){
  
  plotDatEffects <- right_join(model_effects$modeledData,shp, by=shape_level)
  plotDatEffects$positive[plotDatEffects$n==0]<-NaN
  
  
  #convert to fraction 
  if(!is.null(censusdb)){
    plotDatEffects$positive <- plotDatEffects$positive / censusdb$observedData$P005004.2010
    plotDatEffects$modeled_count_mode <- plotDatEffects$modeled_count_mode / censusdb$observedData$P005004.2010
  }
  
  bbox<-sf::st_bbox(shp$geometry)
  
  mapSettings <- ggplot() + #xlim(c(min(122.5, -bbox[1]),max(121.7,-bbox[3]))) + ylim(c(max(47.17,bbox[2]),min(47.76,bbox[4]))) +
    theme_bw() +
    theme(axis.text=element_blank(),axis.ticks=element_blank(),panel.grid.major=element_line(colour="transparent"), panel.border = element_blank())
  p<-mapSettings + geom_sf(data=shp,size=0.1,aes(fill=NaN))
  
  # mapSettingsTight <- ggplot() + xlim(c(122.5,122.15)) + ylim(c(47.42,47.76)) +  theme_bw() +
  #   theme(axis.text=element_blank(),axis.ticks=element_blank(),panel.grid.major=element_line(colour="transparent"), panel.border = element_blank())
  # p<-mapSettingsTight + geom_sf(data=shp,size=0.1,aes(fill=NaN))
  
  colorLimits<-c(min(c(plotDatEffects$positive,plotDatEffects$modeled_count_mode),na.rm=TRUE),max(c(plotDatEffects$positive,plotDatEffects$modeled_count_mode),na.rm=TRUE))
  colorBreaks<-round(seq(min(colorLimits),sqrt(max(colorLimits)), length.out = 6)^2)
  colorBreaks<-seq(min(colorLimits),sqrt(max(colorLimits)), length.out = 6)^2
  
  p1 <- p + geom_sf(data=plotDatEffects,size=0, aes(fill=num_bing_queries))  +
    guides(fill=guide_legend(title="num_bing_queries")) +
    scale_fill_viridis(na.value="transparent",trans = "sqrt",breaks=colorBreaks,limits=colorLimits)
  
  p2 <- p + geom_sf(data=plotDatEffects,size=0, aes(fill=modeled_count_mode))  +
    guides(fill=guide_legend(title="Expected")) +
    scale_fill_viridis(na.value="transparent",trans = "sqrt",breaks=colorBreaks,limits=colorLimits)
  
  grid.arrange(p1,p2,nrow=1, top=textGrob(title))
  
}