Purpose

Demonstrate how to create a map of model output.

Map

Use ggplot to create a map from the example data included in BCGcalc.

The example data only includes data from Washington.

# Packages
library(BCGcalc)
library(ggplot2)
library(readxl)
library(BioMonTools)

# Calculate BCG model
## Calculate Metrics
df_samps <- read_excel(
  system.file("./extdata/Data_BCG_PugLowWilVal.xlsx",
              package="BCGcalc"),
  guess_max = 10^6)
## Run Function
myDF <- df_samps
col_add_char <- c("INFRAORDER", 
                  "HABITAT", 
                  "ELEVATION_ATTR", 
                  "GRADIENT_ATTR",
                  "WSAREA_ATTR", 
                  "HABSTRUCT")
col_add_num <- c("UFC",
                 "SAMP_AREA_M2"#,
                 # "DENSITY_M2"
                 )
myDF[, col_add_char] <- NA_character_
myDF[, col_add_num] <- NA_integer_
myCols <- c("Area_mi2", 
            "SurfaceArea", 
            # "Density_m2", 
            "Density_ft2")
df_metval <- metric.values(myDF, 
                           "bugs",
                           fun.cols2keep = myCols) 
#> Joining with `by = join_by(SAMPLEID, INDEX_NAME, INDEX_CLASS)`
## Import Rules
df_rules <- read_excel(
  system.file("./extdata/Rules.xlsx",
              package="BCGcalc"),
  sheet="Rules") 
#> Warning: Expecting logical in S2409 / R2409C19: got '1'
#> Warning: Expecting logical in S2410 / R2410C19: got '11'
#> Warning: Expecting logical in S2411 / R2411C19: got '12'
#> Warning: Expecting logical in S2412 / R2412C19: got '15'
#> Warning: Expecting logical in S2413 / R2413C19: got '16'
#> Warning: Expecting logical in S2414 / R2414C19: got '17'
#> Warning: Expecting logical in S2415 / R2415C19: got '18'
#> Warning: Expecting logical in S2416 / R2416C19: got '19'
#> Warning: Expecting logical in S2417 / R2417C19: got '25'
#> Warning: Expecting logical in S2418 / R2418C19: got '29'
#> Warning: Expecting logical in S2419 / R2419C19: got '4'
#> Warning: Expecting logical in S2420 / R2420C19: got '5'
#> Warning: Expecting logical in S2421 / R2421C19: got '8'
#> Warning: Expecting logical in S2422 / R2422C19: got '9'
#> Warning: Expecting logical in S2423 / R2423C19: got '1'
#> Warning: Expecting logical in S2424 / R2424C19: got '11'
#> Warning: Expecting logical in S2425 / R2425C19: got '12'
#> Warning: Expecting logical in S2426 / R2426C19: got '15'
#> Warning: Expecting logical in S2427 / R2427C19: got '16'
#> Warning: Expecting logical in S2428 / R2428C19: got '17'
#> Warning: Expecting logical in S2429 / R2429C19: got '18'
#> Warning: Expecting logical in S2430 / R2430C19: got '19'
#> Warning: Expecting logical in S2431 / R2431C19: got '25'
#> Warning: Expecting logical in S2432 / R2432C19: got '29'
#> Warning: Expecting logical in S2433 / R2433C19: got '4'
#> Warning: Expecting logical in S2434 / R2434C19: got '5'
#> Warning: Expecting logical in S2435 / R2435C19: got '8'
#> Warning: Expecting logical in S2436 / R2436C19: got '9'
#> Warning: Expecting logical in S2437 / R2437C19: got '1'
#> Warning: Expecting logical in S2438 / R2438C19: got '12'
#> Warning: Expecting logical in S2439 / R2439C19: got '15'
#> Warning: Expecting logical in S2440 / R2440C19: got '19'
#> Warning: Expecting logical in S2441 / R2441C19: got '25'
#> Warning: Expecting logical in S2442 / R2442C19: got '29'
#> Warning: Expecting logical in S2443 / R2443C19: got '4'
#> Warning: Expecting logical in S2444 / R2444C19: got '5'
#> Warning: Expecting logical in S2445 / R2445C19: got '9'
#> Warning: Expecting logical in S2446 / R2446C19: got '1'
#> Warning: Expecting logical in S2447 / R2447C19: got '19'
#> Warning: Expecting logical in S2448 / R2448C19: got '29'
#> Warning: Expecting logical in S2449 / R2449C19: got '4'
#> Warning: Expecting logical in S2450 / R2450C19: got '1'
#> Warning: Expecting logical in S2451 / R2451C19: got '11'
#> Warning: Expecting logical in S2452 / R2452C19: got '12'
#> Warning: Expecting logical in S2453 / R2453C19: got '15'
#> Warning: Expecting logical in S2454 / R2454C19: got '16'
#> Warning: Expecting logical in S2455 / R2455C19: got '17'
#> Warning: Expecting logical in S2456 / R2456C19: got '18'
#> Warning: Expecting logical in S2457 / R2457C19: got '19'
#> Warning: Expecting logical in S2458 / R2458C19: got '25'
#> Warning: Expecting logical in S2459 / R2459C19: got '29'
#> Warning: Expecting logical in S2460 / R2460C19: got '4'
#> Warning: Expecting logical in S2461 / R2461C19: got '5'
#> Warning: Expecting logical in S2462 / R2462C19: got '8'
#> Warning: Expecting logical in S2463 / R2463C19: got '9'
#> Warning: Expecting logical in S2464 / R2464C19: got '1'
#> Warning: Expecting logical in S2465 / R2465C19: got '11'
#> Warning: Expecting logical in S2466 / R2466C19: got '12'
#> Warning: Expecting logical in S2467 / R2467C19: got '15'
#> Warning: Expecting logical in S2468 / R2468C19: got '16'
#> Warning: Expecting logical in S2469 / R2469C19: got '17'
#> Warning: Expecting logical in S2470 / R2470C19: got '19'
#> Warning: Expecting logical in S2471 / R2471C19: got '25'
#> Warning: Expecting logical in S2472 / R2472C19: got '29'
#> Warning: Expecting logical in S2473 / R2473C19: got '4'
#> Warning: Expecting logical in S2474 / R2474C19: got '5'
#> Warning: Expecting logical in S2475 / R2475C19: got '8'
#> Warning: Expecting logical in S2476 / R2476C19: got '9'
#> Warning: Expecting logical in S2477 / R2477C19: got '1'
#> Warning: Expecting logical in S2478 / R2478C19: got '12'
#> Warning: Expecting logical in S2479 / R2479C19: got '15'
#> Warning: Expecting logical in S2480 / R2480C19: got '19'
#> Warning: Expecting logical in S2481 / R2481C19: got '25'
#> Warning: Expecting logical in S2482 / R2482C19: got '29'
#> Warning: Expecting logical in S2483 / R2483C19: got '4'
#> Warning: Expecting logical in S2484 / R2484C19: got '5'
#> Warning: Expecting logical in S2485 / R2485C19: got '9'
#> Warning: Expecting logical in S2486 / R2486C19: got '1'
#> Warning: Expecting logical in S2487 / R2487C19: got '19'
#> Warning: Expecting logical in S2488 / R2488C19: got '29'
#> Warning: Expecting logical in S2489 / R2489C19: got '4'
## Calculate Metric Memberships
df_metmemb <- BCG.Metric.Membership(df_metval, 
                                    df_rules)
