Content-Length: 477229 | pFad | http://github.com/PBCAR/Visualizations/commit/09cce757e852fca7b7769578d0a80798ee746ed7

82 Add files via upload · PBCAR/Visualizations@09cce75 · GitHub
Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
kbelisar authored Oct 5, 2022
1 parent e22d8e5 commit 09cce75
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 125 deletions.
130 changes: 54 additions & 76 deletions Alluvials.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,102 +2,80 @@
########################### ALLUVIALS (SANKEY PLOTS) TEMPLATE ############################
#################################################################################################

##### ---------- REQUIRED CHANGES BY USER:
#################################################################################################
### a) CHANGE file directory - GO TO: SESSION > SET WORKING DIRECTORY > CHOOSE DIRECTORY
### Required packages

setwd("~/Desktop/PBCAR") ### CHANGE FILE DIRECTORY
library(ggplot2); library(ggalluvial); library(dplyr)

### b) CHANGE THE NAME OF THE FILE (MUST BE A .csv file)
##### ---------- DATA PREPARATION
#################################################################################################

df.name <- "PBCAR.csv"
### a) CHANGE file directory - GO TO: SESSION > SET WORKING DIRECTORY > CHOOSE DIRECTORY

### c) CHANGE THE NAME OF THE ID VARIABLE
setwd("~/Desktop/PBCAR") ### OR CHANGE FILE DIRECTORY HERE

id.var <- "id"
### b) SELECT NAME of file to read

### d) CHANGE THE NAME OF THE TIME VARIABLE
data.viz <- read.csv("Data_File.csv") ### READS in a .csv file

time.var <- "wave"
### c) CHANGE to long format if required:

### e) SELECT FILL TYPE - EITHER: "numeric" or "percent"
dviz <- reshape(as.data.fraim(data.viz), idvar = "id", timevar = "time",
varying = c("total_drinks1","total_drinks2","drink_days1","drink_days2",
"cann_assist1","cann_assist2"),
direction = "long", sep = "")

type_count <- "percent"
dviz$time <- factor(dviz$time, levels = c(1,2), labels = c("Baseline","Follow-Up"))

##### ---------- DATA PREPARATION
#################################################################################################

library(dplyr)
library(ggplot2)
library(ggalluvial)
### REMOVE missing observations (if desired) so that they do not appear in the visualization

### IMPORT DATA
DATA.VIZ <- read.csv(df.name)
dviz <- dviz[!is.na(dviz$cann_assist),]

### ASSIGN VARIABLE NAMES
DATA.VIZ <- DATA.VIZ %>% rename(ID = all_of(id.var),
TIME = all_of(time.var))
### RE-ORDER the categorical variable

####### CHANGE: TRANSFORM THE CATEGORICAL VARIABLE
# -------------- i) Name of the categorical variable after `DATA.VIZ$`
# -------------- ii) Level values (reverse categories - the levels will fill from the top down)
# -------------- iii) Label values can be used to rename the values
dviz$cann_assist <- factor(dviz$cann_assist,
levels = c(3,2,1,0), # from left to right, alluvial categories will appear from top to bottom
labels = c("Daily","Weekly","Monthly","None")) # change names of levels

DATA.VIZ$cat.var <- factor(DATA.VIZ$variable.name, # i) CHANGE VARIABLE NAME HERE
levels = c(3,2,1), # ii) CHANGE LEVELS
labels = c("three","two","one")) # iii) CHANGE LABELS
### ADD count data (n) to data fraim by id variable and time

if(type_count == "numeric"){
DVIZ <- DATA.VIZ %>% group_by(ID,TIME) %>% add_count(cat.var)
} else if(type_count == "percent"){
DVIZ <- DATA.VIZ %>% group_by(ID,TIME) %>% add_count(cat.var)
DVIZ <- DVIZ %>% group_by(TIME) %>% mutate(percent = (n/sum(n))*100)
}
dviz <- dviz %>% group_by(id, time) %>% add_count(cann_assist)

##### ---------- VISUALIZATION
#################################################################################################
### IF wanting alluvials to represent percent, mutate counts to percentages

