Skip to content

Commit

Permalink
version 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanInglis authored and cran-robot committed Jun 30, 2023
0 parents commit ab311f7
Show file tree
Hide file tree
Showing 40 changed files with 10,760 additions and 0 deletions.
18 changes: 18 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Package: corrViz
Type: Package
Title: Visualise Correlations
Version: 0.1.0
Author: Alan Inglis
Maintainer: Alan Inglis <alan.n.inglis@gmail.com>
Description: An investigative tool designed to help users visualize correlations between variables in their datasets. This package aims to provide an easy and effective way to explore and visualize these correlations, making it easier to interpret and communicate results.
License: GPL (>= 2)
Encoding: UTF-8
Imports: ggplot2, stats, grDevices, visNetwork, plotly, DendSer,
gganimate, igraph, ggraph, circlize, GGally, purrr, shiny
RoxygenNote: 7.2.3
Suggests: knitr, rmarkdown, scales, gifski
VignetteBuilder: knitr
NeedsCompilation: no
Packaged: 2023-06-29 15:47:40 UTC; alaninglis
Repository: CRAN
Date/Publication: 2023-06-30 11:40:07 UTC
39 changes: 39 additions & 0 deletions MD5
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
bab24dff6d81ef7cbee61b2b31942774 *DESCRIPTION
bae140e33dfdba1b82cfaf674332705d *NAMESPACE
795cc78bde4a39fddc941d5206752171 *R/animSolar.R
d30ac5104d19994e8c9ec5dbe0358489 *R/corrBarplot.R
79e163633f55b21e6ebe8aacf1ccaefd *R/corrBubble.R
a9e74694dd5220dada05da64ce074ad3 *R/corrChord.R
107c554f014794777bddd83c69eed964 *R/corrCircle.R
038cf8b4d8f0f9246fec4788d3b08474 *R/corrGrid.R
5b42f2e6c09b1376776ce31d3d3491a2 *R/corrHeatmap.R
c4bafdd485c3f372551b60ece8d4399d *R/corrNetwork.R
6ebd2a67134b28fc6632ce24e6daf4ef *R/corrPairs.R
b78ece3f0194d8204bf223912c9cbb09 *R/corrSankey.R
0523eac9ddea0f6b2f9aca01700018c2 *R/corrShiny.R
9fb3f592f67879a268e6032c7aa413a3 *R/corrSolar.R
206a4b0c222a57f203b5b7d73e2df90d *R/matrix2long.R
64b753eba49192f9f525363ddb151271 *README.md
8aa0349e09e9cd9daf03f2431e883d57 *build/vignette.rds
29ca88effccbfe979bf4f3adf99eb2e3 *inst/doc/corrViz.R
e729fdb7773ef2c756e9d7508d5593c5 *inst/doc/corrViz.Rmd
6b4eec60a1ec0d54450bfeda5e471ae3 *inst/doc/corrViz.html
5afbee77bdce7f5870043d005552b1ba *man/animSolar.Rd
9912dee19f1dac10f87a24ff2594646d *man/corrBarplot.Rd
71960c9427e2f91f72a8b2ea19857aa5 *man/corrBubble.Rd
5d4bbcdc50a1c35f1f284269676c0b19 *man/corrChord.Rd
518d885739dfb6a3ba217f3345adb325 *man/corrCircle.Rd
8deb9c36711592750c2eb3156645ffbf *man/corrGrid.Rd
9849aaefc8b35f1a4e8a2ccf13cb945e *man/corrHeatmap.Rd
a677058b52c1c2de4fc2da1795c43e9e *man/corrNetwork.Rd
10c49ec87185c687dbdb3b655f4c25de *man/corrPairs.Rd
b249af21fe155bffe721c8061641308a *man/corrSankey.Rd
ad8def40b092d8223d222550ec5f7159 *man/corrShiny.Rd
e062ea9e401a7c9510b6015401e02da1 *man/corrSolar.Rd
52f9cce159f1facba482d84cfe921122 *man/figures/README-chord-1.png
491dac80c3a9d6a169ae720d76b2b8c0 *man/figures/README-chord2-1.png
276183789e0c829040c3e117cda5dcdc *man/figures/README-circle-1.png
f5ca8df2fe792d5c94f382947ad40e88 *man/figures/README-circle2-1.png
1b37298d86c70ebd8909596cb615eaf2 *man/figures/README-solar-1.png
775b1a8574f6d4420a05f7f18c948502 *man/matrix2long.Rd
e729fdb7773ef2c756e9d7508d5593c5 *vignettes/corrViz.Rmd
57 changes: 57 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Generated by roxygen2: do not edit by hand

