Compare commits

...

3 Commits

Author SHA1 Message Date
marcelcosta 46b6efc706 Añadida función sem. 2022-02-12 12:05:34 +01:00
marcelcosta 728b2429f6 Añadida función gglegend. 2022-02-12 12:05:15 +01:00
marcelcosta 31811bec30 Añadida función perc. 2022-02-12 12:05:04 +01:00
6 changed files with 109 additions and 0 deletions
+13
View File
@@ -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}}),...))
}
}
+7
View File
@@ -0,0 +1,7 @@
perc<-function(x, per100=T){
if (per100==T){
return(x*100/sum(x, na.rm = T))
}else{
return(x/sum(x, na.rm = T))
}
}
+4
View File
@@ -0,0 +1,4 @@
sem <- function(x, na.rm=F){
sem<-sd(x, na.rm = na.rm)/sqrt(length(x))
return(sem)
}
+43
View File
@@ -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")
}
+25
View File
@@ -0,0 +1,25 @@
\name{perc}
\alias{perc}
\title{perc}
\usage{
perc(x, per100=T)
}
\arguments{
\item{x}{A numeric vector or an R object but not a factor coercible to numeric by as.double(x).}
\item{perc}{A logical value indicating whether the result must sum 100 (TRUE) or 1 (FALSE).}
}
\description{
This function the percentages of a numeric vector.
}
\examples{
v<-c(2,5,10,3)
perc(v)
# It can be used with dplyr tables
library(tidyverse)
df<-data.frame("X"=c("A","A","B","B"), "Y"=v)
df \%>\% group_by(X) \%>\% summarise(Y=perc(Y))
# Or it can be under 1
df \%>\% group_by(X) \%>\% summarise(Y=perc(Y, per100=F))
}
+17
View File
@@ -0,0 +1,17 @@
\name{sem}
\alias{sem}
\title{sem}
\usage{
sem(x, na.rm=T)
}
\arguments{
\item{x}{A numeric vector or an R object but not a factor coercible to numeric by as.double(x).}
\item{na.rm}{A logical value indicating whether NA values should be stripped before the computation proceeds.}
}
\description{
This function computes the Standard Error of the Mean of the values in x. If na.rm is TRUE then missing values are removed before computation proceeds.
}
\examples{
v<-rnorm(10)
sem(v)
}