Home > other >  Base R plot legend from different df than plot, help needed
Base R plot legend from different df than plot, help needed

Time:11-21

First time posting a question and relatively new to R, so bear with me please. Im building a loop that will take 2 columns at a time to calculate symmetric pivot coordinates (isometric log-ratios) from around 50 variables and produces one xy plot per loop. I have two questions regarding to the plotting. 1) I managed to get loop working and plot diagrams using base R plot, with colored sample points based on variable in column 1 (Cluster). I am struggling with the legend as while it takes the names of the cluster from the column but the colors are not produced expect for one cluster. Any help? 2) I would prefer using ggplot in this loop but it is not working, can anybody help with that? So the loop will create dataframe Z with two columns with calculated pivot coordinated from two variables in df. In the plot, these variables are plotted from Z but will need to get the names from df along with points colored based on column 1 in df.

set.seed(42)  ## for sake of reproducibility
df_syn <- as.data.frame(matrix(runif(n=50, min=1, max=20), nrow=10)) 
names(df_syn) <- c('A', 'B', 'C', 'D', 'E')
df_cr <- data.frame(Cluster=c('A', 'A', 'B', 'B', 'C', 'C', 'C', 'C', 'A', 'B'))
df_syn <- cbind(df_cr, df_syn)
D <- ncol(df_syn[, 2:6])
for (i in 2:D) {
  for (j in 2:D) {
    if (i == j) {
      plot(0, 0, type="n", xaxt="n", yaxt="n")
      text(0, 0, names(df_syn[i]))
    } else {
      Z <- robCompositions::pivotCoord(df_syn[, c(i, j, 2:D[-c(i, j)])], method="symm")
      plot(Z[, 1:2], 
           xlab=paste(colnames(df_syn[i])), ylab=paste(colnames(df_syn[j])), col=factor(df_syn$Cluster), pch=19)
      legend("bottomright", legend=unique(df_syn[, 1]), pch=19, ncol=1, col=factor(df_syn$Cluster))
    }
  }
}

CodePudding user response:

This is actually an easy fix. You specified col=factor(df_syn$Cluster), which gives 1, 1, 2, 2, 3, 3, 3, 3, 1, 2, and since just the first three elements are needed, you get black, black, red. What you want ist unique(factor(df_syn$Cluster)).

par(mfrow=c(D - 1, D - 1))
for (i in 2:D) {
  for (j in 2:D) {
    if (i == j) {
      plot(0, 0, type="n", xaxt="n", yaxt="n")
      text(0, 0, names(df_syn[i]))
    } else {
      Z <- robCompositions::pivotCoord(df_syn[, c(i, j, 2:D[-c(i, j)])], method="symm")
      plot(Z[, 1:2], 
           xlab=paste(colnames(df_syn[i])), ylab=paste(colnames(df_syn[j])), col=factor(df_syn$Cluster), pch=19)
      legend("bottomright", legend=unique(df_syn[, 1]), pch=19, ncol=1, col=unique(factor(df_syn$Cluster)))
    }
  }
}

enter image description here

  • Related