export(animSolar)
export(corrBarplot)
export(corrBubble)
export(corrChord)
export(corrCircle)
export(corrGrid)
export(corrHeatmap)
export(corrNetwork)
export(corrPairs)
export(corrSankey)
export(corrShiny)
export(corrSolar)
export(matrix2long)
import(ggplot2)
import(ggraph)
import(shiny)
importFrom(DendSer,dser)
importFrom(GGally,eval_data_col)
importFrom(GGally,ggpairs)
importFrom(GGally,wrap)
importFrom(circlize,chordDiagram)
importFrom(circlize,colorRamp2)
importFrom(gganimate,anim_save)
importFrom(gganimate,animate)
importFrom(gganimate,gifski_renderer)
importFrom(gganimate,transition_manual)
importFrom(ggplot2,aes_string)
importFrom(ggplot2,annotate)
importFrom(ggplot2,geom_point)
importFrom(ggplot2,geom_smooth)
importFrom(ggplot2,labs)
importFrom(ggplot2,scale_color_continuous)
importFrom(ggplot2,scale_color_discrete)
importFrom(ggplot2,scale_size_continuous)
importFrom(ggplot2,scale_size_discrete)
importFrom(grDevices,colorRampPalette)
importFrom(grDevices,rainbow)
importFrom(igraph,graph_from_data_frame)
importFrom(plotly,ggplotly)
importFrom(plotly,plot_ly)
importFrom(plotly,style)
importFrom(purrr,pmap)
importFrom(stats,as.dist)
importFrom(stats,cor)
importFrom(stats,cor.test)
importFrom(stats,na.omit)
importFrom(stats,reshape)
importFrom(visNetwork,visEdges)
importFrom(visNetwork,visIgraphLayout)
importFrom(visNetwork,visInteraction)
importFrom(visNetwork,visLegend)
importFrom(visNetwork,visNetwork)
importFrom(visNetwork,visNodes)
importFrom(visNetwork,visOptions)
importFrom(visNetwork,visPhysics)
203 changes: 203 additions & 0 deletions R/animSolar.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
#' animSolar
#'
#' This function creates an animated solar system plot of correlations between
#' variables in a dataset.
#'
#' @param mat A square correlation matrix to visualise.
#' @param sun A character string specifying the column name in the dataset to be treated
#' as the 'sun' in the solar system plot.
#' @param export A logical value specifying whether to export the animation as a GIF file, default is FALSE.
#' @param num_frames An integer specifying the number of frames in the animation, default is 100.
#' @param path A character string specifying the directory path where the GIF file will be saved, default is NULL.
#' @param gif_name A character string specifying the name of the GIF file. Must be in the format "myFile.gif"
#' @param fps An integer specifying the frames per second for the animation.
#' Default is 60 and is only used when exporting a gif via \code{export = TRUE}.
#'
#'
#' @details In a solar system correlation plot, the dependent variable of
#' interest is positioned at the center, represented as the sun.
#' The explanatory variables are depicted as planets orbiting
#' around the sun, with their distance from the sun corresponding
#' to the absolute value of their correlation with the dependent variable.
#' Therefore, the greater the distance of a planet from the sun,
#' the weaker the correlation between the explanatory variable
#' and the dependent variable.
#'
#' @details The \code{num_frames} argument is used to select the number of frames.
#' Setting this to a low value will produce the plot
#' quicker, however having a low number of frames will result in the "planets" jumping
#' as the frames transition. Additionally, a low values of \code{num_frames} will affect the
#' orbit of the animation when setting \code{export = FALSE}. This differs from the
#' \code{fps} argument which sets the number of frames to play per second for use
#' when exporting a gif.
#'
#' @return An animated solar system plot displaying correlations.
#'
#'
#' @importFrom gganimate transition_manual
#' @importFrom gganimate animate
#' @importFrom gganimate anim_save
#' @importFrom gganimate gifski_renderer
#' @importFrom stats cor
#' @importFrom stats na.omit
#' @import ggplot2
#'
#' @examples
#' cm <- cor(mtcars)
#'
#' animSolar(mat = cm,
#' sun = 'mpg',
#' export = FALSE,
#' num_frames = 25)
#'
#' @export
animSolar <- function(mat,
sun = NULL,
export = FALSE,
num_frames = 100,
path = NULL,
gif_name = "solar_system.gif",
fps = 60) {
if(export){
if (!requireNamespace("gifski", quietly = TRUE)) {
stop('ERROR: gifski package is required to render animation.
Please use: install.packages("gifski")')
}
}

# declare global vars
colr <- col_name <- NULL

# Calculate correlation matrix
cor_matrix <- mat #cor(data, method = method)
diag(cor_matrix) <- NA

# Convert matrix to data frame
df_data_long <- matrix2long(cor_matrix)

# Filter rows where col_name equals 'sun'
df_data_filtered <- subset(df_data_long, col_name == sun)

# Order rows by 'value' column in descending order
df_data_ordered <- df_data_filtered[order(df_data_filtered$value, decreasing = TRUE),]

# Rename columns
names(df_data_ordered)[names(df_data_ordered) == "value"] <- "r"
names(df_data_ordered)[names(df_data_ordered) == "col_name"] <- "x"
names(df_data_ordered)[names(df_data_ordered) == "row_name"] <- "y"

# Assign the result to correlations
correlations <- df_data_ordered

# check if any correaltions would round to 1 and change to 0.9
correlations$r <- ifelse(correlations$r > 0.9, 0.9, correlations$r)


# Assign orbit radius based on absolute, rounded correlation values
correlations$r <- round(correlations$r, 1)
# Calculate new columns 'orbit_radius' and 'angle'
correlations <- transform(correlations,
orbit_radius = 1 - round(abs(r), 1),
angle = 2 * pi * seq_len(nrow(correlations)) / nrow(correlations)
)


# add correlation colour
correlations$colr <- ifelse(correlations$r <= 0, "blue", "red")
correlations <- na.omit(correlations)

# create animated plot
nframes <- num_frames
seqFrames <- (ncol(mat) - 1)

# ang <- rep(seq(0, 2 * pi, length.out = seqFrames), 10)
ang <- rep(seq(0, 2 * pi, length.out = nframes), seqFrames)
ang <- ang + rep(correlations$angle, each = nframes)

y <- x <- r <- id <- orbit_radius <- angle <- frame <- NULL
solar_system <- data.frame(
y = rep(correlations$y, each = nframes),
x = rep(correlations$x, each = nframes),
r = rep(correlations$r, each = nframes),
id = rep(correlations$id, each = nframes),
orbit_radius = rep(correlations$orbit_radius, each = nframes),
angle = ang,
colr = rep(correlations$colr, each = nframes),
frame = rep(1:nframes, seqFrames)
)


# Function to generate points along the circumference of a circle
circle_points <- function(radius, n_points = 100) {
data.frame(
x = radius * cos(seq(0, 2 * pi, length.out = n_points)),
y = radius * sin(seq(0, 2 * pi, length.out = n_points)),
r = radius
)
}

# Define a function to apply circle_points and assign an 'id' to each element in the list
create_circle_points_df <- function(radius, id) {
df <- circle_points(radius)
df$id <- id
return(df)
}

# Use lapply to apply the create_circle_points_df function to each unique orbit_radius
circle_data_list <- mapply(create_circle_points_df,
unique(correlations$orbit_radius),
seq_along(unique(correlations$orbit_radius)),
SIMPLIFY = FALSE)

# Combine the resulting list of data frames into a single data frame using base R's 'do.call' and 'rbind'
circle_data <- do.call(rbind, circle_data_list)
circle_data$id <- as.character(circle_data$id)



# create df of circle names to display
circle_name <- data.frame(
nam = unique(round(abs(correlations$r), 1)),
nam2 = unique(round(abs(correlations$orbit_radius), 1))
)

suppressWarnings({
p <- ggplot() +
geom_path(data = circle_data, aes(x = x, y = y, group = id, color = id), linetype = "solid", alpha = 0.5) +
geom_point(data = solar_system, aes(
x = orbit_radius * cos(angle),
y = orbit_radius * sin(angle),
size = 3,
color = colr,
frame = frame,
label = r
), alpha = 0.8) +
geom_text(data = solar_system, aes(
x = orbit_radius * cos(angle),
y = orbit_radius * sin(angle),
label = y,
frame = frame
), hjust = -0.5, vjust = 0.5) +
geom_point(data = solar_system, aes(x = 0, y = 0), size = 6, color = "yellow") +
geom_text(data = solar_system, aes(x = 0, y = 0, label = sun), hjust = -0.5, vjust = -1) +
geom_text(
data = correlations, aes(x = 0, y = orbit_radius, label = round(abs(r), 1)),
size = 3.5, alpha = 0.3
) +
coord_fixed() +
theme_void() +
theme(legend.position = "none") +
transition_manual(frame)
})

if (export) {
animation <- animate(p,
nframes = 500, fps = fps, end_pause = 0,
width = 800, height = 800, units = "px",
renderer = gifski_renderer()
)
anim_save(gif_name, animation, path = path)
} else {
ggplotly(p, tooltip = c("label"))
}
}

0 comments on commit ab311f7

Please sign in to comment.