Main manuscript

Figures

Figure 1 - Phylogenetic tree with traits

# Prepare data sets ###################

Reflectance <- read.csv(here::here("Data/FromCode/Reflectance_PData.csv"))[-1]

RPDataAgg <- read.csv(here::here("Data/FromCode/PolarzElytraRefl.csv"))[-1]

RPDAgg1 <-
  RPDataAgg %>%
  dplyr::select(-ind, -spp) %>%
  dplyr::select(phylogeny_name, everything()) %>%
  dplyr::group_by(phylogeny_name) %>% # group
  dplyr::summarise(across(everything(), list(mean))) %>% 
  select(1,9) %>% 
  arrange(phylogeny_name)

Cons1agg <- read.csv(here::here("Data/FromCode/ConsolidatedReflectivitySpp.csv"))[-1] %>% 
  arrange(phylogeny_name) %>% 
  mutate (Pol = RPDAgg1$ElytrPol_1)

# Convert to a data frame for the comparative.data function
Cons1agg <- as.data.frame(Cons1agg)
head(Cons1agg)
phylogeny_nameTOTVISNIRRessizeFRSFRS2Pol
Anomala_antigua17.24.5528.3-3.46 1.77-6.92-1.29 0.161 
Anoplognathus_abnormis27.316.5 36.6-6.87 1.58-11.2 -1.85 0.0643
Anoplognathus_aeneus22.110.4 32.3-5.22 3.11-9.12-3.07 0.112 
Anoplognathus_aureus44.934.3 54.2-6.82 1.71-12.5 -1.47 0.206 
Anoplognathus_boisduvalii31.117.3 43.2-1.06 2.61-5.48-1.23 0.0885
Anoplognathus_brevicollis27.412.9 40.10.2362.51-3.850.2160.0871
# Modify to make it compatible with tree tips
ConsAgg<- as.data.frame(Cons1agg) # create new data frame
rownames(ConsAgg) <- ConsAgg[, 1] # make species the row names
ConsAgg <- ConsAgg[, 2:length(ConsAgg)] # eliminate spp name (redundant)

# Separate data frames

TOTdf <- ConsAgg %>% dplyr::select(TOT)
VISdf <- ConsAgg %>% dplyr::select(VIS)
Resdf <- ConsAgg %>% dplyr::select(Res)
Poldf <- ConsAgg %>% dplyr::select(Pol)


# Combine with phylogeny ###################
# Phylogeny
trees <- ape::read.tree(here::here("Data/XMAS_mat2b_bst2ef_set23nn2_pinct.nwk"))

# read the tree
MCCtree.raw <-
  ape::read.nexus(here::here("Data/xmas_mat2b_bst2ef_set23nn2_pinct_med.tre"))

# Prune extra spp in the tree, not contain in the test sample
species.MCC <- as.data.frame(unique(Cons1agg$phylogeny_name))

# Convert to "row names" (required for following steps)
row.names(species.MCC) <- species.MCC[, 1]

# Make sure the names in data set and tree match
temp.MCC <- name.check(MCCtree.raw, species.MCC)

# This step would be neccesary if the tips had been different.
# MCCtree <- drop.tip(MCCtree.raw, temp.MCC$tree_not_data)
# Not used in our case.

# Instead, changed the name
MCCtree <- MCCtree.raw

# This is to control the length of the tips:
MCCtree$edge.length <- MCCtree$edge.length * 0.1 # Run only once!
# Add traits to the tree ######################
# create a MCC tree without tip names for the plot

tree_only <- ggtree(MCCtree, layout = "rectangular")

# create heat map columns
# Note: log transforms increase contrast for visualization purposes

# Add TOTAL reflectivity
p0 <- gheatmap(tree_only, (TOTdf),
  width = 0.04, low = "gray90", high = "#ffc100",
  font.size = 2.2,
  colnames_position = "bottom", colnames_angle = 90,
  colnames_offset_x = 0, colnames_offset_y = -0.5,
  legend_title = "TOT"
)

p1.scale <- p0 + new_scale_fill() # add new scale for second variable

# Add VIS
p1 <- gheatmap(p1.scale, (VISdf),
  offset = 0.12, # to avoid overlapping with p1
  width = 0.04, low = "gray90", high = "#fe6100",
  font.size = 2.2,
  colnames_position = "bottom", colnames_angle = 90,
  colnames_offset_x = 0, colnames_offset_y = -0.5,
  legend_title = "VIS"
)

p2.scale <- p1 + new_scale_fill() # add new scale for third variable

