SummaryFigures_Height_WithAttributes: SummaryFigures_Height_WithAttributes

Description Usage Arguments Details Author(s) Examples

View source: R/SummaryFigures_Height_WithAttributes.R

Description

Returns a static or interactive (plotly-friendly) ggplot boxplot summarizing species heights across your ecological site(s) based on summary variables (Growth habit, individual species, or sagebrush), colored by attributed. Hard-coded for BLM grazing allotment naming convention. Will update to generalize for spatial attributes.

Usage

1
SummaryFigures_Height(Species_plots_ecosite, EcologicalSiteId, SummaryVar, GroupBy, Interactive, Attributed_Pks)

Arguments

Species_plots_ecosite

Combined AIM and LMF species indicator, subset to your ecological site(s). Returned output from Combine_AIM_LMF_Species and SubsetEcologicalSite_Species.

EcologicalSiteId

Full ecological site id, list of ecological sites, or object

SummaryVar

"Height""

GroupBy

c("Species", "GrowthHabit", "Sagebrush")

Interactive

logical. If TRUE, boxplot aesthetics will be set up for interactive plotly::ggplotly object. If FALSE, will return static box plot.

Attributed_Pks

simplified dataframe with PrimaryKeys associated with each shapefile attribute

Details

If GroupBy == "Species", returns a list of plots that must be called on- boxplots are broken up by GrowthHabitSub, with individual species within each group as faceting row. Y values are height in cm. See example for how to call on plot. If GroupBy == "GrowthHabit", returns a boxplot. X values are growth habit, Y values are height in cm. If GroupBy == "Sagebrush", returns a boxplot. X values are sagebrush species, Y values are height in cm.

Author(s)

Rachel Burke, ecologist/analyst @ Jornada

Examples

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# If SummaryVar == "Height", GroupBy == "GrowthHabit", Interactive == TRUE:
plotly::ggplotly(SummaryFigures_Height(Species_plots_ecosite,
                  EcologicalSiteId, SummaryVar = "Height",
                  GroupBy = "GrowthHabit", Interactive = TRUE),
                  tooltip = "text")
# If SummaryVar == "Height", GroupBy == "GrowthHabit", Interactive == FALSE:
SummaryFigures_Height(Species_plots_ecosite, EcologicalSiteId,
                      SummaryVar = "Height", GroupBy = "GrowthHabit",
                      Interactive = FALSE)
# If SummaryVar == "Height", GroupBy == "Species", Interactive == TRUE:
Plots <- SummaryFigures_Height(Species_plots_ecosite,
                               EcologicalSiteId,
                               SummaryVar = "Height",
                               GroupBy = "Species",
                               Interactive = TRUE, 
                               Attributed_Pks = Attributed_Pks)
plotly::ggplotly((Plots[["Graminoid.Annual"]]), tooltip = "text")
# If SummaryVar == "Height", GroupBy == "Species", Interactive == FALSE:
Plots <- SummaryFigures_Height(Species_plots_ecosite,
                               EcologicalSiteId,
                               SummaryVar = "Height",
                               GroupBy = "Species",
                               Interactive = FALSE,
                                Attributed_Pks = Attributed_Pks)
Plots[["Graminoid.Annual"]]
## The function is currently defined as
function(Species_plots_ecosite, EcologicalSiteId, 
                           SummaryVar, GroupBy, Interactive, Attributed_Pks){


SpeciesList <- SpeciesList 
                                             Family, SpeciesState,
                                             SynonymOf, UpdatedSpeciesCode) 
                 dplyr::mutate(link = paste("https://plants.sc.egov.usda.gov/core/profile?symbol=", Species, sep = "")) 

#Merge with species list so we can hover for scientific name
Species_plots_ecosite_attributed <- merge(Species_plots_ecosite, Attributed_Pks, by = "PrimaryKey", all = TRUE) 
                                    unique() 

EcoSitePlots_Attributed <- merge(EcoSitePlots, Attributed_Pks, by = "PrimaryKey", all = TRUE)


