How is it possible to get a legend outside plotMDS?
1
0
Entering edit mode
mictadlo ▴ 10
@mictadlo-10885
Last seen 4.3 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 • 6.3k views
ADD COMMENT
4
Entering edit mode
@james-w-macdonald-5106
Last seen 5 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")[2], par("usr")[4], 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()
ADD REPLY

Login before adding your answer.

Traffic: 772 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