This is a collection of important code and tricks to keep in mind!

Creating your own theme

theme_5oes<-function() {
  theme_minimal(base_size=11, base_family="OpenSans") %+replace%  #select a base theme to go off from
    theme(
      legend.background=element_rect(fill='transparent', colour=NA),
      legend.key= element_rect(fill='transparent', colour=NA),  #borders and background
      legend.position="bottom",
      legend.text=element_text(size=7),
      legend.key.size=unit(0.3, "cm"),
      legend.title = element_text(face="bold",size=7),
      
      panel.grid.minor = element_blank(),
      panel.grid.major = element_line(colour="grey91", size=0.3),
      panel.grid.major.x=element_blank() ,
      
      axis.ticks=element_blank(),
      axis.title.y=element_text(angle=90, margin=margin(r=10)),
      axis.text.x=element_text(margin=margin(t=-5)), #how close the xaxis labels are to the line, t=top
      axis.title.x=element_text(margin=margin(t=5)),
      
    )
}

Basic Manipulations

Reading csvs and exporting dataframes to a csv

#reading csvs, make sure to set working directory first 

data<-read.csv("name.csv")

#exporting dataframes

write.csv (data,"name.csv")

Converting one specific column into a factor

data$column<-as.factor(data$column) 

#can do the same with "as.numeric", "as.factor"

Changing multiple columns into numeric values

data[,5:9]<-sapply(data[,5:9], as.numeric)

Replace all NAs with a value of 0

data[is.na(data)]<-0

Selecting Specific Columns

data<-data %>%
  select(column_name, column_name, ends_with("value/text" ))  #selects columns that ends with what is stated

Change values in columns

data<-data %>% 
  mutate (column_name= recode (column_name, 
                             "value"= "new value" ,
                             "value" = "new value" ))

Renaming Columns

data <- data %>%
  rename("new value"= column_name)

colnames(data) <- sub("value","", colnames(data))  #clean up colnames, remove 'value' from column names

Subsetting Data

newdata<-subset(data, column_name=="value" & column_name=="value" & column_name >=1 )

Filtering data

#slightly different from subsetting data because nothing is removed (i.e. subsetting factors will still give you the same number of factors unless you recall it)
data<- data %>%
  filter(col<=value)

#another way for text callings
data<- data %>%
  filter(family %in% c("Acanthuridae", "Balistidae"))

#recalling factors after filtering
data$colx<-as.factor(data$colx)

Separate values in one column into various columns

data <- data %>%
  separate(column_name,into=c("col1", "col2"), sep= " ") =

Create new column based on set values

  mutate (newcolname= case_when(
    col <=value ~ "new value",
    col >value & col <=value ~ "new value",
    TRUE ~ NA_character_ )) #catch all records that don't fit any of the criteria

Fill in missing values

data <- data %>%
complete(col, nesting(col2,col3), fill=list(col4=0))

#nesting binds variables together as one unit. Variables inside a nesting call will not expand. (i.e. it is fixed and not dependent on the other variables called) You can nest multiple groups together to act like one variable as below:

data <-data %>%
  complete(
    nesting(col1,col2),
    nesting(col3,col),
    fill=list(col4=0)
  )

Removing certain values within a column

data<-data[!(data$column=="value"),] #searches by row and removes the stated value. <!> tells it to remove these accounts, without the <!> it will keep all rows with these accounts of "value"

Rounding values

data$col<-round(data$col,1)

Changing axis labels from decimal -> integer

library(scales)

plot + scale_y_continuous(labels=label_number(accuracy=1))

Calculations

Counting distinct number of species by a group

data <- data %>%
  group_by(col1) %>%
  summarise(n=n_distinct(col2))

St.Deviation / St.Error

library(matrixStats)

data<- data %>%

  mutate(mean=rowMeans(.[cols]),stdev=rowSds(as.matrix(.[cols])),se=stdev/sqrt(n))

Aesthetics

Establishing colors

Colors<-c( "value"="#FAF3DD", 
           "value"= "#C8D5B9")

#Call in when creating ggplot
  scale_fill_manual(values=Colors) 

