From 78f25d874f928799b679403ce3eabfcd482e2334 Mon Sep 17 00:00:00 2001 From: Laura DeCicco Date: Thu, 13 Apr 2017 15:24:35 -0500 Subject: [PATCH 1/2] Cleaned up map --- R/makeMap.R | 44 +++++++++++++++++-------------------------- inst/shiny/mapStuff.R | 37 ++++++++++++++---------------------- man/getMapInfo.Rd | 4 +--- 3 files changed, 32 insertions(+), 53 deletions(-) diff --git a/R/makeMap.R b/R/makeMap.R index de11ef5d..3b984e21 100644 --- a/R/makeMap.R +++ b/R/makeMap.R @@ -6,7 +6,6 @@ #' @param category either "Biological", "Chemical Class", or "Chemical" #' @param chem_site data frame with at least columns SiteID, site_grouping, and Short Name #' @param mean_logic logical \code{TRUE} is mean, \code{FALSE} is maximum -#' @param hit_threshold numeric threshold defining a "hit" #' @export #' @import ggplot2 #' @importFrom stats median @@ -37,8 +36,7 @@ getMapInfo <- function(chemicalSummary, chem_site, category = "Biological", - mean_logic = FALSE, - hit_threshold = 0.1){ + mean_logic = FALSE){ match.arg(category, c("Biological","Chemical Class","Chemical")) @@ -54,42 +52,34 @@ getMapInfo <- function(chemicalSummary, typeWords <- "chemical classes" } - statsOfGroupOrdered <- statsOfGroup(chemicalSummary = chemicalSummary, - category = category, - hit_threshold = hit_threshold) - sumStat <- statsOfColumns(chemicalSummary = chemicalSummary, - category = category, - hit_threshold = hit_threshold, - mean_logic = mean_logic) + mapData <- chem_site[,c("Short Name", "dec_lat", "dec_lon", "SiteID")] - mapData <- left_join(sumStat, distinct(select(statsOfGroupOrdered, site, nSamples)), by="site") - mapData <- left_join(mapData, chem_site[,c("Short Name", "dec_lat", "dec_lon")], by=c("site"="Short Name")) + nSamples <- select(chemicalSummary,site,date) %>% + distinct() %>% + group_by(site) %>% + summarize(count = n()) - col_types <- c("darkblue","dodgerblue","green4","gold1","orange","brown","red") + meanStuff <- graphData(chemicalSummary = chemicalSummary, + category = category, mean_logic = mean_logic) %>% + group_by(site) %>% + summarize(meanMax = max(meanEAR)) %>% + left_join(nSamples, by="site") - earCols <- grep("EAR", names(mapData)) + mapData <- left_join(mapData, meanStuff, by=c("SiteID"="site")) - if(length(earCols) > 1){ - if(mean_logic){ - mapData$meanMax <- rowMeans(mapData[,earCols], na.rm = TRUE) - } else { - mapData$meanMax <- apply(mapData[,earCols], 1, function(x) max(x, na.rm = TRUE)) - } - } else { - mapData$meanMax <- mapData[,earCols] - } + col_types <- c("darkblue","dodgerblue","green4","gold1","orange","brown","red") - counts <- mapData$nSamples + counts <- mapData$count if(length(siteToFind) > 1){ leg_vals <- unique(as.numeric(quantile(mapData$meanMax, probs=c(0,0.01,0.1,0.25,0.5,0.75,0.9,.99,1), na.rm=TRUE))) pal = colorBin(col_types, mapData$meanMax, bins = leg_vals) rad <-3*seq(1,4,length.out = 16) - if(sum(mapData$nSamples) == 0){ + if(sum(mapData$count, na.rm = TRUE) == 0){ mapData$sizes <- rad[1] } else { - mapData$sizes <- rad[as.numeric(cut(mapData$nSamples, breaks=16))] + mapData$sizes <- rad[as.numeric(cut(mapData$count, breaks=16))] } } else { @@ -100,4 +90,4 @@ getMapInfo <- function(chemicalSummary, return(list(mapData=mapData, pal=pal)) -} \ No newline at end of file +} diff --git a/inst/shiny/mapStuff.R b/inst/shiny/mapStuff.R index aee58978..64941254 100644 --- a/inst/shiny/mapStuff.R +++ b/inst/shiny/mapStuff.R @@ -15,29 +15,15 @@ output$mapFooter <- renderUI({ ) chemicalSummary <- chemicalSummary() - catType = as.numeric(input$radioMaxGroup) - meanEARlogic <- as.logical(input$meanEAR) - hit_threshold <- hitThresValue() - statsOfGroupOrdered <- statsOfGroup(chemicalSummary = chemicalSummary, - category = c("Biological","Chemical","Chemical Class")[catType], - hit_threshold = hit_threshold) - statsOfGroupOrdered <- statsOfGroupOrdered %>% + nSamples <- select(chemicalSummary,site,date) %>% + distinct() %>% group_by(site) %>% - summarize(nSamples = median(nSamples, na.rm = TRUE)) - - if(input$radioMaxGroup == "1"){ - word <- "groups" - } else if (input$radioMaxGroup == "2"){ - word <- "chemicals" - } else { - word <- "classes" - } + summarize(count = n()) - HTML(paste0("
Size range represents number of ",word, - " with hits. Ranges from ", min(statsOfGroupOrdered$nSamples,na.rm = TRUE), + HTML(paste0("
Size range represents number of samples. Ranges from ", min(nSamples$count,na.rm = TRUE), " - ", - max(statsOfGroupOrdered$nSamples,na.rm = TRUE),"
")) + max(nSamples$count,na.rm = TRUE),"")) }) @@ -62,8 +48,7 @@ observe({ mapDataList <- getMapInfo(chemicalSummary, chem_site = chem_site, category = c("Biological","Chemical","Chemical Class")[catType], - mean_logic = meanEARlogic, - hit_threshold = hitThresValue()) + mean_logic = meanEARlogic) mapData <- mapDataList$mapData pal <- mapDataList$pal @@ -71,7 +56,7 @@ observe({ if(length(siteToFind) == 1){ mapData <- filter(chem_site, SiteID == siteToFind) %>% - mutate(nSamples = median(mapData$nSamples), + mutate(nSamples = median(mapData$count), meanMax = median(mapData$meanMax), sizes = median(mapData$sizes)) } @@ -79,10 +64,16 @@ observe({ map <- leafletProxy("mymap", data=mapData) %>% clearMarkers() %>% clearControls() %>% + setView(lng = mean(mapData$dec_lon, na.rm = TRUE), + lat = mean(mapData$dec_lat, na.rm = TRUE), zoom=6) %>% + fitBounds(lng1 = min(mapData$dec_lon, na.rm = TRUE), + lat1 = min(mapData$dec_lat, na.rm = TRUE), + lng2 = max(mapData$dec_lon, na.rm = TRUE), + lat2 = max(mapData$dec_lat, na.rm = TRUE)) %>% addCircleMarkers(lat=~dec_lat, lng=~dec_lon, popup=paste0('',mapData$site,"
", "', - "', + "', # "', # "', '
",maxEARWords,": ",sprintf("%.1f",mapData$meanMax),'
Number of Samples: ",mapData$nSamples,'
Number of Samples: ",mapData$count,'
Frequency: ",sprintf("%.1f",mapData$freq),'
Number of ",typeWords," with hits: ",counts,'
') , diff --git a/man/getMapInfo.Rd b/man/getMapInfo.Rd index 9bd21edb..f6e98ad4 100644 --- a/man/getMapInfo.Rd +++ b/man/getMapInfo.Rd @@ -5,7 +5,7 @@ \title{getMapInfo} \usage{ getMapInfo(chemicalSummary, chem_site, category = "Biological", - mean_logic = FALSE, hit_threshold = 0.1) + mean_logic = FALSE) } \arguments{ \item{chemicalSummary}{data frame from \code{get_chemical_summary}} @@ -15,8 +15,6 @@ getMapInfo(chemicalSummary, chem_site, category = "Biological", \item{category}{either "Biological", "Chemical Class", or "Chemical"} \item{mean_logic}{logical \code{TRUE} is mean, \code{FALSE} is maximum} - -\item{hit_threshold}{numeric threshold defining a "hit"} } \description{ getMapInfo From 7002b7b5ffaca76f817ea0cc976ba1d99752176c Mon Sep 17 00:00:00 2001 From: Laura DeCicco Date: Thu, 13 Apr 2017 15:36:08 -0500 Subject: [PATCH 2/2] Forgot to import n --- NAMESPACE | 1 + R/makeMap.R | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/NAMESPACE b/NAMESPACE index 769d9ed6..55b3e5b5 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -46,6 +46,7 @@ importFrom(dplyr,group_by_) importFrom(dplyr,left_join) importFrom(dplyr,mutate) importFrom(dplyr,mutate_) +importFrom(dplyr,n) importFrom(dplyr,rename) importFrom(dplyr,rename_) importFrom(dplyr,right_join) diff --git a/R/makeMap.R b/R/makeMap.R index 3b984e21..10e94e8d 100644 --- a/R/makeMap.R +++ b/R/makeMap.R @@ -9,6 +9,7 @@ #' @export #' @import ggplot2 #' @importFrom stats median +#' @importFrom dplyr n #' @importFrom grDevices colorRampPalette #' @importFrom leaflet colorBin #' @importFrom dplyr full_join filter mutate select left_join right_join @@ -40,7 +41,7 @@ getMapInfo <- function(chemicalSummary, match.arg(category, c("Biological","Chemical Class","Chemical")) - site <- nSamples <- `Short Name` <- Fullname <- dec_lat <- dec_lon <- ".dplyr" + site <- meanEAR <- nSamples <- `Short Name` <- Fullname <- dec_lat <- dec_lon <- ".dplyr" siteToFind <- chem_site$`Short Name`