jfq3/ggordiplots

Customize_gg_ordisurf

Closed this issue · 1 comments

Great code, would it be available on CRAN?.

I would like to customize my plot. This is probably a bit naive, but any help would be greatly appreciated:

1- Ticken curves,
2- colour dots by groups

p= gg_ordisurf(bird.nmds, env.var = bird.all.xy$Elevation, var.label="Elevation")

p$plot +theme_bw(base_size = 19) +
theme(panel.grid = element_blank(),
legend.position="none")+
xlim(-1, 1.2)+ ylim(-0.5,0.5)+
scale_colour_gradient(low = "green", high = "red", na.value = NA)+
labs(x=paste0("NMDS-1"),
y=paste0("NMDS-2 "), title = "Bird communities")

image

jfq3 commented

Changing the line size does not seem to work correctly at this time. You should be able to change it with a size = some value when plotting the stat_contour. The way-around is to save the plot to a file, playing with the dimensions until you are satisfied with it.

library(vegan)
#> Loading required package: permute
#> Loading required package: lattice
#> This is vegan 2.6-2
library(ggordiplots)
#> Warning: package 'ggordiplots' was built under R version 4.2.1
#> Loading required package: ggplot2
#> Loading required package: glue

data(varespec)
data(varechem)

# Add groups to your env file so you can color symbols
varechem$groups <- c(rep("A", 6), rep("B", 6), rep("C", 6), rep("D", 6))
head(varechem)
#>       N    P     K    Ca    Mg    S    Al   Fe    Mn   Zn  Mo Baresoil Humdepth
#> 18 19.8 42.1 139.9 519.4  90.0 32.3  39.0 40.9  58.1  4.5 0.3     43.9      2.2
#> 15 13.4 39.1 167.3 356.7  70.7 35.2  88.1 39.0  52.4  5.4 0.3     23.6      2.2
#> 24 20.2 67.7 207.1 973.3 209.1 58.1 138.0 35.4  32.1 16.8 0.8     21.2      2.0
#> 27 20.6 60.8 233.7 834.0 127.2 40.7  15.4  4.4 132.0 10.7 0.2     18.7      2.9
#> 23 23.8 54.5 180.6 777.0 125.8 39.5  24.2  3.0  50.1  6.6 0.3     46.0      3.0
#> 19 22.8 40.9 171.4 691.8 151.4 40.8 104.8 17.6  43.6  9.1 0.4     40.5      3.8
#>     pH groups
#> 18 2.7      A
#> 15 2.8      A
#> 24 3.0      A
#> 27 2.8      A
#> 23 2.7      A
#> 19 2.7      A

vare.dist <- vegdist(varespec, method = "bray")
set.seed(123)
vare.mds <- monoMDS(vare.dist)

plot_data <- gg_ordisurf(ord = vare.mds, env = varechem$Al, groups = varechem$groups, plot = FALSE)

# Get data frames for two geoms, geom_point and geom_contour
df_ord <- plot_data$df_ord
df_surf <- plot_data$df_surf

# This is how binwidth is calculated if it is not given
r <- range(varechem$Al)
binwidth <- (r[2] - r[1])/15
binwidth
#> [1] 28.2

var.label <- "Aluminum"

ggplot() +
  geom_point(data = df_ord, aes(x = x,  y = y, fill = Group),
             shape = 21,
             color = "#00000000",
             size = 3) +
  stat_contour(data = df_surf, aes(x = x, y = y, z = z, color = ..level..),
               binwidth = binwidth,
               show.legend = FALSE) +
  scale_colour_gradient(low = "green", high = "red", na.value = NA) +
  labs(x = "NMDS 1",
       y = "NMDS 2",
       title = "My Plot Title") +
  coord_fixed(ratio = 1)

Created on 2022-10-17 by the reprex package (v2.0.1)