#Unhardcode last 3 columns in selection
Species_plots_ecosite_attributed <- merge(Species_plots_ecosite_attributed , SpeciesList , by = c("Species" , "SpeciesState")) 
                         dplyr::select(Species, ScientificName, CommonName, PrimaryKey, 
                                PlotID,  AH_SpeciesCover, 
                                AH_SpeciesCover_n, Hgt_Species_Avg, 
                                Hgt_Species_Avg_n, GrowthHabit, GrowthHabitSub, Duration, 
                                Noxious, SG_Group, link, 
                                ALLOT_NAME, ALLOT_NO, PAST_NAME) 
                         dplyr::mutate_if(is.numeric, round , digits = 2) 


##Setting color palette for plot
  

NoxNonPal_Fill <- c("grey75"  , "#D55E00")
NoxNonPal_Dot <- c("grey33" , "#993300")
## FIgure out how to not hardcode ALLOT_NAME and instead use attribute_title
Attribute_Fill <- scales::seq_gradient_pal("#009966", "#E69F00", "Lab")(seq(0,1, length.out = length(unique(Species_plots_ecosite_attributed$PAST_NAME))))


# Prep for height
HgtPrep <- Species_plots_ecosite_attributed 
             filter(!is.na(GrowthHabitSub)) 
  
 
if(SummaryVar == "Height" & GroupBy == "Species"){
  #Prep
  #Summarizing by species across an EcologicalSiteId

  if(Interactive){
    
    #Species
    
    Plots <- lapply(X = split(HgtPrep, list(HgtPrep$GrowthHabitSub , HgtPrep$Duration) , 
                               drop = TRUE),
                     FUN = function(HgtPrep){
                           current_plot <- ggplot(HgtPrep , 
                                                  aes(x = Species , 
                                                      y = Hgt_Species_Avg, 
                                                      text = paste("Plot Id: " ,  PlotID , 
                                                            "PrimaryKey: " , PrimaryKey ,
                                                             "Species: " , Species , 
                                                             "Average Height (cm): "  , Hgt_Species_Avg , 
                                                             "Average Height, n: " , Hgt_Species_Avg_n ,
                                                             "Allotment: ", ALLOT_NAME,
                                                             "Pasture: ", PAST_NAME, 
                                                              sep = "<br>"))) +
                                          geom_boxplot(width = .6 , outlier.shape = NA) +
                                          geom_jitter(width = .15 , shape = 21) +
                                          # scale_y_continuous(limits = c(0 , 100)) +
                                          theme_light() +
                                          coord_flip() + 
                                          theme(axis.text.y = element_blank() , axis.ticks.y = element_blank() ,
                                                axis.line.y = element_blank(), axis.title.x = element_blank())  +
                                                facet_grid(rows = vars(Species) ,
                                                     switch = "y" ,
                                                     scales = "free_y" , drop = TRUE)
                                      return(current_plot)
                                      })
                
    
  }
  
  if(!Interactive){
    
    Plots <- lapply(X = split(HgtPrep, list(HgtPrep$GrowthHabitSub , HgtPrep$Duration) , 
                              drop = TRUE),
                    FUN = function(HgtPrep){
                    current_plot <- ggplot(HgtPrep , aes(x = Species , y = Hgt_Species_Avg)) +
                                           geom_boxplot(width = .6 , outlier.shape = NA) +
                                           geom_jitter(width = .15 , size = 2 , aes(color = PAST_NAME, shape = Noxious)) +
                                           scale_color_manual(values = Attribute_Fill, na.value="#000000") + 
                                         # scale_y_continuous(limits = c(0 , 100)) +
                                           theme_light() +
                                           labs(x = "Species" , y = "Average Height, cm",
                                                caption = paste("Species height in: ", toString(EcologicalSiteId), sep = "")) + 
                                           theme(axis.text.y = element_blank() , 
                                                 axis.ticks.y = element_blank() ,
                                                 axis.line.y = element_blank()) + 
                                                 coord_flip() +
                                                 facet_grid(rows = vars(Species) ,
                                                            switch = "y" ,
                                                            scales = "free_y" , drop = TRUE)
                                           return(current_plot) 
                                           })
    
  }
}
  

