Define fortify and autoplot functions to allow ggplot2 to handle some popular R packages.

Overview
http://www.r-pkg.org/badges/version/ggfortify https://cranlogs.r-pkg.org/badges/grand-total/ggfortify https://coveralls.io/repos/sinhrks/ggfortify/badge.svg?branch=master&service=github

ggfortify

This package offers fortify and autoplot functions to allow automatic ggplot2 to visualize statistical result of popular R packages. Check out our R Journal paper for more details on the overall architecture design and a gallery of visualizations created with this package. Also check out autoplotly package that could automatically generate interactive visualizations with plotly.js style based on ggfortify. The generated visualizations can also be easily extended using ggplot2 syntax while staying interactive.

Note: For functions which returns list, ggfortify tries to infer a background class using its attribute names. Such functions are marked as "(inference)".

Installation

  • Install the latest stable release from CRAN:

    install.packages('ggfortify')
    
  • Install the development version from Github:

    if (!require("remotes")) install.packages("remotes")
    remotes::install_github('sinhrks/ggfortify')
    

Examples

Reference/Citation

To cite ggfortify in publications, please use (available via citation("ggfortify")):

Yuan Tang, Masaaki Horikoshi, and Wenxuan Li (2016). ggfortify: Unified Interface to Visualize Statistical Result of Popular R Packages. The R Journal, 8.2, 478-489.

Masaaki Horikoshi and Yuan Tang (2016). ggfortify: Data Visualization Tools for Statistical Analysis Results. https://CRAN.R-project.org/package=ggfortify

Coverage

This covers following classes:

  • base::matrix
  • base::table (supports fortify only)
  • cluster::clara
  • cluster::fanny
  • cluster::pam
  • cluster::silhouette
  • changepoint::cpt
  • dlm::dlmFilter
  • dlm::dlmSmooth (inference)
  • fGarch::fGARCH
  • forecast::bats
  • forecast::forecast
  • forecast::ets
  • forecast::nnetar
  • fracdiff::fracdiff
  • glmnet::cv.glmnet
  • glmnet::glmnet
  • KFAS::KFS
  • KFAS::signal (inference)
  • lfda::lfda
  • lfda::klfda
  • lfda::self
  • maps::map
  • MASS::isoMDS (inference)
  • MASS::sammon (inference)
  • raster::RasterBrick
  • raster::RasterCommon
  • raster::RasterLayer
  • raster::RasterStack
  • ROCR::performance
  • sp::Line
  • sp::Lines
  • sp::Polygon
  • sp::Polygons
  • sp::SpatialLines
  • sp::SpatialLinesDataFrame
  • sp::SpatialPoints
  • sp::SpatialPointsDataFrame
  • sp::SpatialPolygons
  • sp::SpatialPolygonsDataFrame
  • splines::basis
  • stats::acf
  • stats::ar
  • stats::Arima
  • stats::cmdscale (inference)
  • stats::decomposed.ts
  • stats::density
  • stats::factanal
  • stats::glm
  • stats::HoltWinters
  • stats::kmeans
  • stats::lm
  • stats::prcomp
  • stats::princomp
  • stats::spec
  • stats::stepfun
  • stats::stl
  • stats::ts
  • survival::survfit
  • survival::survfit.cox
  • survival::survfitms
  • strucchange::breakpoints
  • strucchange::breakpointsfull
  • timeSeries::timeSeries
  • tseries::irts
  • vars::varprd
  • xts::xts
  • zoo::zooreg

Helper Functions

  • ggdistribution to plot PDF/CDF
  • ggcpgram to plot cpgram
  • ggtsdiag to plot tsdiag
  • ggfreqplot to generalize monthplot

ggplot2 Families

There are some useful plotting packages using ggplot2. ggfortify will not focus on area already covered by these packages.

