Rtsne Plot legend overlap and point filling in

I have been trying to visualise my data using Rtsne however I can't get my legend to appear next to my plot and I can't get the points to fill in. I have been trying various code alterations including changing the location of the legend manually:

tsne <- Rtsne(featureMatrix, perplexity=40, theta=0.5, dims=2, col=factor(conds), fill=factor(conds)+
  geom_point(size=4)+ 
  labs(title= "tSNE: PC1 vs PC2" ,x=paste0("PC1(",pca_data_perc[1],"%)"),y=paste0("PC2 (",pca_data_perc[2],"%)")) +
  theme_bw() +  
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))+
  geom_text_repel(aes(label=type$Sample), point.padding = 0.5))

set.seed(1)
plot(tsne$Y, col=factor(conds))
legend(x = 6,y = 4,
       legend = c("Control","T/P", "T/P_Alk","T/P_Imat","T/P_Combo"),
       fill = 1:5,
       border = "black")

When I just place the legend in the x = "topright"

When I have moved the legend manually:

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()

1 Like