if(SummaryVar == "Height" & GroupBy == "GrowthHabit"){
  
if(Interactive){
  
  Plots <- ggplot(HgtPrep, aes(x = GrowthHabit , y = Hgt_Species_Avg , 
                               text = paste("Plot Id: " , PlotID , 
                                       "PrimaryKey: " , PrimaryKey ,
                                       "Species: " , Species , 
                                       "Average Height (cm): "  , Hgt_Species_Avg , 
                                       "Average Height , n: " , Hgt_Species_Avg_n ,
                                       "Allotment: ", ALLOT_NAME, 
                                       'Pasture: ', PAST_NAME,
                                        sep = "<br>"))) +
                             geom_boxplot() +
                             geom_jitter(width = .1 , shape = 21) + 
                             theme_light() +
                             theme(axis.ticks.y = element_blank() ,
                             axis.line.y = element_blank(), 
                             axis.title.y = element_blank() ,
                             axis.title.x = element_blank() , 
                             axis.text.y = element_blank()) +
                             coord_flip() +
                             facet_grid(rows = vars(GrowthHabitSub) ,
                             switch = "y" ,
                             scales = "free_y" , drop = TRUE)
  
}
  
  if(!Interactive){
          Plots <- ggplot(HgtPrep, aes(x = GrowthHabit , y = Hgt_Species_Avg)) +
                         geom_boxplot() +
                         geom_jitter(width = .2 , 
                                    (aes(color = PAST_NAME, shape = Noxious))) +
                         labs(x = "Growth Habit" , 
                              y =  "Average Height, cm",
                              caption = paste("Species height in: ", 
                                              toString(EcologicalSiteId), sep = "")) + 
                         theme_light() +
                         scale_color_manual(values = Attribute_Fill, na.value="#000000") +
                         theme(axis.text.y = element_blank() , 
                               axis.ticks.y = element_blank() ,
                               axis.line.y = element_blank()) + 
                         coord_flip() +
                         facet_grid(rows = vars(GrowthHabitSub) ,
                                    switch = "y" ,
                                    scales = "free_y" , drop = TRUE)
    
  }
  
} 
  

if(SummaryVar == "Height" & GroupBy == "Sagebrush"){
    Sagebrush <- HgtPrep 
    if(nrow(Sagebrush) < 1){Plots <- NULL}
  else{
  if(Interactive){
        Plots <- ggplot(Sagebrush, aes(x = Species , y = Hgt_Species_Avg , 
                       text = paste("Plot Id: " , PlotID , 
                       "PrimaryKey: " , PrimaryKey ,
                       "Species: " , Species , 
                       "Average Height (cm): "  , Hgt_Species_Avg , 
                       "Average Height , n: " , Hgt_Species_Avg_n ,
                       "Allotment: ", ALLOT_NAME,
                       "Pasture: ", PAST_NAME,
                       sep = "<br>"))) +
              geom_boxplot() +
              geom_jitter(width = .1 , shape = 21) + 
              theme_light() +
              theme(axis.ticks.y = element_blank() ,
              axis.line.y = element_blank(), axis.title.y = element_blank() ,
              axis.title.x = element_blank(), axis.text.y = element_blank()) +
              facet_grid(rows = vars(Species), scales = "free_y", drop = TRUE) + 
              coord_flip() 
     
}

if(!Interactive){
  
  Plots <-  ggplot(Sagebrush, aes(x = Species , y = Hgt_Species_Avg)) +
           geom_boxplot() +
           geom_jitter(width = .1 , aes(color = PAST_NAME, shape = Noxious)) + theme_light() +
           theme(axis.ticks.y = element_blank() ,
           axis.line.y = element_blank()) +  
           scale_color_manual(values = Attribute_Fill, na.value="#000000") +
           labs(x = "Species" , 
                y = "Average Height, cm",
                caption = paste("Species height in: ", 
                                toString(EcologicalSiteId), sep = "")) + 
           coord_flip() 
}
}
}
  return(Plots)
  
}

R-Burke/SiteSummaryTool documentation built on Oct. 15, 2020, 8:21 p.m.