Comments
  • survfit error on plot

    survfit error on plot

    Hi,

    please, when i try to run

    library(ggfortify) library(survival) fit <- survfit(Surv(time, status) ~ sex, data = lung) autoplot(fit)

    gave this error: Error: GeomConfint was built with an incompatible version of ggproto. Please reinstall the package that provides this extension.

    thank you so much

    opened by fabioand2k 16
  • Keep order of factor levels.

    Keep order of factor levels.

    survfit from the survival package keeps the user's order of factor levels, but they become alphabetically reordered once ggfortify is used. This change keeps the levels in their original order.

    closes #142. closes #143.

    opened by DarioS 15
  • importing autoplot

    importing autoplot

    Dear Sir, I have written a function in which I used ggfortify to plot PCA the overall function is pretty simple, see below. When I do the package (https://github.com/kendomaniac/docker4seq) check I get the following warning: samplesPca: no visible global function definition for ‘autoplot’ Undefined global functions or variables: autoplot

    I import in my function ggfortify, but it seems that the autoplot in not imported from ggfortify Could please suggest a way of fix this problem? Cheers Raf

    ###################################### #' @title Sample PCA on raw counts #' @description This function runs PCA via ggfortify and ggplot2 on the output of samples2experiments function #' #' @param data, the counts dataframe generated by samples2experiemtn function #' @param groups, a character string indicating association between samples and experimental groups #' @param label, a boolean value, TRUE or FALSE to ad the names of the samples in the plot #' @param label.size, an integer value indicating the size of label #' @param output.file, a tab delimited file in which the first column are names from counts object from experiment.table.Rda file generated with samples2experiment function. In the pdf will be also rpesent the istigram fo the PCA variance dispersion. #' @import ggfortify #' @importFrom graphics plot #' @importFrom grDevices pdf #' @importFrom grDevices dev.off #' @importFrom stats prcomp #' @return a pdf file with the PCA plot #' @examples #'\dontrun{ #' load("test_experiment.tables.Rda") #' samplesPca(data=counts, groups=c("d","eb","wb","ws"), label = TRUE, label.size = 3) #' output.file="test.pdf", label = FALSE, label.size = 3) #' } #' @export samplesPca <- function(data, groups=NULL, output.file=NULL, label, label.size){ if(!is.null(groups)){ data.df <- data.frame(names(data), as.factor(groups)) names(data.df) <- c("samples","groups") if(is.null(output.file)){ autoplot.prcomp(prcomp(t(data)), data=data.df, colour='groups', label = label, label.size = label.size) }else{ pdf(output.file) plot(prcomp(t(data)), main="") autoplot(prcomp(t(data)), data=data.df, colour='groups', label = label, label.size = label.size) dev.off() cat(paste("\nPCA is saved in ",output.file, sep="")) } }else{ if(is.null(output.file)){ autoplot(prcomp(t(data))) }else{ pdf(output.file) plot(prcomp(t(data)), main="") autoplot(prcomp(t(data))) dev.off() cat(paste("\nPCA is saved in ",output.file, sep="")) } } return(data.df) }

    opened by kendomaniac 12
  • Remove label legend

    Remove label legend

    Hi,

    Thanks for the amazing package. Is there a way to remove label legend which is a small "a" in ggforty? Some parameters to work as geom_text(show.legend=F)? Thanks.

    Bests, oben

    opened by obenno 11
  • Package got removed from the CRAN

    Package got removed from the CRAN

    https://cran.r-project.org/web/packages/ggfortify/index.html shows:

    Package ‘ggfortify’ was removed from the CRAN repository.
    
    Formerly available versions can be obtained from the archive.
    
    Archived on 2016-05-02 as check problems were not corrected despite reminders.
    

    > install.packages fails as expected; however, the devtools install continues to work as expected after:

    library(devtools)
    install_github('sinhrks/ggfortify')
    
    opened by sciatro 10
  • ggsave doesn't recognize ggfortity objects

    ggsave doesn't recognize ggfortity objects

    plotdiag <-autoplot(result, which=c(1:3,5), mfrow=c(2,2)) plotdiag

    ***partdiage;

    ggsave(plot=plotdiag, file='cuckoo-R-diag.png', h=4, w=6, units="in", dpi=300)

    returns Error in UseMethod("grid.draw") : no applicable method for 'grid.draw' applied to an object of class "ggmultiplot"

    I think Wickham modifies ggsave() to accept gtable objects. Do you create such an object? Perhaps add that as a class as well?

    Using ggfortify 0.1.0 and ggplot 2.0.0

    opened by cschwarz-stat-sfu-ca 10
  • autoplot.lm() uses deprecated dplyr::arrange_()

    autoplot.lm() uses deprecated dplyr::arrange_()

    It appears that autoplot.lm() uses deprecated dplyr functions. This is a problem because dplyr is now throwing warnings about this and people using ggfortify on a daily basis for creating diagnostic plots shouldn't think that ggfortify has been abandoned.

    In particular lines 132, 136 and 140 in R/fortify_stats_lm.R use this to sort the fortified data by some column, but we can now just specify the arrangement without quoting desc(abs(.resid)). The same is true of the other instances.

    I cloned ggfortify and made the changes and it appears to work. However when I went to build the package, it seems that there are a bunch more dplyr deprecated function calls in the vignettes "basics.Rmd", "plot_pca.Rmd", "plot_surv.Rmd", "plot_ts.Rmd". I was reluctant to dive into all of those vignettes just to report this bug.

    I suspect that it wouldn't take to long to fix the vignettes, but I suspect this issue will touch a few other classes. I would be happy to start fixing those if it would be appreciated, but I didn't want to invest too much time before contacting people in charge.

    library(ggfortify) options(lifecycle_verbosity = 'error') # error out on deprecation warnings model <- lm( Volume ~ Height, data=trees) autoplot(model)

    The output (via reprex::reprex() ) is as follows:

    library(ggfortify) #> Loading required package: ggplot2 options(lifecycle_verbosity = 'error') model <- lm( Volume ~ Height, data=trees) autoplot(model) #> Error: arrange_() was deprecated in lifecycle 0.7.0 and is now defunct. #> Please use arrange() instead. #> See vignette('programming') for more help

    opened by dereksonderegger 8
  • Added support of survfitms

    Added support of survfitms

    Greetings.

    This PR adds support of survfitms - multistate survival models built using Surv(..., type = 'mstate').

    Here is an overview of changes to fortify:

    • To preserve original variable name depending on model class (surv or pstate) it is added to the end of the output data.frame;
    • Where necessary checks for variable name are made;
    • If surv.connect = T new rows are repeated according to number of states;
    • In the end columns are sorted to be in usual order.

    To autoplot:

    • Added facet_grid support - argument grid. It's more convenient for 2-dim set plots;
    • Added support to swap order of faceting (if facets) or column with rows (if grid). By default event first, then strata. Argument strip_swap;
    • An argument conf.int.group = NULL was added to plot_confint. It's required to split CI ribbons by both - event and strata, because it can't be done with fill only;
    • such artificial column group is assembled in autoplot.

    Feedback or questions are welcome.

    opened by yoursdearboy 7
  • Support survfit objects with multiple stratification variables. Fixes #214

    Support survfit objects with multiple stratification variables. Fixes #214

    In fortify_surv.R, changed the regular expression on line 49 to be "[^,]=" instead of ".=". This is in response to Issue #214, and the change is meant to allow autoplot to work on survfit objects with multiple stratification variables.

    testResults.txt

    opened by KellenBrosnahan 6
  • First try attempt at fortify & autoplot for spline basis objects

    First try attempt at fortify & autoplot for spline basis objects

    My attempt at #128. Unfortunately, I can't test it myself because the package fails devtools::check() with a nonsensical error message. It says:

    Installation failed.
    See ‘/private/var/folders/d3/ff25_6td3sjc02d8rfbwvmxc0000gn/T/RtmpxbR4VD/ggfortify.Rcheck/00install.out’ for details.
    

    And when I check the suggested log file, I see:

    * installing *source* package ‘ggfortify’ ...
    ** R
    ** inst
    ** preparing package for lazy loading
    ** help
    *** installing help indices
    ** building package indices
    ** installing vignettes
    ** testing if installed package can be loaded
    Warning: namespace ‘IRanges’ is not available and has been replaced
    by .GlobalEnv when processing object ‘’
    Warning in library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :
      there is no package called ‘IRanges’
    Error in .requirePackage(package) :
      unable to find required package ‘IRanges’
    Error: loading failed
    Execution halted
    ERROR: loading failed
    * removing ‘/private/var/folders/d3/ff25_6td3sjc02d8rfbwvmxc0000gn/T/RtmpxbR4VD/ggfortify.Rcheck/ggfortify’
    

    I can't figure out why it's trying to load IRanges, and I can't figure out why it would be unable to do so, since I have it installed.

    opened by DarwinAwardWinner 6
  • COMPAT: Support ggplot2 1.1.0

    COMPAT: Support ggplot2 1.1.0

    Closes #67. Some changes are required for ggplot2 1.1.0 (dev). This works on my local with ggplot2 dev, still needs followings.

    • [x] Add a logic to distinguish ggplot2 version (and use previous logic for earlier vers...).
    • [x] Allow Travis to test 1.1.0 and earlier ggplot2 versions
    • ~~R CMD check with R dev version~~ Using R dev version is little hard to maintain dep packages. Skipped.
    opened by sinhrks 6
  • "colour" vs "color" when calling autoplot on a PCA

    When I follow your sample code to plot a PCA object, but call color= instead of colour=, the argument is ignored.

    library(ggfortify)
    library(cowplot)
    pca_res <- prcomp(iris[1:4], scale = TRUE)
    british <- autoplot(pca_res, data=iris, colour='Species')
    us <- autoplot(pca_res, data=iris, color='Species')
    plot_grid(british, us,  labels=c('colour', 'color'))
    
    

    I think this is the only time I've ever encountered a difference between the American and British spellings.

    > sessionInfo()
    R version 4.1.2 (2021-11-01)
    Platform: x86_64-w64-mingw32/x64 (64-bit)
    Running under: Windows 10 x64 (build 19044)
    
    Matrix products: default
    
    locale:
    [1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
    [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
    [5] LC_TIME=English_United States.1252    
    
    attached base packages:
    [1] stats     graphics  grDevices utils     datasets  methods   base     
    
    other attached packages:
    [1] cowplot_1.1.1    ggfortify_0.4.14 ggplot2_3.3.5  
    

    image3

    opened by lcmurtaugh 1
  • no x/y labels with ggdistribution plots

    no x/y labels with ggdistribution plots

    Hello,

    with the ggdistribution() function x and y labels do not appear.

    Code to reproduce the issue:

    p <- ggfortify::ggdistribution(dnorm, seq(-3, 3, 0.1), mean = 0, sd = 1)
    
    p <- p +
        labs(
        title = "Title",
        subtitle = "subtitle",
        caption = "name of the author copyright",
        tag = "Plot A",
        x = "x label",
        y = "y label"
    ) 
    p
    

    Plot produced by the code:

    xlab-problem


    System information

    • OS Platform and Distribution:macOS Monterey 12.1
    • ggfortify installed from (e.g. CRAN or Github): CRAN
    • ggfortify version: 0.4.14
    • ggplot2: 3.3.5
    • R: 4.1.2 (2021-11-01)

    Thanks, Peter

    opened by petzi53 12
  • Loadings cutoff option (PCA)

    Loadings cutoff option (PCA)

    Hello!

    First, thank you for developing the package, it has been very useful.

    I actually open an issue to request (if possible) a new feature at plotting the factor loadings in a PCA. There are already nice aesthetic options for the loadings. However, I would be interested on setting a loadings.cutoff option to select the desired ones. When working with PCAs based on many variables (50 in my case) it can become very messy even when playing with sizes and all. Furthermore, there are some factors that I may not be interested on, because they don't explain any variance in the samples, so it's also a nice feature to filter-out some factors.

    Thank you in advance.

    Regards, Cristian

    contributions welcome 
    opened by cdiazmun 5
  • how to change the confidence level

    how to change the confidence level

    I am trying to change the confidence level but conf.int.value gets ignored. Is there a workaround? No matter what level I put, it gets ignored.

    library(forecast) library(ggfortify) d.arima <- auto.arima(AirPassengers) autoplot(d.arima, predict=predict(d.arima,n.ahead=3),ts.colour = 'blue', predict.colour = 'blue', predict.linetype = 'dashed', conf.int = TRUE,conf.int.value=0.99)

    opened by FelipeCarrillo 7
  • Feature request: a method for the objects obtained from lmer, glmer of  package lme4

    Feature request: a method for the objects obtained from lmer, glmer of package lme4

    I have tried autoplot with ggfortify loaded for a model output of the function lmer of the package lme4. I have tried also ggfortify:::autoplot.lm applied to the model and did not work any of both options. It would be great if you could add a method for the class merMod (lmerMod, glmerMod).

    Thanks!

    enhancement contributions welcome 
    opened by iago-pssjd 0
Releases(v0.4.15)
Owner
Sinhrks
Sinhrks
The visual framework is designed on the idea of module and implemented by mixin method

Visual Framework The visual framework is designed on the idea of module and implemented by mixin method. Its biggest feature is the mixins module whic

LEFTeyes 9 Sep 19, 2022
Bokeh Plotting Backend for Pandas and GeoPandas

Pandas-Bokeh provides a Bokeh plotting backend for Pandas, GeoPandas and Pyspark DataFrames, similar to the already existing Visualization feature of

Patrik Hlobil 822 Jan 07, 2023
OpenStats is a library built on top of streamlit that extracts data from the Github API and shows the main KPIs

Open Stats Discover and share the KPIs of your OpenSource project. OpenStats is a library built on top of streamlit that extracts data from the Github

Pere Miquel Brull 4 Apr 03, 2022
Advanced hot reloading for Python

The missing element of Python - Advanced Hot Reloading Details Reloadium adds hot reloading also called "edit and continue" functionality to any Pytho

Reloadware 1.9k Jan 04, 2023
Simple function to plot multiple barplots in the same figure.

Simple function to plot multiple barplots in the same figure. Supports padding and custom color.

Matthias Jakobs 2 Feb 21, 2022
Visualize large time-series data in plotly

plotly_resampler enables visualizing large sequential data by adding resampling functionality to Plotly figures. In this Plotly-Resampler demo over 11

PreDiCT.IDLab 604 Dec 28, 2022
DataVisualization - The evolution of my arduino and python journey. New level of competence achieved

DataVisualization - The evolution of my arduino and python journey. New level of competence achieved

1 Jan 03, 2022
Visualization of numerical optimization algorithms

Visualization of numerical optimization algorithms

Zhengxia Zou 46 Dec 01, 2022
This project is an Algorithm Visualizer where a user can visualize algorithms like Bubble Sort, Merge Sort, Quick Sort, Selection Sort, Linear Search and Binary Search.

Algo_Visualizer This project is an Algorithm Visualizer where a user can visualize common algorithms like "Bubble Sort", "Merge Sort", "Quick Sort", "

Rahul 4 Feb 07, 2022
A Python Binder that merge 2 files with any extension by creating a new python file and compiling it to exe which runs both payloads.

Update ! ANONFILE MIGHT NOT WORK ! About A Python Binder that merge 2 files with any extension by creating a new python file and compiling it to exe w

Vesper 15 Oct 12, 2022
Simple CLI python app to show a stocks graph performance. Made with Matplotlib and Tiingo.

stock-graph-python Simple CLI python app to show a stocks graph performance. Made with Matplotlib and Tiingo. Tiingo API Key You will need to add your

Toby 3 May 14, 2022
Collection of data visualizing projects through Tableau, Data Wrapper, and Power BI

Data-Visualization-Projects Collection of data visualizing projects through Tableau, Data Wrapper, and Power BI Indigenous-Brands-Social-Movements Pyt

Jinwoo(Roy) Yoon 1 Feb 05, 2022
A pandas extension that solves all problems of Jalai/Iraninan/Shamsi dates

Jalali Pandas Extentsion A pandas extension that solves all problems of Jalai/Iraninan/Shamsi dates Features Series Extenstion Convert string to Jalal

51 Jan 02, 2023
Lime: Explaining the predictions of any machine learning classifier

lime This project is about explaining what machine learning classifiers (or models) are doing. At the moment, we support explaining individual predict

Marco Tulio Correia Ribeiro 10.3k Dec 29, 2022
Python Data Structures for Humans™.

Schematics Python Data Structures for Humans™. About Project documentation: https://schematics.readthedocs.io/en/latest/ Schematics is a Python librar

Schematics 2.5k Dec 28, 2022
A tool to plot and execute Rossmos's Formula, that helps to catch serial criminals using mathematics

Rossmo Plotter A tool to plot and execute Rossmos's Formula using python, that helps to catch serial criminals using mathematics Author: Amlan Saha Ku

Amlan Saha Kundu 3 Aug 29, 2022
With Holoviews, your data visualizes itself.

HoloViews Stop plotting your data - annotate your data and let it visualize itself. HoloViews is an open-source Python library designed to make data a

HoloViz 2.3k Jan 02, 2023
Plotly Dash Command Line Tools - Easily create and deploy Plotly Dash projects from templates

🛠️ dash-tools - Create and Deploy Plotly Dash Apps from Command Line | | | | | Create a templated multi-page Plotly Dash app with CLI in less than 7

Andrew Hossack 50 Dec 30, 2022
Splore - a simple graphical interface for scrolling through and exploring data sets of molecules

Scroll through and exPLORE molecule sets The splore framework aims to offer a si

3 Jun 18, 2022
Debugging, monitoring and visualization for Python Machine Learning and Data Science

Welcome to TensorWatch TensorWatch is a debugging and visualization tool designed for data science, deep learning and reinforcement learning from Micr

Microsoft 3.3k Dec 27, 2022