0

I have my data

varechem <-
  structure(
    list(
      `POX-C` = c(
        869.153225806452,
        841.409274193548,
        720.344758064516,
        828.798387096774,
        904.46370967742,
        773.310483870968,
        793.487903225806,
        874.197580645161,
        900.932661290323,
        778.354838709677
      ),
      `B-glucosidase` = c(
        1.90612612612613,
        1.60509009009009,
        1.42864864864865,
        1.82355855855856,
        1.76761261261261,
        1.34855855855856,
        1.37504504504504,
        1.5863963963964,
        1.1290990990991,
        1.4686036036036
      ),
      Protein = c(
        6284.21052631579,
        6250.52631578947,
        6103.15789473684,
        6280,
        6275.78947368421,
        4368.42105263158,
        1240,
        6191.57894736842,
        5745.26315789474,
        6970.52631578947
      )
    ),
    row.names = c(
      "M.T1.R1.S1.16S.S50",
      "M.T1.R1.S2.16S.S62",
      "M.T1.R1.S3.16S.S74",
      "M.T1.R2.S1.16S.S86",
      "M.T1.R2.S2.16S.S3",
      "M.T1.R2.S3.16S.S15",
      "M.T1.R3.S1.16S.S27",
      "M.T1.R3.S2.16S.S39",
      "M.T1.R3.S3.16S.S51",
      "M.T1.R4.S1.16S.S63"
    ),
    class = "data.frame"
  )

varespec <-
  structure(
    list(
      A = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
      B = c(1,
            1, 1, 1, 1, 1, 1, 1, 1, 1),
      C = c(1, 1, 1, 2, 1, 1, 1, 1, 1,
            3),
      D = c(2, 1, 1, 1, 1, 1, 1, 1, 1, 1),
      E = c(1, 1, 1, 1, 1,
            3, 1, 1, 1, 1),
      F = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
      G = c(1,
            1, 11, 20, 15, 13, 23, 9, 1, 16),
      H = c(2, 1, 1, 4, 1, 1, 1,
            1, 1, 1),
      I = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
      J = c(9, 3, 20,
            21, 16, 19, 22, 13, 12, 26)
    ),
    row.names = c(
      "M.T1.R1.S1.16S.S50",
      "M.T1.R1.S2.16S.S62",
      "M.T1.R1.S3.16S.S74",
      "M.T1.R2.S1.16S.S86",
      "M.T1.R2.S2.16S.S3",
      "M.T1.R2.S3.16S.S15",
      "M.T1.R3.S1.16S.S27",
      "M.T1.R3.S2.16S.S39",
      "M.T1.R3.S3.16S.S51",
      "M.T1.R4.S1.16S.S63"
    ),
    class = "data.frame"
  )

I have my codes:

library(ggplot2); library(vegan)
sol <- cca(varespec, varechem)
scrs<-scores(sol,display=c("sp","wa","lc","bp","cn"))
df_sites <- data.frame(scrs$sites)
df_sites$Sites <- gsub("\\..*", "", rownames(varechem))

df_sites$Sites <- factor(df_sites$Sites)
# rownames(df_sites) <- gsub("[*].*$", "",rownames(df_sites))
colnames(df_sites)<-c("x","y","Sites")

#Draw sites
p<-ggplot()
p<-p+geom_point(data=df_sites,aes(x,y,colour=Sites), shape = "diamond", size = 2) 
p <- p + scale_colour_manual(values = c("blue"), guide = FALSE)
p

#Draw biplots
multiplier <- vegan:::ordiArrowMul(scrs$biplot)

df_arrows<- scrs$biplot*multiplier
colnames(df_arrows)<-c("x","y")
df_arrows=as.data.frame(df_arrows)


#adding arrows for chemicals (environment variables)
pa<-p+geom_segment(data=df_arrows, aes(x = 0, y = 0, xend = x, yend = y),
                   arrow = arrow(length = unit(0.3, "cm")), arrow.fill = "black")
pa

###adjust the position of the labels or shapes
df_arrows <- as.data.frame(df_arrows*1.1)
df_arrows$Chemicals <- factor(rownames(df_arrows))
cp <- pa+geom_point(data= df_arrows, aes(x, y, group= Chemicals, shape = Chemicals), size = 4) + scale_shape_manual(values=1:nlevels(df_arrows$Chemicals)) + coord_equal()


#### # Draw species
df_species<- as.data.frame(scrs$species)
colnames(df_species)<-c("x","y")


significant_taxa <- c("A", "D")

df_species$significant <- ifelse(rownames(df_species) %in% significant_taxa, "Sig", "Not-sig")

df_species$significant <- as.character(df_species$significant)  

get.colour <- c("red", "orange")

#relevel factor so "Sig" will appear first in the legend
df_species$significant <- factor(df_species$significant, levels = c("Sig", "Not-sig"))
df_species$coloured <- "black"
df_species$coloured [match(significant_taxa, rownames(df_species))] <- get.colour
df_species$coloured <- as.factor(df_species$coloured)

library(dplyr)
df_species <- df_species %>% 
  mutate(labels = rownames(df_species))


scp <- cp+geom_point(data=df_species,aes(x=x,y=y, group = significant, size = significant))+
scale_size_manual(values =c(2.5, 0.2)) 
scp
library(ggrepel)

scp + geom_text_repel(data = subset(df_species, significant == "Sig"),
                aes(x = x, y = y, label = labels), angle = 60, size = 3)

enter image description here

I am having problem colouring only A and D text and the corresponding two data points in different colours (say green and red). How can I do this ?

1 Answer 1

1

I think your error is about the use of group = significant in the geom_point, it prevents for the definition of color. If you use this code, you will get the right plot:

ggplot() +
  geom_point(data=df_sites,aes(x,y), color = "blue", shape = "diamond", size = 2) +
  geom_segment(data=df_arrows, aes(x = 0, y = 0, xend = x, yend = y),
               arrow = arrow(length = unit(0.3, "cm")), arrow.fill = "black") +
  geom_point(data= df_arrows, aes(x, y, group= Chemicals, shape = Chemicals), size = 4) + 
  scale_shape_manual(values=1:nlevels(df_arrows$Chemicals)) + 
  coord_equal() +
  geom_point(data = df_species, aes(x = x, y = y, color = coloured, size = significant)) +
  scale_size_manual(values = c(2.5, 1)) +
  geom_text_repel(data = subset(df_species, significant == "Sig"),
                  aes(x = x, y = y, label = labels, color = coloured), angle = 60, size = 3) +
  scale_color_manual(values = c("black","orange","red"), guide = FALSE)

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.