diff --git a/R/clustsort.R b/R/clustsort.R index e69de29..3ecbb11 100644 --- a/R/clustsort.R +++ b/R/clustsort.R @@ -0,0 +1,5 @@ +clustsort<-function(x){ + xorder<-pull(x, 1)[hclust(dist(x %>% select(-1)))$order] + yorder<-colnames(x)[2:ncol(x)][hclust(dist(t(x %>% select(-1))))$order] + return(list("x"=xorder,"y"=yorder)) +} diff --git a/R/ggheatmap.R b/R/ggheatmap.R new file mode 100644 index 0000000..11cd698 --- /dev/null +++ b/R/ggheatmap.R @@ -0,0 +1,14 @@ +ggheatmap<-function(df, x=NULL, y=NULL, value=NULL){ + if (is.null(x)){x=colnames(df)[1]} + if (is.null(y)){y=colnames(df)[2]} + if (is.null(value)){value=colnames(df)[3]} + + order<-clustsort(df %>% spread(y,value)) + + df %>% + ggplot(aes(df[,x], df[,y], fill=df[,value]))+ + scale_x_discrete(limits=order$x)+ + scale_y_discrete(limits=order$y)+ + labs(x=x, y=y)+ + theme_heatmap() +} diff --git a/R/theme_heatmap.R b/R/theme_heatmap.R new file mode 100644 index 0000000..d66325b --- /dev/null +++ b/R/theme_heatmap.R @@ -0,0 +1,8 @@ +theme_heatmap<-function(x){ + list(geom_tile(), + scale_fill_gradientn(colors=col2(200)), + theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust=0.5), + panel.background = element_blank(), + axis.ticks = element_blank()) + ) +} diff --git a/man/ggheatmap.Rd b/man/ggheatmap.Rd new file mode 100644 index 0000000..3a60db7 --- /dev/null +++ b/man/ggheatmap.Rd @@ -0,0 +1,12 @@ +\name{ggheatmap} +\alias{ggheatmap} +\title{ggheatmap} +\usage{ +ggheatmap(x) +} +\description{ +Generates a heatmap using ggplot for a dataframe. +} +\examples{ +ggheatmap() +} diff --git a/man/theme_heatmap.Rd b/man/theme_heatmap.Rd new file mode 100644 index 0000000..de15899 --- /dev/null +++ b/man/theme_heatmap.Rd @@ -0,0 +1,12 @@ +\name{theme_heatmap} +\alias{theme_heatmap} +\title{theme_heatmap} +\usage{ +theme_heatmap() +} +\description{ +Adds geom_tile, color from col2 and theme for heatmap. +} +\examples{ +g+theme_heatmap() +}