hmsc-r / HMSC

GNU General Public License v3.0
102 stars 37 forks source link

Changing order of categories on X axis for prediction plots #158

Open akrowe20 opened 1 year ago

akrowe20 commented 1 year ago

Hello, I am working off of the scripts provided in the courses. Is there a way to change the order that categories are listed in the x axis of the prediction plots? One of my fixed effects is month, and I would like my prediction plots to show the temporal order, whereas right now it is showing alphabetical order. Thanks! Amanda

jarioksa commented 1 year ago

This is (probably) standard R – you don't give details which prediction plots you have. However, the R default is to order factor levels alphabetically. You can change this default using argument levels in factor() command:

> fact <- factor(rep(c("bad","good"), 3))
> fact
[1] bad  good bad  good bad  good  
Levels: bad good # alphabetical
> fact <- factor(fact, levels=c("good","bad"))
> fact
[1] bad  good bad  good bad  good
Levels: good bad # order changed with levels argument, data unchanged

For temporal factor, you may also consider ordered factors which you can get with argument ordered = TRUE in factor(). There are a plenty of examples in help(factor) and R introductory guides. However, changing temporal factor to ordered will display the presentation of results: instead of single factor levels, the results will be given as a linear trend + quadratic + cubic + more complex. You may not want this, but in some cases this can be useful.

akrowe20 commented 1 year ago

Thank you. I am working in this part of the code. Is there a way to change this in the prediction code, or do I have to go back to the defining models step to change this? For example, I am working in this part of the code. Can I change the order in the env.list settings?

trait.list = NULL #community weighted mean shown for all traits

env.list = NULL #predictions constructed over all environmental gradients

##################################################################################################

SETTING COMMONLY ADJUSTED PARAMETERS TO NULL WHICH CORRESPONDS TO DEFAULT CHOICE (END)

##################################################################################################

##################################################################################################

CHANGE DEFAULT OPTIONS BY REMOVING COMMENT AND SETTING VALUE (BEGINNING)

NOTE THAT THIS IS THE ONLY SECTION OF THE SCRIPT THAT YOU TYPICALLY NEED TO MODIFY

##################################################################################################

use species.list to select which species are used as examples for which predictions are shown

species.list should be a list of length the number of models.

for each element provide either 0 (use default) or a vector of species indices

species.list = list(1,2,3, 4, 5, 6) species.list[[1]] = 1:5 #options for selecting all species in results species.list[[2]] = 1:5 species.list[[3]]=1:5 species.list[[4]]=1:5 species.list[[5]]=1:5 species.list[[6]]=1:5

use trait.list to select for which traits predictions for community weighted mean traits are shown

trait.list should be a list of length the number of models.

for each element provide either 0 (use default) or a vector of trait indices

see models[[j]]$trNames to see which trait each index corresponds to

trait.list = list()

trait.list[[1]] = c(2,10)

trait.list[[2]] = 0

use env.list to select over which environmental gradients predictions are generated

env.list should be a list of length the number of models.

for each element provide either 0 (use default) or a vector of environmental variables

env.list = list(1,2,3,4,5,6) env.list[[1]] = c("month", "humidity", "transectid","month"= relevel("month","december", "january", "february", "march", "april", "may", "june")) env.list[[2]] = c("month", "humidity", "transectid","month"= relevel("month","december", "january", "february", "march", "april", "may", "june")) env.list[[3]] = c("month", "temp", "transectid", "month"= relevel("month","december", "january", "february", "march", "april", "may", "june")) env.list[[4]] = c("month", "temp", "transectid", "month"= relevel("month","december", "january", "february", "march", "april", "may", "june")) env.list[[5]] =c("month", "lumavg", "transectid", "month"= relevel("month","december", "january", "february", "march", "april", "may", "june")) env.list[[6]] = c("month", "lumavg", "transectid", "month"= relevel("month","december", "january", "february", "march", "april", "may", "june"))