Skip to content

Commit

Permalink
Merge pull request #178 from ldecicco-USGS/master
Browse files Browse the repository at this point in the history
Cleaned up map
  • Loading branch information
ldecicco-USGS authored Apr 13, 2017
2 parents 3036bf8 + 7002b7b commit f9c09c9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 54 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
47 changes: 19 additions & 28 deletions R/makeMap.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
#' @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
#' @importFrom dplyr n
#' @importFrom grDevices colorRampPalette
#' @importFrom leaflet colorBin
#' @importFrom dplyr full_join filter mutate select left_join right_join
Expand Down Expand Up @@ -37,12 +37,11 @@
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"))

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`

Expand All @@ -54,42 +53,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 {
Expand All @@ -100,4 +91,4 @@ getMapInfo <- function(chemicalSummary,

return(list(mapData=mapData, pal=pal))

}
}
37 changes: 14 additions & 23 deletions inst/shiny/mapStuff.R
Original file line number Diff line number Diff line change
Expand Up @@ -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("<h5>Size range represents number of ",word,
" with hits. Ranges from ", min(statsOfGroupOrdered$nSamples,na.rm = TRUE),
HTML(paste0("<h5>Size range represents number of samples. Ranges from ", min(nSamples$count,na.rm = TRUE),
" - ",
max(statsOfGroupOrdered$nSamples,na.rm = TRUE),"</h5>"))
max(nSamples$count,na.rm = TRUE),"</h5>"))

})

Expand All @@ -62,27 +48,32 @@ 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

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))
}

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('<b>',mapData$site,"</b><br/><table>",
"<tr><td>",maxEARWords,": </td><td>",sprintf("%.1f",mapData$meanMax),'</td></tr>',
"<tr><td>Number of Samples: </td><td>",mapData$nSamples,'</td></tr>',
"<tr><td>Number of Samples: </td><td>",mapData$count,'</td></tr>',
# "<tr><td>Frequency: </td><td>",sprintf("%.1f",mapData$freq),'</td></tr>',
# "<tr><td>Number of ",typeWords," with hits: </td><td>",counts,'</td></tr>',
'</table>') ,
Expand Down
4 changes: 1 addition & 3 deletions man/getMapInfo.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f9c09c9

Please sign in to comment.