How is it possible to get a legend outside plotMDS?
1
0
Entering edit mode
Last seen 2.7 years ago

How is it possible to get a legend outside plotMDS?

pch <- c(0,1,2,15,16,17)
colors <- rep(c("darkgreen", "red", "blue"), 2)
plotMDS(y, col=colors[group], pch=pch[group])
legend("topleft", legend=levels(group), pch=pch, col=colors, ncol=2)


Thank you in advance.

edger ggplot2 • 4.2k views
4
Entering edit mode
@james-w-macdonald-5106
Last seen 6 hours ago
United States

Using the example for plotMDS:

sd <- 0.3*sqrt(4/rchisq(1000,df=4))
x <- matrix(rnorm(1000*6,sd=sd),1000,6)
rownames(x) <- paste("Gene",1:1000)
x[1:50,4:6] <- x[1:50,4:6] + 2
## capture par settings, then add space to the right
opar <- par(no.readonly = TRUE)
par(xpd = TRUE, mar = par()$mar + c(0, 0, 0, 5)) ## get yer plot on plotMDS(x, pch = 16, col = c(rep("black",3), rep("red",3))) legend(par("usr"), par("usr"), c("Grp1","Grp2"), pch = 16, col = c("black","red"), bty = "n") ## set par back to original par(opar) ADD COMMENT 2 Entering edit mode Or alternatively, if you are a fan of the tidyverse, mds <- plotMDS(x) toplot <- data.frame(Dim1 = mds$x, Dim2 = mds\$y, Group = factor(paste0("Grp", rep(1:2, each = 3))))
library(ggplot2)
ggplot(toplot, aes(Dim1, Dim2, colour = Group)) + geom_point()