## Calculate Level Memberships
df_levmemb <- BCG.Level.Membership(df_metmemb, 
                                   df_rules)
## Run Function
df_levels <- BCG.Level.Assignment(df_levmemb)


# Add Lat-Long
## Get unique stations
df_stations <- as.data.frame(unique(df_samps[, c("SampleID",
                                                 "Latitude",
                                                 "Longitude"
                                                 )]))

## Merge
df_map <- merge(df_levels,
                df_stations,
                by.x = "SampleID",
                by.y = "SampleID",
                all.x = TRUE)
## Level1 Name to Character
df_map$Primary_BCG_Level <- as.character(df_map$Primary_BCG_Level)

# Palette, Color Blind
## http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/
pal_cb <- c("#E69F00", 
            "#56B4E9", 
            "#009E73",
            "#F0E442", 
            "#0072B2",
            "#D55E00", 
            "#CC79A7", 
            "#999999")
pal_bcg_old <- c("green", 
                 "cyan", 
                 "blue",
                 "orange", 
                 "red")
pal_bcg <- c("blue", 
             "green",
             "gray", 
             "yellow", 
             "dark orange", 
             "red")

# Shapes
shp_solid <- c(17, 16) # solid triangle and circle

# Map
## Map of OR and WA
m1 <- ggplot(data = subset(map_data("state"),
                           region %in% c("washington"))) + 
  geom_polygon(aes(x = long, y = lat, group = group)
               , fill = "gray90"
               , color = "black") +
  coord_fixed(1.3) + 
  theme_void() +
  # Add Points
  geom_point(data = df_map,
             aes(Longitude,
                 Latitude,
                 shape = INDEX_NAME,
                 color = Primary_BCG_Level),
             na.rm = TRUE) +
  # Change shape
  scale_shape_manual(name = "Site Type", 
                     values = shp_solid) +
  # Change colors
  scale_colour_manual(name = "Level 1 Assignment", 
                      values = pal_bcg) + 
  # Map Title
  labs(title="Pacific Northwest BCG",
       subtitle="Example Samples, WA only") +
  theme(plot.title = element_text(hjust = 0.5),
        plot.subtitle = element_text(hjust=0.5))

## Display Map
m1