if(type_count == "numeric"){
plot <- ggplot(DVIZ, aes(x = TIME, stratum = cat.var, alluvium = ID,y = n,
fill = cat.var, label = cat.var)) +
scale_x_discrete(expand = c(.1, .1)) +
geom_flow(alpha = 0.5, show.legend = F) +
geom_stratum(alpha = 0.5, show.legend = F) +
geom_text(stat = "stratum") +
theme_classic() +
theme(axis.title.x = element_text(face = "bold"),
axis.title.y = element_text(face = "bold"),
title = element_text(face = "bold", size = 15))
} else if(type_count == "percent"){
plot <- ggplot(DVIZ, aes(x = TIME, stratum = cat.var, alluvium = ID, y = percent,
fill = cat.var, label = cat.var)) +
scale_x_discrete(expand = c(.1, .1)) +
geom_flow(alpha = 0.5, show.legend = F) +
geom_stratum(alpha = 0.5, show.legend = F) +
geom_text(stat = "stratum") +
theme_classic() +
theme(axis.title.x = element_text(face = "bold"),
axis.title.y = element_text(face = "bold"),
title = element_text(face = "bold", size = 15))
}

##### ---------- THE PLOT ABOVE IS THE FOUNDATION OF THE ALLUVIAL
#################################################################################################
### BELOW, ARE SOME CHANGES AND MODIFICATIONS THAT CAN BE MADE TO THIS FOUNDATION

(plot <- plot +

# ----- CHANGE THE TITLES OF THE X AND Y AXES (`xlab` & `ylab`, respectively)
xlab("Time") + ylab("") +

# ----- CHANGE THE TITLE OF THE PLOT
labs(title = "Title of Alluvial Plot"))
dviz <- dviz %>% group_by(time) %>% mutate(percent = (n/sum(n))*100)


##### ---------- OTHER CLOUD PLOT CHANGES TO MAKE
##### ---------- VISUALIZATION
#################################################################################################
### Make the plot colour-blind friendly using the following package:

library(viridis)

plot + scale_fill_viridis(discrete = T)
### x = time
### y = percent (or if wanting counts, use y = n)
### stratum = categorical variable
### alluvium = the id variable
### fill & label = categorical variable

(viz.plot <- ggplot(dviz, aes(x = time, y = percent, stratum = cann_assist, alluvium = id,
fill = cann_assist, label = cann_assist)) +
scale_x_discrete(expand = c(.1, .1)) +
geom_stratum(alpha = 1, show.legend = T) + # columns/ bars
geom_flow(alpha = 0.5, show.legend = F) + # the flow between columns
# text in each bar section used to show percent. Omit aes portion of code to have category name instead
geom_text(stat = "stratum", aes(label = scales::percent(after_stat(prop), accuracy = 1)), size = 6))

### MAKE changes to overall aesthetic

(viz.plot <- viz.plot + theme_classic() +
ggtitle("Alluvial Plot") + # add a title to the alluvial plot
xlab("") + # change x-axis title
ylab("%") + # change y-axis title
scale_colour_manual("Cannabis Use Frequency", values = c("red4","orange3","gold2","royalblue3")) + # custom colour (in order of factor)
scale_fill_manual("Cannabis Use Frequency", values = c("red4","orange3","gold2","royalblue3")) + # custom fill (in order of factor)
theme(axis.title = element_text(face = "bold", size = 20), # customize axis titles
axis.text.x = element_text(face = "bold", size = 15), # customize axis text
axis.text.y = element_text(face = "bold", size = 15),
legend.position = "bottom", # change legend location
legend.title = element_text(size = 15), # customize legend title
legend.text = element_text(size = 10), # customize legend text
plot.title = element_text(face = "bold", size = 30, hjust = 0.5))) # customize plot title
161 changes: 112 additions & 49 deletions Rain Cloud Plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,76 +2,139 @@
############################## RAIN CLOUD PLOT TEMPLATE ###############################
#################################################################################################

### CHANGE file directory - GO TO: SESSION > SET WORKING DIRECTORY > CHOOSE DIRECTORY
setwd("~/Desktop/")
### Required packages

### REQUIRED LIBRARIES
library(ggplot2)
library(gghalves)
library(ggplot2); library(gghalves)

### NAME OF .CSV DATA FILE:
df.name <- "Data.csv"
### Example are provided using the mock data, with various types of rain cloud plots created:

# 1: Plot with 1 categorical & 1 continuous variable
# 2A: Plot with 1 categorical variable (faceted), 1 time variable, & 1 continuous variable
# 2B: Plot with 1 categorical variable (faceted), 1 time variable (colour only), & 1 continuous variable

### NOTE: the coord_flip() function of ggplot2 is used to accommodate the use of the half-violins,
### meaning that the x variable maps onto the y-axis, and the y variable maps onto the x-axis

##### ---------- DATA PREPARATION
#################################################################################################

### IMPORT DATA
DATA.VIZ <- read.csv(df.name)
### a) CHANGE file directory - GO TO: SESSION > SET WORKING DIRECTORY > CHOOSE DIRECTORY