Legend one line

plot<-plot + guides(fill=guide_legend(nrow=1, reverse=TRUE)) 
#be wary of the difference between colour= and fill=, specify "reverse" (the order) if necessary

Reordering levels in factor

data$column <- factor(data$column, levels=c("value1", "value2"))

Partial italics/bold

#not the most elegant solution...

## for partial italic/bold of values within the dataframe ##

#rename values to an expression
mylabels <- c("Acropora Branching"=expression (paste(italic("Acropora")," Branching")), 
              "Acropora Digitate"=expression (paste(italic("Acropora")," Digitate")), 
              "Acropora Encrusting"= expression (paste(italic("Acropora")," Encrusting")), 
              "Acropora Submassive"= expression (paste(italic("Acropora")," Submassive")), 
              "Acropora Table"= expression (paste(italic("Acropora")," Table")), 
              "Coral Branching" = expression (paste("Coral Branching")), 
              "Coral Digitate" = expression (paste("Coral Digitate")), 
              "Coral Encursting" = expression (paste("Coral Encursting")), 
              "Coral Submassive" = expression (paste("Coral Submassive")), 
              "Coral Foliose" = expression (paste("Coral Foliose")), 
              "Coral Laminate" = expression (paste("Coral Laminate")), 
              "Coral Massive" = expression (paste("Coral Massive")), 
              "Millepora" = expression (paste(italic("Millepora"))),
              "Mushroom Coral" = expression (paste("Mushroom Coral")), 
              "Other" = expression (paste("Other"))
)

#color code as per usual
densityplot <- 
  ggplot(dsize_simplified, aes(size)) +
  #geom_histogram(aes(y=..density..),colour="grey20", fill="white",binwidth = 6)+
  geom_density(aes(fill=factor(growthform)),alpha=0.3) +
  scale_fill_manual(values=c("Acropora Branching"= "#c83737",
                             "Acropora Digitate"="#f9dbbd",
                             "Acropora Encrusting"="#ffa5ab",
                             "Acropora Submassive"="#da627d",
                             "Acropora Table" = "#a53860",
                             "Coral Branching"= "#2ca089",
                             "Coral Digitate"="#bee9e8",
                             "Coral Encrusting"="#62b6cb",
                             "Coral Submassive"="#cae9ff",
                             "Coral Foliose"="#5fa8d3",
                             "Coral Laminate"="#1b4965",
                             "Coral Massive"= "#275390",
                             "Millepora"="#ff7f2a",
                             "Mushroom Coral"="#a05a2c",
                             "Other"="#dedede")) +
  facet_grid(growthform ~ site) +
  theme_5oes() +
  labs(x="Size (cm)", y="Density")+
  theme (axis.text.y=element_text(margin=margin(r=-3),size=11) ,
         axis.text.x=element_text(size=11,margin=margin(r=-15),),
         panel.grid.major.y=element_blank(),
         panel.grid.major.x = element_line(colour="grey91", size=0.3), 
         panel.spacing=unit(2,"lines"),
         strip.text.x=element_text(face="bold", size=13),
         strip.text.y=element_text(angle=0, face="italic"),
         legend.position="none"          )

densityplot


ggsave("densitydistr.png", plot =densityplot, width = 50, height = 30, units="cm", bg="white")  

## for partial italics/bold for levels of factors ##

levels(tb_density_sc_summary$growthform) <-
  c("Acropora Table"="bolditalic ('Acropora')* bold(' Table')",
    "Coral Branching" ="bold('Coral Branching')",
    "Coral Encrusting"="bold('Coral Encrusting')",
    "Coral Submassive"= "bold('Coral Submassive')",
    "Coral Massive"= "bold('Coral Massive')",
    "Other"="bold('Other')")

