Colors to Plotting PCA (2D) by R
0
0
Entering edit mode
2.2 years ago
hosin • 0

Hi there

I want to design a PCA plot by R. Repeatedly I have received this error:

Error in plot.xy(xy, type, ...) : invalid color name 'PopColor'

Popcolor is a column in the popGroups file with this format:

Pop2  Symbol  PopColor  PopColorBg
AFF   0       black     gray
AFF   0       black     gray
ALL   1       red       black
ALL   1       red       black
ALL   1       red       black
AMM   2       green     blue
AMM   2       green     blue
AMM   2       green     blue
AMM   2       green     blue

I would be grateful if someone can help me to solve that problem!

R plot pca • 1.1k views
ADD COMMENT
0
Entering edit mode

If you want help, you need to do more than this. What command are you using to turn this into a plot?

ADD REPLY
0
Entering edit mode

Hi swbarnes2, Thanks for your respond and your consideration. The R code is:

library(rgl)

fn <- "D:/DD/data/PCA/RUNPCA/plot/eigenvec2.txt"
evecDat <- read.table(fn, skip = 1, col.names = c("Sample", "PC1", "PC2", "Pop"))
indTable = read.table("D:/DD/data/PCA/RUNPCA/plot/MinSS_regions.txt", col.names = c("Sample", "Sex", "Pop2"))

layout(matrix(c(1, 2), ncol = 1), heights = c(1.5, 1))
par(mar = c(4, 4, 0, 0))

merged1EvecDat = merge(evecDat, indTable, by = "Sample")
popGroups = read.table("D:/DD/data/PCA/RUNPCA/plot/popGroups.txt", col.names = c("Pop2", "Symbol", "PopColor", "PopColorBg"))
mergedEvecDat = merge(merged1EvecDat, popGroups, by = "Pop2", all = TRUE)

# Now do some corrections : convert to characters colours (if they are text) and put a default colour for those undefined (in this case "gray")

mergedEvecDat$PopColor <- as.character(mergedEvecDat$PopColor)
mergedEvecDat$PopColorBg <- as.character(mergedEvecDat$PopColorBg)
mergedEvecDat$PopColor[is.na(mergedEvecDat$PopColor)] <- "gray"
mergedEvecDat$Symbol[is.na(mergedEvecDat$Symbol)] <- 9
#mergedEvecDat$PopColor[is.na(mergedEvecDat$PopColor)] <- "gray"

pdf(
  file = "D:/DD/data/PCA/RUNPCA/plot/MyPlot.pdf",
  width = 50,
  height = 25
)

plot(mergedEvecDat$PC2, mergedEvecDat$PC1,
  main = "Name_Of_The_Plot",
  xlim = c(-0.18, 0.16),
  ylim = rev(range(0.11, -0.14)),
  # The plot code continues with symbols used, background, and colour
  pch = mergedEvecDat$Symbol,
  col = mergedEvecDat$PopColor,
  bg = mergedEvecDat$PopColorBg,
  cex = 1, cex.axis = 0.9, cex.lab = 0.9,
  xlab = "PC2", ylab = "PC1")

plot.new()
dev.copy(jpeg,filename="plot.jpg");
par(mar = rep(0, 4))
legend("bottom",
  legend = mergedEvecDat$Pop2,
  pch = mergedEvecDat$Symbol,
  col = mergedEvecDat$PopColor,
  pt.bg = mergedEvecDat$PopColorBg,
  ncol = 14,
  cex = 0.9)
dev.off()
ADD REPLY

Login before adding your answer.

Traffic: 1877 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6