1

I am trying to use ggplot() to plot a betadispers object mod1 so that I can better control the colours.

I extracted the centroids from mod1 and I am using geom_point() for plotting the yearly replicates for each dune , geom_seg()to plot the lines for each dune-star, and a second geom_point() statement to plot the centroids. When I plot this using scale_colour_manual(values=cols, guide= FALSE) it only changes the colour of the first geom_points and the geom_seg but not the centroids.

How do I control the colour of each component separately such that the dune points are coloured by cols, the segments are coloured by cols1 and the centroids use cols2? I'd also like to change the colour of the black outline for each centroid to cols1.

library(vegan)
library(ggplot2)

cols = c("blue","red")
cols1 = c("green","dark orange")
cols2 = c("purple","yellow")

data(dune)
sites = data.frame(year = rep(c(1:5), times= 4), dune = rep(c(1:4),each=5), dune.type = rep(c("A","B"),each=10))
distances <- vegdist(dune, method = "bray")

#create Betadispersion model on betad (effectively a PCoA)
mod1 <- with(sites, betadisper(distances, dune, type = "centroid"))
s = scores(mod1)

# Get points
pnt_sites = as.data.frame(s$sites)
pnt_sites = cbind(pnt_sites, sites)

# Get centroids
pnt_centroids = as.data.frame(s$centroids)
pnt_centroids$dune = rownames(pnt_centroids)
pnt_centroids$dune.type = rep(c("A","B"),each=2)

# Calculate segments
seg = pnt_sites[, c("PCoA1", "PCoA2", "dune")]
tmp = rename(pnt_centroids, c("PCoA1" = "PCoA1_ctr", "PCoA2" = "PCoA2_ctr"))
seg = join(seg, tmp, "dune")

# Plot
ggplot() +
  geom_point(
    data = pnt_sites,
    aes(x = PCoA1, y = PCoA2, colour = dune.type, shape = dune.type),
    size = 2
  ) +
  geom_segment(
    data = seg,
    aes(x = PCoA1, y = PCoA2, xend = PCoA1_ctr, yend = PCoA2_ctr, colour = 
       dune.type)
  ) +
  geom_point(
    data = pnt_centroids,
    aes(x = PCoA1, y = PCoA2, fill = dune.type),
    size = 3, shape = 21
  ) +
  scale_colour_manual(values=cols, guide= FALSE) +
  coord_equal() +
  theme_bw() 

1 Answer 1

2

You can only specify scale_colour_manual() once per plot, not multiple times for each geom_point call, so you need to combine your centroids and sites into one dataframe (adding variables for centroid/site, and centroid A/centroid B/site A/site B), then plot as a single geom_point() layer

#combine centroids and points into one dataframe
pnt_centroids$year = NA
pnt_centroids$data.type = "centroid"
pnt_sites$data.type = "site"
sites_centroids <- rbind(pnt_centroids, pnt_sites)
sites_centroids$type <- paste(sites_centroids$data.type, sites_centroids$dune.type)

When you define vectors of colors to use for scale_fill_manual and scale_colour_manual, each will have 6 levels, to match the number of variables you have (4 point types plus 2 segment types). Your site points and segments do not have a fill attribute, so fill will be ignored when plotting those points and segments, but you still need to define 6 colors in scale_fill_manual so that your filled points for centroids plot properly.

#change the cols vector definitions at the beginning of code to this
cols.fill <- c("purple", "yellow", "purple", "yellow", "purple", "yellow")
cols.colour <- c("green", "dark orange", "green", "dark orange", "blue", "red")

Specify the new colour, fill, and shape scales in the plot code like this:

# Plot
ggplot() +
  geom_segment( #segment must go before point so points are in front of lines
    data = seg,
    aes(x = PCoA1, y = PCoA2, xend = PCoA1_ctr, yend = PCoA2_ctr, colour = dune.type)) +
  geom_point(
    data = sites_centroids,
    aes(x = PCoA1, y = PCoA2, colour = type, fill = type, shape = type), size = 2) +
  scale_colour_manual(values = cols.colour) +
  scale_fill_manual(values = cols.fill, guide = FALSE) +
  scale_shape_manual(values = c(21, 21, 16, 17)) + 
  coord_equal() +
  theme_bw() 

Here is the result. The legend gets a bit busy, it may be better to delete it and use text annotation to label the dune types.

enter image description here

Not the answer you're looking for? Browse other questions tagged or ask your own question.