diff --git a/R/gglegend.R b/R/gglegend.R new file mode 100644 index 0000000..aad009f --- /dev/null +++ b/R/gglegend.R @@ -0,0 +1,13 @@ +gglegend<-function(data, x, y, var, stat="median", color="black", ...){ + if (stat == "median"){ + data<-data %>% group_by({{var}}) %>% summarise("{{x}}":=median({{x}}, na.rm=T), "{{y}}":=median({{y}}, na.rm=T)) + } + if (stat == "mean"){ + data<-data %>% group_by({{var}}) %>% summarise("{{x}}":=mean({{x}}, na.rm=T), "{{y}}":=mean({{y}}, na.rm=T)) + } + if (!is.null(color)){ + return(geom_label(data = data, aes({{x}},{{y}}, label={{var}}), color=color, ...)) + }else{ + return(geom_label(data = data, aes({{x}},{{y}}, label={{var}}),...)) + } +} diff --git a/man/gglegend.Rd b/man/gglegend.Rd new file mode 100644 index 0000000..684403d --- /dev/null +++ b/man/gglegend.Rd @@ -0,0 +1,43 @@ +\name{gglegend} +\alias{gglegend} +\title{gglegend} +\usage{ +gglegend(data, x, y, var, stat="median", color="black", ...) +} +\arguments{ + \item{data}{A data frame from where to take x and y axis, and the grouping variable.} + \item{x}{The variable that will be used for X axis in the heatmap.} + \item{y}{The variable that will be used for Y axis in the heatmap.} + \item{value}{The variable that will be used for grouping the heatmap. The labels will be taken from it} + \item{stat}{The statistical central test that will be used to calculate the label coordinates. "mean" or "median" are posible, defaulting to "median".} + \item{color}{The color of the labels, "black" by default. If NULL, nothig will be passed to the geom_label so it may be taken from aesthetics.} + \item{...}{Other arguments that will be passed to the geom_label function.} +} +\description{ +Generates a geom_label object with labels in the center of each population or agrupation. +} +\examples{ +library(tidyverse) +library(Rtsne) +library(Rphenograph) +library(igraph) + +## We will generate an example using tSNE and Phenograph algorithms, but can be applied to any other situation. + +iris_unique <- unique(iris) # Remove duplicates +iris_matrix <- as.matrix(iris_unique[,1:4]) + +# Set a seed if you want reproducible results +set.seed(42) +tsne_out <- Rtsne(iris_matrix,pca=FALSE,perplexity=30,theta=0.0) # Run TSNE + +rpheno<-Rphenograph(tsne_out$Y) +df<-data.frame(as.data.frame(tsne_out$Y), "Clust"=factor(membership(rpheno[[2]]))) +head(df) + +ggplot(df, aes(V1,V2, color=Clust))+ + geom_point()+ + gglegend(df, V1, V2, Clust)+ + guides(color="none") + +}