Search
Question: How is it possible to get a legend outside plotMDS?
0
gravatar for mictadlo
12 days ago by
mictadlo0
mictadlo0 wrote:

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.

ADD COMMENTlink modified 12 days ago by James W. MacDonald44k • written 12 days ago by mictadlo0
4
gravatar for James W. MacDonald
12 days ago by
United States
James W. MacDonald44k 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
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 COMMENTlink modified 12 days ago by Gordon Smyth32k • written 12 days ago by James W. MacDonald44k
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))))
library(ggplot2)
ggplot(toplot, aes(Dim1, Dim2, colour = Group)) + geom_point()
ADD REPLYlink written 12 days ago by James W. MacDonald44k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.2.0
Traffic: 198 users visited in the last hour