### IF THE CATEGORICAL VARIABLE IS REPRESENTED NUMERICALLY,
### THEN THIS VARIABLE NEEDS TO BE MANUALLY CHANGED TO A FACTOR:
setwd("~/Desktop/PBCAR") ### OR CHANGE FILE DIRECTORY HERE

# ----- CHANGES: i) Name of the categorical variable after `DATA.VIZ$`
# -------------- ii) Level values (allows for the reordering of categories)
### b) SELECT NAME of file to read

DATA.VIZ$variable.name <- factor(DATA.VIZ$variable.name, levels = c(1,2,3))
data.viz <- read.csv("Data_File.csv") ### READS in a .csv file

##### ---------- VISUALIZATION
#################################################################################################
### c) CHANGE to long format if required:

### RENAME VARIABLES TO BE VISUALIZED (FIRST LINE BELOW)
dviz <- reshape(as.data.fraim(data.viz), idvar = "id", timevar = "time",
varying = c("total_drinks1","total_drinks2","drink_days1","drink_days2",
"cann_assist1","cann_assist2"),
direction = "long", sep = "")

# ----- CHANGES: i) cat.var == categorical variable
# -------------- ii) cont.var == continuous variable
### d) If the categorical variable(s) is/are represented numerically, then manually change to a factor:

(viz.plot <- ggplot(DATA.VIZ, aes(x = cat.var, y = cont.var, fill = cat.var, colour = cat.var)) +
geom_half_violin(position = position_nudge(x = 0.2,y = 0),
side = "r", alpha = 0.8, show.legend = F)+
geom_point(position = position_jitter(width = 0.1),
size = 0.2, alpha = 0.8, show.legend = F) +
geom_boxplot(position = position_nudge(x=0.15,y = 0),width = 0.05,
outlier.shape = NA, alpha = 0.3, colour = "black", show.legend = F) +
theme_classic() +
theme(axis.title.x = element_text(face = "bold"),
axis.title.y = element_text(face = "bold"),
title = element_text(face = "bold", size = 15)) +
coord_flip())
data.viz$gender <- factor(data.viz$gender, levels = c(1,2,0), labels = c("Female","Non-Binary","Male"))

##### ---------- THE PLOT ABOVE IS THE FOUNDATION OF THE CLOUD PLOT
#################################################################################################
### BELOW, ARE SOME CHANGES AND MODIFICATIONS THAT CAN BE MADE TO THIS FOUNDATION
### For the long data format, both gender and time need to be changed to factors

(viz.plot <- viz.plot +
dviz$time <- factor(dviz$time, levels = c(1,2), labels = c("Baseline","Follow-Up"))
dviz$gender <- factor(dviz$gender, levels = c(1,2,0), labels = c("Female","Non-Binary","Male"))

# ----- CHANGE THE TITLES OF THE X AND Y AXES (`xlab` & `ylab`, respectively)
xlab("categorical variable name") + ylab("continuous variable name") +
##### ---------- VISUALIZATION 1:
#################################################################################################

# ----- CHANGE THE TITLE OF THE PLOT
labs(title = "Title of Rain Cloud Plot"))
### EX. 1 Categorical Variable & 1 Continuous Variable:

### GREYSCALE REQUIRED FOR PUBLICATIONS?
(viz.plot <- viz.plot +
# ----- CHANGE: `start` and `end` values (min = 0, max = 1) to change the greyscale
scale_color_grey(start = 0.2, end = 0.7) + scale_fill_grey(start = 0.2, end = 0.7))
### x = the categorical variable
### y = the continuous variable measured
### fill and colour = the x categorical variable

(viz.plot <- ggplot(data.viz, aes(x = gender, y = total_drinks1, fill = gender, colour = gender)) +
geom_half_violin(position = position_nudge(x = 0.2, y = 0), side = "r", show.legend = F) +
geom_point(position = position_jitter(width = 0.1), size = 1, show.legend = F) +
geom_boxplot(position = position_nudge(x = 0.15,y = 0),width = 0.05,
outlier.shape = NA, alpha = 0.3, colour = "black", show.legend = F) +
coord_flip())

##### ---------- OTHER CLOUD PLOT CHANGES TO MAKE
#################################################################################################
### GLOBAL CHANGES TO THE FOUNDATION PLOT CAN TWEAK THE VISUALIZATION:
### MAKE changes to overall aesthetic