plot<- ggplot( subdata, aes(x=reorder(sizeclass,desc(sizeclass)), y=avgdensity, fill=factor(growthform))) + 
    geom_bar(stat="identity", show.legend=FALSE) +  
    geom_errorbar(aes(ymin=avgdensity-se, ymax=avgdensity+se),width=0.2, colour="grey21", alpha=0.8)+ 
    scale_fill_manual(values=c("bolditalic ('Acropora')* bold(' Table')" = "#c83737",
                               "bold('Coral Branching')"= "#2ca089",
                               "bold('Coral Encrusting')"  ="#62b6cb",
                               "bold('Coral Submassive')"     ="#cae9ff",
                               "bold('Coral Massive')" = "#275390",
                               "bold('Other')" = "#dedede")) + ....

Facet

If scales and space are free, then the mapping between position and values in the data will be the same across all panels. This is particularly useful for categorical axes.


Facet Wrap

 ggplot( subdata, aes(x=reorder(sizeclass,desc(sizeclass)), y=n, fill=factor(growthform))) + 
    geom_bar(stat="identity", show.legend=FALSE) +  
    scale_fill_manual(values=c("bolditalic ('Acropora')* bold(' Table')" = "#c83737",
                               "bold('Coral Branching')"= "#2ca089",
                               "bold('Coral Encrusting')"  ="#62b6cb",
                               "bold('Coral Submassive')"     ="#cae9ff",
                               "bold('Coral Massive')" = "#275390",
                               "bold('Other')" = "#dedede")) +

    facet_wrap(~growthform,ncol=1, drop=FALSE, labeller = label_parsed) +
    scale_x_discrete(labels=sizelabels) +
    scale_y_continuous(limits = c(0,80))+
    
    geom_hline(yintercept=0, size=1, color="grey30")+
    theme_5oes() +
    coord_flip() +
    labs (y=expression(paste("Count")), x= "Size Class",title=paste(subdata$site) ) +
    theme (axis.text.y=element_text(margin=margin(r=-3)) ,
           axis.text.x=element_text(size=10,margin=margin(r=-15)),
           panel.grid.major.y=element_blank(),
           panel.grid.major.x = element_line(colour="grey91", size=0.3), #how close the axis labels are to the line, t=top         panel.spacing=unit(2,"lines"),
           panel.spacing=unit(2,"lines"),
           strip.text.x=element_text(face="bold"),
           plot.title=element_text(face="bold")

Facet Grid

compiled_avgmean<-  ggplot(abundance, aes(x=reorder(species,desc(species)), y=avgmean, fill=factor(family))) +
  geom_bar(stat="identity", width=0.7, show.legend=FALSE, position="dodge") +  #dodge adjusts how close the bars are to each other
  
  # scale=free sets it so x and y axis are not fixed. you can call which axis to lock by setting "scale=free_x" or  "scale=free_y"
  facet_grid(family~site, space="free", scale="free", labeller=labeller(site=ID_label)) +
  geom_errorbar(aes(ymin=avgmean-se, ymax=avgmean+se),width=0.2, colour="grey21", alpha=0.8, position=position_dodge(.9))+  #error bars
  scale_fill_manual(values=c("Acanthuridae"= "#a9ddd6",
                             "Balistidae"="#7a8b99",
                             "Chaetodontidae"="#91adc2",
                             "Dasyatidae"="#7bdff2",
                             "Haemulidae"="#9ba0bc",
                             "Labridae" = "#c1b8c8",
                             "Lethrinidae"= "#68a691",
                             "Lutjanidae"="#ffe5d4",
                             "Pomacanthidae"="#efc7c2",
                             "Scaridae"="#70a288",
                             "Serranidae"="#e26d5c",
                             "Siganidae"="#694f5d")) +
  
  geom_hline(yintercept=0, size=1, color="grey30")+
  theme_5oes() +
  coord_flip() +
  labs(y=expression(paste('Abundance/250m'^'2')), x='Species') +
  theme (axis.text.y=element_text(face="italic",margin=margin(r=-3)) ,
         axis.text.x=element_text(margin=margin(r=-15)),
         panel.grid.major.y=element_blank(),
         panel.grid.major.x = element_line(colour="grey", size=0.3), #how close the axis labels are to the line, t=top
         strip.text=element_text(face="bold.italic"),
         panel.spacing=unit(1, "lines"),
         strip.text.y=element_text(angle=0, face="bold"),
         strip.background.y=element_rect(color="#7899be",fill="white", size=0.5, linetype="dotted"),
         plot.title=element_text(hjust=0.5)
  ) 


compiled_avgmean

Combining plots

library(ggpubr)
library(cowplot)

sizeclass_all <- ggarrange(plotlist=sc_gf, nrow=1)

sizeclass_all


ggsave("sizeclass_all.png", plot =sizeclass_all, width = 40, height = 30, units="cm", bg="white")  

Saving plots as a .pdf

ggsave(sizeclass_all, filename="sizeclass.pdf", device=cairo_pdf, width=40, height=40, units="cm")

Functions

Function to create a ggplot for every level in a factor defined

#determine factor levels

factor_lev<- levels(data$factor)

#set up folder to store figures ************************* this will be the same as the working directory, so just
# copy and paste, but make sure to add the "/" at the end
folder<-"[file directory]"

#lapply for each family entry

test <- lapply (factor_lev, function (xxxx) { 
  
  # subset data by each entry in the defined factor
  subdata <-subset(data, data$factor == xxxx )
  
  # create plot
  
  ggplot(subdata, aes(x=reorder(species,desc(species)), y=avgmean)) +
    geom_bar(stat="identity", width=0.2, show.legend=FALSE, fill="#7899be") +  
    facet_wrap(~site, labeller=labeller(site=ID_label)) +
    geom_errorbar(aes(ymin=avgmean-se, ymax=avgmean+se),width=0.2, colour="grey21", alpha=0.8) +
    geom_hline(yintercept=0, size=1, color="grey30")+
    theme_5oes() +
    coord_flip() +
    labs(y=expression(paste('Abundance/250m'^'2')), x='Species', title=paste(subdata$family))+
    theme (axis.text.y=element_text(face="italic",margin=margin(r=-3)) ,
           axis.text.x=element_text(margin=margin(r=-15)),
           panel.grid.major.y=element_blank(),
           panel.grid.major.x = element_line(colour="grey91", size=0.3), 
           strip.text=element_text(face="bold.italic"),
           panel.spacing=unit(1,"lines"),
           plot.title=element_text(hjust=0.5)
    ) 
  
  # save the plot
  
    ggsave(filename=paste0(folder,indiv, sep="_","abundance_site",".png"),
          device="png",
          width = 30 ,
          height = 15, 
          units="cm", 
          bg="white")
  
})

Creating integer axis (i.e. no decimal points)

# the code below was retrieved from: https://joshuacook.netlify.com/post/integer-values-ggplot-axis/ , 
# I still need to properly understand functions to get an idea of how the function works exactly... eek! 
# floor returns the largest integer value - less than or equal to the number/expression
# pretty function executes equally spaced round values. 
# The '...'argument is special and can contain any number of arguments. It is generally used if the number
# of arguments is unknown or in cases where the arguments will be passed on to another function

integer_breaks <- function(n = 5, ...) {
  fxn <- function(y) {
    breaks <- floor(pretty(y, n, ...))
    names(breaks) <- attr(breaks, "labels")
    breaks
  }
  return(fxn)
}

Plots

geom_bar

g_nspecies_site<- ggplot(nspecies_site,aes(x=site, y=n)) + 
  
  geom_bar(stat="identity", fill="#7899be", width=0.4) + 
  theme_5oes() +
  coord_flip() +
  labs (x='Site', y='Number of Species') +
  geom_hline(yintercept=0, size=1, color="grey30") +
  theme (axis.text.y=element_text(margin=margin(r=-15), size=13) ,
         axis.text.x=element_text(margin=margin(r=-15), size=13),
         axis.title.x=element_text(size=15),
         axis.title.y=element_text(size=15),
         panel.grid.major.y=element_blank(),
         legend.position="none",
         panel.grid.major.x = element_line(colour="grey91", size=0.3)) #how close the axis labels are to the line, t=top

g_nspecies_site

ggsave("nspecies_site.png", plot = g_nspecies_site, width = 15, height = 10, units="cm", bg="white")  

geom_col

#use when you are just showing counts, can use geom_bar but make sure you define: (stat="identity")

gcoral_size_m10a<-ggplot(coral_size_m10, aes(x=reorder(Site, desc(Site)), y=Count, fill=factor(Size)), show.legend=TRUE)+
  geom_col(aes(fill=Size)) + #if you want bars overlapping, specify position=position_dodge(width=1))
  facet_wrap(~Depth, labeller=labeller(Depth=depth_names))+
  scale_fill_manual(values=c("#a67f48","#9d5141","#798881", "#ecbbad", "#314856", "#dedede"), 
                    name="Size (cm) :  ", 
                    labels=c("320+", "161-320", "81-160", "41-80", "21-40", "11-20"))+
  geom_hline(yintercept=0, size=1, color="grey30")+
  theme_5oes()+
  coord_flip() +
  theme (axis.text.y=element_text(margin=margin(r=-3)) ,
         axis.text.x=element_text(margin=margin(r=-15)),
         panel.grid.major.y=element_blank(),
         panel.grid.major.x = element_line(colour="grey91", size=0.3), #how close the axis labels are to the line, t=top
         strip.text=element_text(face="bold.italic"),
          )

geom_segment

#color coded by site sections in descending value fashion

#reorder sites
cor_site$Site <- factor(cor_site$Site, levels=c("W1", "W2", "W3", "W4", "W5", "W6", "W7", "W8", "W9", "E1",
                                                "E2", "E3", "E4", "E4.1", "E5", "E6", "E6.1", "E7","E8","E8.1", 
                                                "S1"))

#establish color of groups
group.colors <-c(W="#f76b7a", E="#732324", S="#e39bb0")

gcor_site <- cor_site %>%
  
  mutate (Site=fct_reorder(Site, Species)) %>%
  
  ggplot(aes(x=Site ,y=Species)) +
  geom_segment(aes(x=Site, xend=Site, y=0, yend=Species), color="#f5d0c5") +
  geom_point(aes(color=Group),size=4, alpha=0.5, shape=16 ) + #shape16 has no outline
  scale_color_manual(values=group.colors, name="Site Position", 
                     breaks=c("W", "E", "S"), 
                     labels=c("West", "East", "South")) +
  geom_hline(yintercept=0, size=0.5, color="grey50")+
  theme_5oes() +
  coord_flip() +
  theme(
    panel.grid.major.y = element_blank(),
    panel.border = element_blank(),
    axis.ticks.y = element_blank(),
    axis.text.y=element_text(margin=margin(r=-10)) ,
    axis.text.x=element_text(margin=margin(r=-15)),
    panel.grid.major.x = element_line(colour="grey91", size=0.3),
    legend.position=c(0.9,0.2)
  )

gcor_site 

geom_count

# good for use with two categorical axis

#color and size of bubble considered

g_nsp_fam_site<-ggplot(nsp_fam_site_all, aes(x=site, y=reorder(family, desc(family)))) +
  geom_count(aes(color=ifelse(avg==0, NA, avg), size=ifelse(avg==0, NA, avg)), alpha=0.9, shape=16, show.legend=FALSE) +   #alpha is the transparency of the circles
  scale_colour_gradient(low="#37C8AB", high="#3771C8", na.value=NA) + #sets the color of circles according to the data
  scale_size(range=c(5,15),  guide=FALSE) + #sets how big the circles will be to according to the data
  theme_5oes() +
  geom_text(data=subset(nsp_fam_site_all, avg>=1 & avg<25 ), aes(x=site, y=family, label=avg), color="white", size=2.5, fontface="bold")+  #add a text in the circles, for all abundances > 5
  geom_text(data=subset(nsp_fam_site_all, avg<1), aes(x=site, y=family, label=avg), color="white", size=2, fontface="bold")+  #add a text in the circles, for all abundances > 5
  geom_text(data=subset(nsp_fam_site_all, avg>=25), aes(x=site, y=family, label=avg), color="white", size=4, fontface="bold")+  #add a text in the circles, for all abundances > 5
  
  labs (y="Family", x="Site")+
  theme(
    legend.position = "top" 
    
  ) 

g_nsp_fam_site

ggsave("nsp_fam_site.png", plot = g_nsp_fam_site, width = 15 ,height = 20, units="cm", bg="white")

geom_violin + geom_jitter

#rename depth
depth_names<-c(
  "RF"= "Reef Flat",
  "MR"= "Mid-Reef",
  "LS"="Lower Slope"
)

gcoral_size_less10_3a<-ggplot(coral_size_less10_3, aes(x=reorder(Site, desc(Site)),y=Size)) +
  geom_violin(aes(fill=Depth2), colour="white" )+
  geom_jitter(colour="grey25",size=0.35, alpha=0.3, width=0.15, height=0.7)+ 
  #width and height= degree of jitter in x and y direction
  scale_fill_manual(values=c("#dedede","#aac9b4","#7899be"), 
                    name="Depth: ",
                    labels=c("Reef Flat", "Mid-Reef Slope", "Lower Slope")) +
  facet_wrap(~Depth2, labeller=labeller(Depth2=depth_names))
  geom_hline(yintercept=0, size=1, color="grey30") +
  theme_5oes() +
  coord_flip() +
  theme (axis.text.y=element_text(margin=margin(r=-3)) ,
         axis.text.x=element_text(margin=margin(r=-15)),
         panel.grid.major.y=element_blank(),
         panel.grid.major.x = element_line(colour="grey91", size=0.3), 
         strip.text=element_text(face="bold.italic"), 
         panel.spacing=unit(2,"lines")
        )

GeomViolin Figure

geom_density_ridges

#great to show distribution
library(ggridges)

#this plot will show the points under the ridges
xxxx<-ggplot(data, aes(y=reorder(site, desc(site)), x=tree_height)) +
  geom_density_ridges2(
    alpha=0.6, fill="#2ca05a", 
    quantile_lines=T, quantiles= 2, 
    jittered_points = TRUE, scale = .95, rel_min_height = .01,
    point_shape = "|", point_size = 3, size = 0.25,
    position = position_points_jitter(height = 0)) +
  labs(x="Tree Height (cm)", y="Plot")+
  geom_label (data=trees_data %>% group_by(plot) %>%
                summarise (tree_height=median(tree_height)),
              aes (label=tree_height), 
              position=position_nudge(y=0.25), size=2.5) +

  scale_x_continuous (expand=c(0,0)) +
  scale_y_discrete (expand =c (0,0)) +
  coord_cartesian(clip="off") +
  theme_ridges()+
  theme_5oes() +
  theme(
   axis.text.x=element_text(margin=margin(t=10))) 

xxxx

ggsave("mg_height_density.png", plot = xxxx, width = 25, height = 20, units="cm", bg="white")

pyramid figures

gdemographic<-ggplot(demographic, aes(x = reorder(Item, desc(Item)), y = ifelse(Nationality=="n_international", -count, count), fill = Nationality)) +   # Fill column
  geom_hline(yintercept=10, size=0.3, color="grey91") +
  geom_hline(yintercept=20, size=0.3, color="grey91") +
  geom_hline(yintercept=30, size=0.3, color="grey91") +
  geom_hline(yintercept=40, size=0.3, color="grey91") +
  geom_hline(yintercept=-10, size=0.3, color="grey91") +
  geom_hline(yintercept=-20, size=0.3, color="grey91") +
  geom_hline(yintercept=-30, size=0.3, color="grey91") +
  geom_hline(yintercept=-40, size=0.3, color="grey91") +
  geom_bar(stat = "identity", width = .6) +   # draw the bars
  
  geom_text(data =~subset (., Nationality=="n_international"), 
            aes(label=count, color=Nationality, size=2.5, fontface="bold"), hjust=-0.4) +
  geom_text(data =~subset (., Nationality=="n_citizen"), 
            aes(label=count, color=Nationality, size=2.5, fontface="bold"), hjust=1.5) +
  scale_color_manual(values=c("white", "white")) +
  
  
  scale_y_continuous(limits=c(-50,50),
                     breaks = seq(-50,50, length.out=21),   # Breaks
                     labels = c('50','45','40','35','30','25','20','15','10','5','0','5','10','15','20','25','30','35','40','45','50')) + 
  coord_flip() +  
  labs(x= "Demographics", y=" Count") +
  theme_tufte(base_size = 14, base_family="Open Sans") +
  theme(plot.title = element_text(hjust = .5), 
        axis.ticks = element_blank(),
        legend.position="none" ,
        axis.title.x=element_text(margin=margin(t=10)),
        axis.title.y=element_blank()
        ) +
  scale_fill_manual(values=c("#FFCD00",'#7899be'))

radar charts

library(fmsb)

# Set minimum, and maximum values (this needs to be part of the data frame)
Min.R <- rep(0,4) # 0 is the value, 4 is the number of columns
Max.R <- rep(5,4) 

coral <-area %>%
  select(newname, livecoral, coraldiv,softcoral, rdeadcoral,ldeadcoral, coralsize, coralbleach, coraldisease) %>%
`row.names<-`(., NULL) %>%   # set row names as NULL
  column_to_rownames(var = "newname") #convert column to row names. 


labels=c("Live\nCoral Cover", "Coral Diversity", "Soft Coral", "Recently\nDead Coral", "Long\nDead Coral", "Coral Sizes", 
         "Coral\nBleaching", "Coral Diseases")


color=c("#f7a399")


lapply(1:19, function(i) { 
  svg(filename=paste("coral_",rownames(coral[i,]),".svg", sep=""), width=12,height=10) #svg offers best resolution
  
  radarchart(rbind(Max.R, Min.R, coral[i,]), 
             axistype=1,
             cglcol="grey", cglty=2, axislabcol="#2b2d42", 
             caxislabels=seq(0,5,1),
             seg=length(seq(0,5,1))-1,
             cglwd=1,
             vlabels=labels,
             pcol= color,
             pfcol=scales::alpha(color,0.5),
             centerzero=TRUE ,
             calcex=1.5 ,   #axis labels
             palcex=1,
             vlcex=1.5,  # category labels
             cex.main=3,
             title= paste(rownames(coral[i,]),""))
  dev.off()
})

Plotly

Generate figure as usual with ggplot, then call it in.

More information on y-axis spacing: https://stackoverflow.com/questions/43229013/plotly-r-setting-the-spacing-between-axis-label-and-axis-ticks-labels

More information on dedicated laytout attributes for specific manipulations: https://plotly.com/r/reference/

See https://rpubs.com/s_edith/plotlytrial for examples of plotly figures

library(ggplot2)
library(plotly)

# First make ggplot
g_cor_species<- ggplot(cor_species,aes(x=reorder(Genus, desc(Genus)), y=Total,
                                       text=paste("Genus: ", Genus, 
                                                  "<br>Count: ", Total)), show.legend=TRUE) + 
  geom_bar(stat="identity", fill="#f76b7a") + 
  theme_5oes() +
  coord_flip() +
  labs (x='Genus', y='Number of Species') +
  geom_hline(yintercept=0, size=1, color="grey30") +
  theme (axis.text.y=element_text(margin=margin(r=-15)) ,
         axis.text.x=element_text(margin=margin(r=-15)),
         panel.grid.major.y=element_blank(),
         panel.grid.major.x = element_line(colour="grey91", size=0.3)) 

# Call in ggplot from previous code, see "text" refereced above to highlight values
plotly<-ggplotly(g_cor_species, tooltip=c("text")) %>%
  layout(yaxis = list(title = paste0(c(rep("&nbsp;", 20),   #adjust y-axis title and spacing
                                "Genus",
                                rep("&nbsp;", 20),
                                rep("\n&nbsp;", 2)),
                              collapse = "")))

#alternative method to adjusting y-axis title and spacing
plotly<-ggplotly(g_cor_species, tooltip=c("text")) %>%
  layout(yaxis = list(title = list(text="Genus", 
                                   standoff=10L)))