how about this?
I use par(mar())
library(tsne)
set.seed(202106)
iris.tsne <- tsne(iris[,1:4])
par(mar=c(3,3,3,6))
plot(iris.tsne,col=as.factor(iris$Species),type='p', main="tsne")
par(xpd=T)
legend("topleft",
legend=c("1", "2", "3"),
pch=c(1,1,1),
col=c(1, 2, 3)
)
legend(par()$usr[2],
par()$usr[4],
legend=c("1", "2", "3"),
pch=c(1,1,1),
col=c(1, 2, 3))
legend(x=par()$usr[2],
y=par()$usr[3],
legend=c("1", "2", "3"),
pch=c(1,1,1),
col=c(1, 2, 3),
xjust=-.1,
yjust=0,
x.intersp=2,
y.intersp=1.5)
but I always use ggplot2.
Because it's beautiful and there are various options for handling the legend.
library(tidyverse)
iris.tsne %>%
as.data.frame() %>%
mutate(type=as.factor(iris$Species)) %>%
ggplot(aes(x=V1,y=V2,color=type))+
geom_point()