# Add NIR
p2 <- gheatmap(p2.scale, (Resdf),
  offset = 0.24, # to avoid overlapping with p1
  width = 0.04, low = "gray90", high = "#ff2c85",
  font.size = 2.2,
  colnames_position = "bottom", colnames_angle = 90,
  colnames_offset_x = 0, colnames_offset_y = -0.5,
  legend_title = "RES"
)

# Add polarisation

p3.scale <- p2 + new_scale_fill() # add new scale for third variable

# Add pol
p3 <- gheatmap(p3.scale, (Poldf),
  offset = 0.36, # to avoid overlapping with p1
  width = 0.04, low = "gray90", high = "#648fff",
  font.size = 2.2,
  colnames_position = "bottom", colnames_angle = 90,
  colnames_offset_x = 0, colnames_offset_y = -0.5,
  legend_title = "POL"
)


# Add a white column

p4.scale <- p3 + new_scale_fill() # add new scale 

# Add a blank column. This is a trick to keep the branches short:
p4 <- gheatmap(p4.scale, (Poldf),
  offset = 2, # to avoid overlapping with p1
  width = 0.04, low = "white", high = "white",
  font.size = 0.01,
  colnames_position = "bottom", colnames_angle = 0,
  colnames_offset_x = 0, colnames_offset_y = -0.5,
  legend_title = " ", 
)

Print:

p4 +
  theme(legend.position = 'left',
    legend.background = element_rect(),
    legend.key = element_blank(), # removes the border
    legend.key.size = unit(0.4, 'cm'), # sets overall area/size of the legend
    legend.text = element_text(size = 8), # text size
    title = element_text(size = 8))+
  geom_tiplab(size=2.76, # add names
              offset = 0.57, # this adjusts the position of the species names
              aes(label= paste0('italic(', label, ')')), # sppnames in italics
              parse=TRUE) # allows us to alter the tips label  



Supplementary

Phylogenetic tree

# create phylogeny object for ggtree()
phylo.MCC <- ggtree(MCCtree, layout = "rectangular") 

# set xlim
phylo.MCC + xlim(0, 33) +
  geom_nodepoint(col = "blue", alpha = 0.4)+
  geom_tiplab(size=2.3)


Hemispherical Reflectance

Examples are A. prasinus , A. rayneri , A. laetus

Step1Example <-
  Reflectance %>%
  select(
    wl,
    contains("prsi01") |
    contains("rayn01") |
    contains("lats03") |
    contains("aurs01")
  ) 


ggplot(Step1Example, aes(x = wl)) +
  geom_line(aes(x = wl, y = prsi01),
    size = 1, colour = "#ff2c85"
  ) +
  geom_line(aes(x = wl, y = rayn01),
    size = 1, colour = "#fe6100"
  ) +
  geom_line(aes(x = wl, y = lats03),
    size = 1, colour = "#648fff"
  ) +
  geom_line(aes(x = wl, y = aurs01),
    size = 1, colour = "#ffb000"
  ) +
  scale_fill_manual(values = c( "#648fff", "#dc267f","#ff7200"), 
                    name = "fill") +
  scale_color_manual(values = c( "#648fff", "#dc267f","#ff7200"),
                    name = "fill") +
  xlab("Wavelength (nm)") +
  ylab("Reflectance (%)") +
  theme_minimal() +
  ylim(0, 90) +
  theme(legend.position = "none")

transmittance and absorbance

TrAb <- read.csv("../Data/13_SuppTransmittancvsAbsorbance.csv") %>% 
  dplyr::mutate(Spp = substr(Beetle, 1, 4)) %>% 
  dplyr::select(-Beetle) %>% 
  dplyr::group_by(Spp) %>% 
  dplyr::summarise(Transm = mean(Td_VIS),
            Absorb = mean(Ab_VIS),
            Trsd = sd(Td_VIS),
            Absd = sd(Ab_VIS))
  

head(TrAb)
SppTransmAbsorbTrsdAbsd
anom4.4190.13.26  0.536
atki1.6290.91.86  0.328
aurs8.6759.90.04570.248
clor8.3276.55.4   6.17 
conc20.2 69.72.68  1.04 
ecry7.3878.61.69  1.71 
ggplot(TrAb, aes(y = reorder(Spp, -Transm))) +
  geom_bar(aes(x = (-Transm), fill = "Transm"), 
           stat = "identity", position = "dodge",  fill = "#ffb000") +
  geom_bar(aes(x = Absorb, fill = "Absorb"), 
          stat = "identity", position = "dodge",  fill = "#648fff") +
  labs(fill = "Variable") +
  theme_minimal() +
  theme(legend.position = "top") +
  ylab("Species") +
  xlab("Percentage (%)")