Search
Question: How is it possible to get a legend outside plotMDS?
0
10 months ago by

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)


modified 10 months ago by James W. MacDonald47k • written 10 months ago by mictadlo0
4
10 months ago by
United States
James W. MacDonald47k wrote:

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
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")[2], par("usr")[4], c("Grp1","Grp2"), pch = 16, col = c("black","red"), bty = "n") ## set par back to original par(opar) ADD COMMENTlink modified 10 months ago by Gordon Smyth34k • written 10 months ago by James W. MacDonald47k 1 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))))
ggplot(toplot, aes(Dim1, Dim2, colour = Group)) + geom_point()