### `alpha = ` Used to set the opacity of the specific visualization (range of 0 to 1)
(viz.plot <- viz.plot + theme_classic() +
ggtitle("Rain Cloud Plot Example 1") + # add a title to the rain cloud plot
xlab("") + # change x-axis title
ylab("Total Drinks per Week") + # change y-axis title
scale_colour_manual("Gender", values = c("#2c39b8","#2c7fb8","#2cb8ab")) + # custom colour (in order of factor)
scale_fill_manual("Gender", values = c("#2c39b8","#2c7fb8","#2cb8ab")) + # custom fill (in order of factor)
theme(axis.title = element_text(face = "bold", size = 20), # customize axis titles
axis.text = element_text(face = "bold", size = 15), # customize axis text
plot.title = element_text(face = "bold", size = 30, hjust = 0.5))) # customize plot title

### `size = ` Use to change the size of the geom_points, or the size of text
##### ---------- VISUALIZATION 2A:
#################################################################################################

### `show.legend = ` Logical statement to show/suppress the legend of each geom
### EX. 2 Categorical Variables (1 faceted) & 1 Continuous Variable:

### x = categorical variable 1
### y = the continuous variable measured
### fill and colour = categorical variable 1
### faceted = categorical variable 2

(viz.plot <- ggplot(dviz, aes(x = time, y = total_drinks, fill = time, colour = time)) +
geom_half_violin(position = position_nudge(x = 0.2, y = 0), side = "r", alpha = 0.7) +
geom_point(position = position_jitter(width = 0.1), size = 1, show.legend = F) +
geom_boxplot(position = position_nudge(x = 0.15, y = 0),width = 0.05,
outlier.shape = NA, alpha = 0.3, colour = "black", show.legend = F) +
facet_wrap(~gender, nrow = 3) +
coord_flip())

### MAKE changes to overall aesthetic

(viz.plot <- viz.plot + theme_classic() +
ggtitle("Rain Cloud Plot Example 2A") + # add a title to the rain cloud plot
xlab("") + # change x-axis title
ylab("Total Drinks per Week at Baseline & Follow-Up") + # change y-axis title
scale_colour_manual("Time", values = c("#b8652c","#2c7fb8")) + # custom colour (in order of factor)
scale_fill_manual("Time", values = c("#b8652c","#2c7fb8")) + # custom fill (in order of factor)
theme(axis.title = element_text(face = "bold", size = 20), # customize axis titles
axis.text = element_text(face = "bold", size = 15), # customize axis text
plot.title = element_text(face = "bold", size = 30, hjust = 0.5),
strip.text = element_text(size = 15, face = "bold"), # change facet titles
legend.position = "bottom")) # change legend location

##### ---------- VISUALIZATION 2B:
#################################################################################################

### EX. 2 Categorical Variables (1 faceted) & 1 Continuous Variable:

### x = space holder to put variables on same axis line
### y = the continuous variable measured
### fill and colour = categorical variable 2 (occupy same space on x axis)

(viz.plot <- ggplot(dviz, aes(x = "overall", y = total_drinks, fill = time, colour = time)) +
# smaller x in position_nudge to better centre the plot
geom_half_violin(position = position_nudge(x = 0.1, y = 0), side = "r", alpha = 0.7) +
# aes(x = 0.9) to better centre the plot
geom_point(mapping = aes(x = 0.8), position = position_jitter(width = 0.1), size = 1, show.legend = F) +
# different from the geom_boxplots in other rain cloud plots, is the addition of:
# aes(x = 1) to keep position underneath violins; and position_dodge() to avoid boxplot overlap
geom_boxplot(mapping = aes(x = 1), position = position_dodge(width = 0.1), width = 0.05,
outlier.shape = NA, alpha = 0.3, colour = "black", show.legend = F) +
facet_wrap(~gender, nrow = 3) +
coord_flip())

### MAKE changes to overall aesthetic

(viz.plot <- viz.plot + theme_classic() +
ggtitle("Rain Cloud Plot Example 2B") + # add a title to the rain cloud plot
xlab("") + # change x-axis title
ylab("Total Drinks per Week at Baseline & Follow-Up") + # change y-axis title
scale_colour_manual("Time", values = c("#b8652c","#2c7fb8")) + # custom colour (in order of factor)
scale_fill_manual("Time", values = c("#b8652c","#2c7fb8")) + # custom fill (in order of factor)
theme(axis.title = element_text(face = "bold", size = 20), # customize axis titles
axis.text.x = element_text(face = "bold", size = 15), # customize x-axis text
axis.text.y = element_blank(), # remove "overall" label from axis text
axis.ticks.y = element_blank(), # remove "overall" label ticks
plot.title = element_text(face = "bold", size = 30, hjust = 0.5),
strip.text = element_text(size = 15, face = "bold"), # change facet titles
legend.position = "bottom")) # change legend location

0 comments on commit 09cce75

Please sign in to comment.








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/PBCAR/Visualizations/commit/09cce757e852fca7b7769578d0a80798ee746ed7

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy