rggplot2pca

Legend position in plotScores (Chemospec package)


I am working on infrared spectral data using the "Chemospec" package. I performed a PCA and now want to visualize the results, the function "plotScores" plots the PCA scores. I am trying to move the position of the legend but it stays in the top right corner.The function "plotScores" uses ggplot2 to visualize the data so I tried different ggplot position assignments but nothing changes.

This is the code I am using:

p <- plotScores(data_sample, pca, pcs = c(1,2), ellipse = "no", tol = 0.01, legendPos = "bottom")
p <- p & ggtitle(myt)
p <- p + theme_gray() 
p <- p + theme(legend.position = "bottom")
p

Has anyone else encountered this problem?


Solution

  • The issue is that the legend is not based on ggplot2s system of guides but added as a text annotation instead. But you can use the leg.loc= argument of plotScores to specify the position of the legend.

    Using the default example from ?plotScores:

    library("ChemoSpec")
    library("ggplot2")
    data(metMUD1)
    
    pca <- c_pcaSpectra(metMUD1)
    p <- plotScores(metMUD1, pca,
      pcs = c(1, 2), ellipse = "cls",
      tol = 0.05, leg.loc = "bottom"
    )
    p <- p + ggtitle("metMUD1 NMR Data")
    p