You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Thank you for developing the RainCloudPlot, which has been my favorite way of presenting data ever since I saw it in twitter.
I was trying to make a RainCloudPlot in a similar style of Figure 10 (Repeated Measures Factorial Rainclouds) but without success. Any help is greatly appreciated!
Below are the dataframe and code to reproduce my problem.
Yanxian
# Before start, run the RainCloudPlot source code to get the function for making geom_flat_violin### This script creates an R function to generate raincloud plots, then simulates ### data for plots. If using for your own data, you only need lines 1-80. ### It relies largely on code previously written by David Robinson ### (https://gist.github.com/dgrtwo/eb7750e74997891d7c20) and ggplot2 by H Wickham
library(tidyverse)
#> Warning: ³Ì¼°ü'tidyverse'ÊÇÓÃR°æ±¾3.4.4 À´½¨ÔìµÄ#> Warning: ³Ì¼°ü'ggplot2'ÊÇÓÃR°æ±¾3.4.4 À´½¨ÔìµÄ#> Warning: ³Ì¼°ü'tibble'ÊÇÓÃR°æ±¾3.4.4 À´½¨ÔìµÄ#> Warning: ³Ì¼°ü'tidyr'ÊÇÓÃR°æ±¾3.4.4 À´½¨ÔìµÄ#> Warning: ³Ì¼°ü'readr'ÊÇÓÃR°æ±¾3.4.4 À´½¨ÔìµÄ#> Warning: ³Ì¼°ü'purrr'ÊÇÓÃR°æ±¾3.4.4 À´½¨ÔìµÄ#> Warning: ³Ì¼°ü'dplyr'ÊÇÓÃR°æ±¾3.4.4 À´½¨ÔìµÄ#> Warning: ³Ì¼°ü'stringr'ÊÇÓÃR°æ±¾3.4.4 À´½¨ÔìµÄ#> Warning: ³Ì¼°ü'forcats'ÊÇÓÃR°æ±¾3.4.4 À´½¨ÔìµÄ# Defining the geom_flat_violin function. Note: the below code modifies the # existing github page by removing a parenthesis in line 50"%||%"<-function(a, b) {
if (!is.null(a)) aelseb
}
geom_flat_violin<-function(mapping=NULL, data=NULL, stat="ydensity",
position="dodge", trim=TRUE, scale="area",
show.legend=NA, inherit.aes=TRUE, ...) {
layer(
data=data,
mapping=mapping,
stat=stat,
geom=GeomFlatViolin,
position=position,
show.legend=show.legend,
inherit.aes=inherit.aes,
params=list(
trim=trim,
scale=scale,
...
)
)
}
GeomFlatViolin<-
ggproto("GeomFlatViolin", Geom,
setup_data=function(data, params) {
data$width<-data$width %||%
params$width %||% (resolution(data$x, FALSE) *0.9)
# ymin, ymax, xmin, and xmax define the bounding rectangle for each groupdata %>%
group_by(group) %>%
mutate(ymin= min(y),
ymax= max(y),
xmin=x,
xmax=x+width/2)
},
draw_group=function(data, panel_scales, coord) {
# Find the points for the line to go all the way arounddata<- transform(data, xminv=x,
xmaxv=x+violinwidth* (xmax-x))
# Make sure it's sorted properly to draw the outlinenewdata<- rbind(plyr::arrange(transform(data, x=xminv), y),
plyr::arrange(transform(data, x=xmaxv), -y))
# Close the polygon: set first and last point the same# Needed for coord_polar and suchnewdata<- rbind(newdata, newdata[1,])
ggplot2:::ggname("geom_flat_violin", GeomPolygon$draw_panel(newdata, panel_scales, coord))
},
draw_key=draw_key_polygon,
default_aes= aes(weight=1, colour="grey20", fill="white", size=0.5,
alpha=NA, linetype="solid"),
required_aes= c("x", "y")
)
# Create a dataframeDiet<- rep(rep(c("REF", "IM"), each=18), 3)
Gut_segment<- rep(c("PI", "MI", "DI"), each=36)
OS_index<- c(rnorm(36, mean=3, sd=1), rnorm(36, mean=0.2, sd=0.05), rnorm(36, mean=0.3, sd=0.08))
df<-data.frame(Diet, Gut_segment, OS_index)
head (df)
#> Diet Gut_segment OS_index#> 1 REF PI 2.657419#> 2 REF PI 3.355020#> 3 REF PI 1.895698#> 4 REF PI 2.569957#> 5 REF PI 3.105802#> 6 REF PI 1.941960# Define the desired order of "Diet" and "Gut_segment"df$Diet<-factor(df$Diet, levels= c("REF", "IM"))
df$Gut_segment<-factor(df$Gut_segment, levels= c("PI", "MI", "DI"))
# make raincloud plot #######################################################################################
library(cowplot)
#> Warning: ³Ì¼°ü'cowplot'ÊÇÓÃR°æ±¾3.4.4 À´½¨ÔìµÄ#> #> ÔØÈë³Ì¼°ü£º'cowplot'#> The following object is masked from 'package:ggplot2':#> #> ggsave# Make a function for setting the number of decimals shown on the graphfmt_dcimals<-function(decimals=0){
function(x) format(x, nsmall=decimals,scientific=FALSE)
}
# First try with facet_wrap #################################################################################
ggplot(df, aes(x=Gut_segment, y=OS_index, fill=Diet)) +
geom_flat_violin(aes(fill=Diet),
position= position_nudge(x=.1, y=0),
adjust=1.5,
trim=FALSE,
alpha=.5,
colour=NA) +
geom_point(aes(x= as.numeric(Gut_segment)-.15, y=OS_index, colour=Diet),
position= position_jitter(width=.05),
size=1,
shape=20) +
geom_boxplot(aes(x=Gut_segment, y=OS_index, fill=Diet),
outlier.shape=NA,
alpha=.5,
width=.1,
colour="black") +
scale_y_continuous(limits= c(0, NA), expand= c(0,0), labels= fmt_dcimals(1)) +
ylab('Organosomatic index (%)') +
facet_wrap(~Gut_segment, nrow=1, scales="free_y") +
scale_colour_brewer(palette="Dark2") +
scale_fill_brewer(palette="Dark2")
#> Warning: ³Ì¼°ü'bindrcpp'ÊÇÓÃR°æ±¾3.4.4 À´½¨ÔìµÄ#> Warning: Removed 17 rows containing missing values (geom_flat_violin).
### COMMENTS: ### Using "Gut_segment" as independent variable resulted in showing all the gut segments on x axis in each plot. ### If use "Diet" as independent variable, can't get the Figure 10 style. # Second try: split the dataframe, make raincloud plot one by one and combine them together ################# # Split the dataframe by "Gut_segment" df_spl<- split(df, f=df$Gut_segment)
# make a raincloud plot for each data frame plist<- lapply(
df_spl,
function(x)
{
ggplot(x, aes(x=Gut_segment, y=OS_index, fill=Diet)) +
geom_flat_violin(aes(fill=Diet),
position= position_nudge(x=.1, y=0),
adjust=1.5,
trim=FALSE,
alpha=.5,
colour=NA) +
geom_point(aes(x= as.numeric(Gut_segment)-.15, y=OS_index, colour=Diet),
position= position_jitter(width=.05),
size=1,
shape=20) +
geom_boxplot(aes(x=Gut_segment, y=OS_index, fill=Diet),
outlier.shape=NA,
alpha=.5,
width=.1,
colour="black") +
scale_y_continuous(limits= c(0, NA), expand= c(0,0), labels= fmt_dcimals(1)) +
ylab('Organosomatic index (%)') +
scale_colour_brewer(palette="Dark2") +
scale_fill_brewer(palette="Dark2")
}
)
plot_grid(plotlist=plist, ncol=3)
#> Warning: Removed 17 rows containing missing values (geom_flat_violin).
### COMMENTS: ### Somehow, "Gut_segment" was converted to differnet numbers for different dataframe in the list, resulting### in wrong alignments for the geom_point. Could not figure out a workaround.
Hi there,
Thank you for developing the RainCloudPlot, which has been my favorite way of presenting data ever since I saw it in twitter.
I was trying to make a RainCloudPlot in a similar style of Figure 10 (Repeated Measures Factorial Rainclouds) but without success. Any help is greatly appreciated!
Below are the dataframe and code to reproduce my problem.
Yanxian
@rdname ggplot2-ggproto
@Format NULL
@Usage NULL
@export
Created on 2018-10-20 by the reprex package (v0.2.1)
Session info
The text was updated successfully, but these errors were encountered: