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 numeric in C1980 / R1980C3: got 'Level 2'
#> Warning: Expecting numeric in C1981 / R1981C3: got 'Level 2'
#> Warning: Expecting numeric in C1982 / R1982C3: got 'Level 2'
#> Warning: Expecting numeric in C1983 / R1983C3: got 'Level 2'
#> Warning: Expecting numeric in C1984 / R1984C3: got 'Level 2'
#> Warning: Expecting numeric in C1985 / R1985C3: got 'Level 2'
#> Warning: Expecting numeric in C1986 / R1986C3: got 'Level 2'
#> Warning: Expecting numeric in C1987 / R1987C3: got 'Level 2'
#> Warning: Expecting numeric in C1988 / R1988C3: got 'Level 2'
#> Warning: Expecting numeric in C1989 / R1989C3: got 'Level 2'
#> Warning: Expecting numeric in C1990 / R1990C3: got 'Level 3'
#> Warning: Expecting numeric in C1991 / R1991C3: got 'Level 3'
#> Warning: Expecting numeric in C1992 / R1992C3: got 'Level 3'
#> Warning: Expecting numeric in C1993 / R1993C3: got 'Level 3'
#> Warning: Expecting numeric in C1994 / R1994C3: got 'Level 3'
#> Warning: Expecting numeric in C1995 / R1995C3: got 'Level 3'
#> Warning: Expecting numeric in C1996 / R1996C3: got 'Level 3'
#> Warning: Expecting numeric in C1997 / R1997C3: got 'Level 3'
#> Warning: Expecting numeric in C1998 / R1998C3: got 'Level 4'
#> Warning: Expecting numeric in C1999 / R1999C3: got 'Level 4'
#> Warning: Expecting numeric in C2000 / R2000C3: got 'Level 4'
#> Warning: Expecting numeric in C2001 / R2001C3: got 'Level 4'
#> Warning: Expecting numeric in C2002 / R2002C3: got 'Level 4'
#> Warning: Expecting numeric in C2003 / R2003C3: got 'Level 4'
#> Warning: Expecting numeric in C2004 / R2004C3: got 'Level 4'
#> Warning: Expecting numeric in C2005 / R2005C3: got 'Level 5'
#> Warning: Expecting numeric in C2006 / R2006C3: got 'Level 5'
#> Warning: Expecting numeric in C2007 / R2007C3: got 'Level 5'
#> Warning: Expecting numeric in C2008 / R2008C3: got 'Level 2'
#> Warning: Expecting numeric in C2009 / R2009C3: got 'Level 2'
#> Warning: Expecting numeric in C2010 / R2010C3: got 'Level 2'
#> Warning: Expecting numeric in C2011 / R2011C3: got 'Level 2'
#> Warning: Expecting numeric in C2012 / R2012C3: got 'Level 2'
#> Warning: Expecting numeric in C2013 / R2013C3: got 'Level 2'
#> Warning: Expecting numeric in C2014 / R2014C3: got 'Level 2'
#> Warning: Expecting numeric in C2015 / R2015C3: got 'Level 2'
#> Warning: Expecting numeric in C2016 / R2016C3: got 'Level 2'
#> Warning: Expecting numeric in C2017 / R2017C3: got 'Level 2'
#> Warning: Expecting numeric in C2018 / R2018C3: got 'Level 3'
#> Warning: Expecting numeric in C2019 / R2019C3: got 'Level 3'
#> Warning: Expecting numeric in C2020 / R2020C3: got 'Level 3'
#> Warning: Expecting numeric in C2021 / R2021C3: got 'Level 3'
#> Warning: Expecting numeric in C2022 / R2022C3: got 'Level 3'
#> Warning: Expecting numeric in C2023 / R2023C3: got 'Level 3'
#> Warning: Expecting numeric in C2024 / R2024C3: got 'Level 3'
#> Warning: Expecting numeric in C2025 / R2025C3: got 'Level 3'
#> Warning: Expecting numeric in C2026 / R2026C3: got 'Level 4'
#> Warning: Expecting numeric in C2027 / R2027C3: got 'Level 4'
#> Warning: Expecting numeric in C2028 / R2028C3: got 'Level 4'
#> Warning: Expecting numeric in C2029 / R2029C3: got 'Level 4'
#> Warning: Expecting numeric in C2030 / R2030C3: got 'Level 4'
#> Warning: Expecting numeric in C2031 / R2031C3: got 'Level 4'
#> Warning: Expecting numeric in C2032 / R2032C3: got 'Level 4'
#> Warning: Expecting numeric in C2033 / R2033C3: got 'Level 5'
#> Warning: Expecting numeric in C2034 / R2034C3: got 'Level 5'
#> Warning: Expecting numeric in C2035 / R2035C3: got 'Level 5'
#> Warning: Expecting numeric in C2036 / R2036C3: got 'Level 2'
#> Warning: Expecting numeric in C2037 / R2037C3: got 'Level 2'
#> Warning: Expecting numeric in C2038 / R2038C3: got 'Level 2'
#> Warning: Expecting numeric in C2039 / R2039C3: got 'Level 2'
#> Warning: Expecting numeric in C2040 / R2040C3: got 'Level 2'
#> Warning: Expecting numeric in C2041 / R2041C3: got 'Level 2'
#> Warning: Expecting numeric in C2042 / R2042C3: got 'Level 2'
#> Warning: Expecting numeric in C2043 / R2043C3: got 'Level 2'
#> Warning: Expecting numeric in C2044 / R2044C3: got 'Level 2'
#> Warning: Expecting numeric in C2045 / R2045C3: got 'Level 2'
#> Warning: Expecting numeric in C2046 / R2046C3: got 'Level 3'
#> Warning: Expecting numeric in C2047 / R2047C3: got 'Level 3'
#> Warning: Expecting numeric in C2048 / R2048C3: got 'Level 3'
#> Warning: Expecting numeric in C2049 / R2049C3: got 'Level 3'
#> Warning: Expecting numeric in C2050 / R2050C3: got 'Level 3'
#> Warning: Expecting numeric in C2051 / R2051C3: got 'Level 3'
#> Warning: Expecting numeric in C2052 / R2052C3: got 'Level 3'
#> Warning: Expecting numeric in C2053 / R2053C3: got 'Level 3'
#> Warning: Expecting numeric in C2054 / R2054C3: got 'Level 3'
#> Warning: Expecting numeric in C2055 / R2055C3: got 'Level 4'
#> Warning: Expecting numeric in C2056 / R2056C3: got 'Level 4'
#> Warning: Expecting numeric in C2057 / R2057C3: got 'Level 4'
#> Warning: Expecting numeric in C2058 / R2058C3: got 'Level 4'
#> Warning: Expecting numeric in C2059 / R2059C3: got 'Level 4'
#> Warning: Expecting numeric in C2060 / R2060C3: got 'Level 4'
#> Warning: Expecting numeric in C2061 / R2061C3: got 'Level 4'
#> Warning: Expecting numeric in C2062 / R2062C3: got 'Level 4'
#> Warning: Expecting numeric in C2063 / R2063C3: got 'Level 5'
#> Warning: Expecting numeric in C2064 / R2064C3: got 'Level 5'
#> Warning: Expecting numeric in C2065 / R2065C3: got 'Level 5'
#> Warning: Expecting numeric in C2066 / R2066C3: got 'Level 2'
#> Warning: Expecting numeric in C2067 / R2067C3: got 'Level 2'
#> Warning: Expecting numeric in C2068 / R2068C3: got 'Level 2'
#> Warning: Expecting numeric in C2069 / R2069C3: got 'Level 2'
#> Warning: Expecting numeric in C2070 / R2070C3: got 'Level 2'
#> Warning: Expecting numeric in C2071 / R2071C3: got 'Level 2'
#> Warning: Expecting numeric in C2072 / R2072C3: got 'Level 2'
#> Warning: Expecting numeric in C2073 / R2073C3: got 'Level 2'
#> Warning: Expecting numeric in C2074 / R2074C3: got 'Level 2'
#> Warning: Expecting numeric in C2075 / R2075C3: got 'Level 3'
#> Warning: Expecting numeric in C2076 / R2076C3: got 'Level 3'
#> Warning: Expecting numeric in C2077 / R2077C3: got 'Level 3'
#> Warning: Expecting numeric in C2078 / R2078C3: got 'Level 3'
#> Warning: Expecting numeric in C2079 / R2079C3: got 'Level 3'
#> Warning: Expecting numeric in C2080 / R2080C3: got 'Level 3'
#> Warning: Expecting numeric in C2081 / R2081C3: got 'Level 3'
#> Warning: Expecting numeric in C2082 / R2082C3: got 'Level 4'
#> Warning: Expecting numeric in C2083 / R2083C3: got 'Level 4'
#> Warning: Expecting numeric in C2084 / R2084C3: got 'Level 4'
#> Warning: Expecting numeric in C2085 / R2085C3: got 'Level 5'
#> Warning: Expecting numeric in C2086 / R2086C3: got 'Level 2'
#> Warning: Expecting numeric in C2087 / R2087C3: got 'Level 2'
#> Warning: Expecting numeric in C2088 / R2088C3: got 'Level 2'
#> Warning: Expecting numeric in C2089 / R2089C3: got 'Level 2'
#> Warning: Expecting numeric in C2090 / R2090C3: got 'Level 2'
#> Warning: Expecting numeric in C2091 / R2091C3: got 'Level 2'
#> Warning: Expecting numeric in C2092 / R2092C3: got 'Level 2'
#> Warning: Expecting numeric in C2093 / R2093C3: got 'Level 2'
#> Warning: Expecting numeric in C2094 / R2094C3: got 'Level 3'
#> Warning: Expecting numeric in C2095 / R2095C3: got 'Level 3'
#> Warning: Expecting numeric in C2096 / R2096C3: got 'Level 3'
#> Warning: Expecting numeric in C2097 / R2097C3: got 'Level 3'
#> Warning: Expecting numeric in C2098 / R2098C3: got 'Level 3'
#> Warning: Expecting numeric in C2099 / R2099C3: got 'Level 4'
#> Warning: Expecting numeric in C2100 / R2100C3: got 'Level 4'
#> Warning: Expecting numeric in C2101 / R2101C3: got 'Level 4'
#> Warning: Expecting numeric in C2102 / R2102C3: got 'Level 5'
#> Warning: Expecting numeric in C2103 / R2103C3: got 'Level 2'
#> Warning: Expecting numeric in C2104 / R2104C3: got 'Level 2'
#> Warning: Expecting numeric in C2105 / R2105C3: got 'Level 2'
#> Warning: Expecting numeric in C2106 / R2106C3: got 'Level 2'
#> Warning: Expecting numeric in C2107 / R2107C3: got 'Level 2'
#> Warning: Expecting numeric in C2108 / R2108C3: got 'Level 2'
#> Warning: Expecting numeric in C2109 / R2109C3: got 'Level 2'
#> Warning: Expecting numeric in C2110 / R2110C3: got 'Level 2'
#> Warning: Expecting numeric in C2111 / R2111C3: got 'Level 2'
#> Warning: Expecting numeric in C2112 / R2112C3: got 'Level 2'
#> Warning: Expecting numeric in C2113 / R2113C3: got 'Level 2'
#> Warning: Expecting numeric in C2114 / R2114C3: got 'Level 2'
#> Warning: Expecting numeric in C2115 / R2115C3: got 'Level 2'
#> Warning: Expecting numeric in C2116 / R2116C3: got 'Level 3'
#> Warning: Expecting numeric in C2117 / R2117C3: got 'Level 3'
#> Warning: Expecting numeric in C2118 / R2118C3: got 'Level 3'
#> Warning: Expecting numeric in C2119 / R2119C3: got 'Level 3'
#> Warning: Expecting numeric in C2120 / R2120C3: got 'Level 3'
#> Warning: Expecting numeric in C2121 / R2121C3: got 'Level 3'
#> Warning: Expecting numeric in C2122 / R2122C3: got 'Level 3'
#> Warning: Expecting numeric in C2123 / R2123C3: got 'Level 3'
#> Warning: Expecting numeric in C2124 / R2124C3: got 'Level 3'
#> Warning: Expecting numeric in C2125 / R2125C3: got 'Level 3'
#> Warning: Expecting numeric in C2126 / R2126C3: got 'Level 3'
#> Warning: Expecting numeric in C2127 / R2127C3: got 'Level 4'
#> Warning: Expecting numeric in C2128 / R2128C3: got 'Level 4'
#> Warning: Expecting numeric in C2129 / R2129C3: got 'Level 4'
#> Warning: Expecting numeric in C2130 / R2130C3: got 'Level 4'
#> Warning: Expecting numeric in C2131 / R2131C3: got 'Level 4'
#> Warning: Expecting numeric in C2132 / R2132C3: got 'Level 4'
#> Warning: Expecting numeric in C2133 / R2133C3: got 'Level 4'
#> Warning: Expecting numeric in C2134 / R2134C3: got 'Level 4'
#> Warning: Expecting numeric in C2135 / R2135C3: got 'Level 5'
#> Warning: Expecting numeric in C2136 / R2136C3: got 'Level 5'
#> Warning: Expecting numeric in C2137 / R2137C3: got 'Level 5'
#> Warning: Expecting numeric in C2138 / R2138C3: got 'Level 5'
#> Warning: Expecting numeric in C2139 / R2139C3: got 'Level 5'
#> Warning: Expecting numeric in C2140 / R2140C3: got 'Level 2'
#> Warning: Expecting numeric in C2141 / R2141C3: got 'Level 2'
#> Warning: Expecting numeric in C2142 / R2142C3: got 'Level 2'
#> Warning: Expecting numeric in C2143 / R2143C3: got 'Level 2'
#> Warning: Expecting numeric in C2144 / R2144C3: got 'Level 2'
#> Warning: Expecting numeric in C2145 / R2145C3: got 'Level 2'
#> Warning: Expecting numeric in C2146 / R2146C3: got 'Level 2'
#> Warning: Expecting numeric in C2147 / R2147C3: got 'Level 2'
#> Warning: Expecting numeric in C2148 / R2148C3: got 'Level 2'
#> Warning: Expecting numeric in C2149 / R2149C3: got 'Level 2'
#> Warning: Expecting numeric in C2150 / R2150C3: got 'Level 3'
#> Warning: Expecting numeric in C2151 / R2151C3: got 'Level 3'
#> Warning: Expecting numeric in C2152 / R2152C3: got 'Level 3'
#> Warning: Expecting numeric in C2153 / R2153C3: got 'Level 3'
#> Warning: Expecting numeric in C2154 / R2154C3: got 'Level 3'
#> Warning: Expecting numeric in C2155 / R2155C3: got 'Level 3'
#> Warning: Expecting numeric in C2156 / R2156C3: got 'Level 3'
#> Warning: Expecting numeric in C2157 / R2157C3: got 'Level 3'
#> Warning: Expecting numeric in C2158 / R2158C3: got 'Level 3'
#> Warning: Expecting numeric in C2159 / R2159C3: got 'Level 4'
#> Warning: Expecting numeric in C2160 / R2160C3: got 'Level 4'
#> Warning: Expecting numeric in C2161 / R2161C3: got 'Level 4'
#> Warning: Expecting numeric in C2162 / R2162C3: got 'Level 4'
#> Warning: Expecting numeric in C2163 / R2163C3: got 'Level 4'
#> Warning: Expecting numeric in C2164 / R2164C3: got 'Level 4'
#> Warning: Expecting numeric in C2165 / R2165C3: got 'Level 4'
#> Warning: Expecting numeric in C2166 / R2166C3: got 'Level 4'
#> Warning: Expecting numeric in C2167 / R2167C3: got 'Level 5'
#> Warning: Expecting numeric in C2168 / R2168C3: got 'Level 5'
#> Warning: Expecting numeric in C2169 / R2169C3: got 'Level 5'
#> Warning: Expecting numeric in C2170 / R2170C3: got 'Level 5'
## Calculate Metric Memberships
df_metmemb <- BCG.Metric.Membership(df_metval, 
                                    df_rules,
                                    col_SITE_TYPE = "INDEX_REGION")
## Calculate Level Memberships
df_levmemb <- BCG.Level.Membership(df_metmemb, 
                                   df_rules)
#> Warning: There were 14 warnings in `dplyr::summarize()`.
#> The first warning was:
#>  In argument: `rules_lev_min = min(LEVEL, na.rm = TRUE)`.
#>  In group 11: `INDEX_NAME = "BCG_ORWA_v1_Bugs500ct"`, `INDEX_CLASS =
#>   "foothills_multihab"`.
#> Caused by warning in `min()`:
#> ! no non-missing arguments to min; returning Inf
#>  Run `dplyr::last_dplyr_warnings()` to see the 13 remaining warnings.
## 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