Coder Social home page Coder Social logo

ggcor's Introduction

ggcor

Lifecycle: experimental

The goal of ggcor is to to provide a set of functions that be used to visualize simply and directly a correlation matrix based on ‘ggplot2’.

Installation

Now ggcor is not on cran, You can install the development version of ggcor from GitHub with:

# install.packages("devtools")
devtools::install_github("houyunhuang/ggcor")

Draw correlation plot quickly

This is a basic example which shows you how to draw correlation plot quickly:

library(ggplot2)
library(ggcor)
quickcor(mtcars) + geom_colour()

quickcor(mtcars, type = "upper") + geom_circle2()

quickcor(mtcars, cor.test = TRUE) +
  geom_square(data = get_data(type = "lower", show.diag = FALSE)) +
  geom_mark(data = get_data(type = "upper", show.diag = FALSE), size = 2.5) +
  geom_abline(slope = -1, intercept = 12)

Mantel test plot

library(vegan)
library(dplyr)
data("varechem")
data("varespec")
set.seed(20191224)

corr <- fortify_cor(varechem, cor.test = TRUE, type = "upper")
mantel <- mantel_test(varespec, varechem,
                      spec.select = list(Spec01 = 1:7,
                                         Spec02 = 8:18,
                                         Spec03 = 19:37,
                                         Spec04 = 38:44)) %>% 
  combination_layout(cor_tbl = corr) %>% 
  mutate(xend = xend + 1,
         rd = cut(r, breaks = c(-Inf, 0.2, 0.4, Inf),
                  labels = c("< 0.2", "0.2 - 0.4", ">= 0.4")),
         pd = cut(p.value, breaks = c(-Inf, 0.01, 0.05, Inf),
                  labels = c("< 0.01", "0.01 - 0.05", ">= 0.05")))

options(ggcor.link.inherit.aes = FALSE)
quickcor(corr) + geom_square() +
  geom_link(aes(colour = pd, size = rd), data = mantel,
            curvature = 0.05) +
  geom_link_point(data = mantel) +
  geom_start_label(aes(x = x - 0.5), hjust = 1, data = mantel) +
  scale_size_manual(values = c(0.5, 1, 2)) +
  scale_colour_manual(values = c("#D95F02", "#1B9E77", "#A2A2A288")) +
  guides(size = guide_legend(title = "Mantel's r",
                             override.aes = list(colour = "grey35"), 
                             order = 2),
         colour = guide_legend(title = "Mantel's p", 
                               override.aes = list(size = 3), 
                               order = 1),
         fill = guide_colorbar(title = "Pearson's r", order = 3)) +
  expand_axis(x = -6)

network

library(tidygraph)
library(ggraph)
net <- fast_correlate(varespec) %>% 
  as_tbl_graph(r.thres = 0.5, p.thres = 0.05) %>% 
  mutate(degree = tidygraph::centrality_degree(mode = "all"))

ggraph(net, "circle") + 
  geom_edge_fan(aes(edge_width = r, edge_linetype = r < 0), 
                edge_colour = "grey80", show.legend = FALSE) +
  geom_node_point(aes(size = degree, colour = name), 
                  show.legend = FALSE) +
  geom_node_text(aes(x = x * 1.08, y = y * 1.08, 
                     angle = -((-node_angle(x, y) + 90) %% 180) + 90, 
                     label = name), size = 4, hjust= "outward",
                 show.legend = FALSE)  +
  scale_edge_color_gradientn(colours = c("blue", "red")) +
  scale_edge_width_continuous(range = c(0.1, 1)) +
  coord_fixed(xlim = c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
  theme_graph()

ggcor's People

Contributors

hy4m avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.