Last active
September 16, 2022 16:51
-
-
Save sahilseth/37a567810a8bdcb64bc3b96b33340b42 to your computer and use it in GitHub Desktop.
ggplot examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override legend | |
# get it inside the plot, and remove alpha | |
theme(legend.position = c(0, 0.2), | |
legend.background = element_blank()) + | |
guides(colour = guide_legend(override.aes = list(alpha = 1))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 1 | |
# https://github.com/kassambara/ggpubr | |
my_comparisons <- list( c("gb", "gc"), c("gb", "noref")) | |
ggplot(df_cnts_per_samp, aes(ref_type, n, color = ref_type)) + | |
geom_jitter() + | |
geom_violin(alpha = 0) + | |
theme_cowplot() + | |
scale_y_log10() + | |
scale_color_npg(name = "") + | |
xlab("Reference type") + | |
ylab("Number of mutations") + | |
stat_compare_means(comparisons = my_comparisons) + # Add pairwise comparisons p-value | |
stat_compare_means(label.y = 1, label.x = "gc") # Add global p-value | |
# to have different groups, follow this: | |
# https://www.datanovia.com/en/lessons/ggplot-stripchart/ | |
ggplot(x.subset, aes(x = lbl, y = value, color = gene_id)) + | |
stat_summary(aes(color = gene_id), fun.data = "mean_sdl", geom="pointrange", shape=18, size=0.8, position = position_dodge(0.8)) + | |
geom_jitter(aes(x = lbl, y = value, color = gene_id), alpha = 0.5, | |
size = 1.2, position = position_jitterdodge(jitter.width = 0.2, dodge.width = 0.8)) + | |
# geom_violin(alpha = 0) + | |
scale_color_manual(values = gene_colors, name = NULL) + | |
theme_cowplot() + |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://github.com/kassambara/ggpubr | |
my_comparisons <- list( c("gb", "gc"), c("gb", "noref")) | |
p2 = ggplot(df_cnts_per_samp, aes(ref_type, n, color = ref_type)) + | |
geom_jitter() + | |
geom_violin(alpha = 0) + | |
theme_cowplot() + | |
scale_y_log10( | |
breaks = scales::trans_breaks("log10", function(x) 10^x), | |
labels = scales::trans_format("log10", scales::math_format(10^.x))) + | |
# annotation_logticks() + | |
scale_color_npg(name = "") + | |
xlab("Reference type") + | |
ylab("Number of mutations") + | |
stat_compare_means(comparisons = my_comparisons) + # Add pairwise comparisons p-value | |
stat_compare_means(label.y = 1, label.x = "gc") # Add global p-value |
add mean comparisons (boxplot/violin)
my_comparisons <- list( c("gb", "gc"), c("gb", "noref"))<br class="Apple-interchange-newline">
stat_compare_means(comparisons` = my_comparisons) + # Add pairwise comparisons p-value
stat_compare_means(label.y = 1, label.x = "gc") # Add global p-value
remove color guide
theme(legend. title = element_blank()) .
theme(legend. position = "none") .
guides(color = FALSE)
change axis ticks label
labs <- c("Citrus", "Crop", "Cypress Swamp")
+ scale_x_discrete(labels= labs)
add mean SD
stat_summary(fun.data = "mean_sdl", geom="pointrange", shape=18, size=0.8, position = position_dodge(0.8))
add regression line and correlation
geom_point() +
geom_smooth(method='lm') +
ggpubr::stat_cor() +
theme_cowplot()
theme cowplot font:
theme_cowplot(font_size = 30) +
computerworld snippets:
# From Computerworld: ggplot2 code snippets
snippet myg_barplot_basic
ggplot(${1:mydataframe}, aes(x=${2:myxcolname}, y=${3:myycolname})) +
geom_bar(stat="identity")
snippet myg_barplot_basic_reorderXaxisByYvalue
ggplot(${1:mydataframe}, aes(x=reorder(${2:myxcolname}, -${3:myycolname}), y=${3:myycolname})) +
geom_bar(stat="identity")
snippet myg_barplot_groupedByColor
ggplot(${1:mydataframe}, aes(x=${2:myxcolname}, y=${3:myycolname}, fill=${4:mygroupcolname})) +
geom_bar(stat="identity", position="dodge")
snippet myg_barplot_basic_barsSetOneColor
ggplot(${1:mydataframe}, aes(x=${2:myxcolname}, y=${3:myycolname})) +
geom_bar(fill="${4:mycolor}", stat="identity")
snippet myg_barplot_basic_bars_blue
ggplot(${1:mydataframe}, aes(x=${2:myxcolname}, y=${3:myycolname})) +
geom_bar(stat="identity", color = "black", fill="#0072B2")
snippet myg_barplot_barslabeled
# Code from an R Graphics Cookbook recipe, by Winston Chang
ggplot(data = ${1:mydataframe}, aes(x=${2:myxcolname}, y=${3:myycolname})) +
geom_bar(stat="identity", color = "black", fill="#0072B2") +
ggtitle("${4:My Headline}") +
xlab("${5:myXaxisname}") +
ylab("${6:myYaxisname}") +
theme_classic() +
geom_text(aes(label=${3:myycolname}), vjust=1.5, colour="white", position=position_dodge(.9), size=5) +
theme(plot.title=element_text(size=24))
snippet myg_barplot_barslabeled_reorderXaxisByYvalue
# Code adapted from an R Graphics Cookbook recipe, by Winston Chang
ggplot(${1:mydataframe}, aes(x=reorder(${2:myxcolname}, -${3:myycolname}), y=${3:myycolname})) +
geom_bar(stat="identity", color = "black", fill="#0072B2") +
ggtitle("${4:myheadline}") +
xlab("${5:myXaxisname}") +
ylab("${6:myYaxisname}") +
theme_classic() +
geom_text(aes(label=${3:myycolname}), vjust=1.5, colour="white", position=position_dodge(.9), size=5) +
theme(plot.title=element_text(size=24))
snippet myg_barplot_grouped_withColorPaletteAndHeadline
ggplot(data = ${1:mydataframe}, aes(x=${2:myXcolname}, y=${3:myYcolname}, fill=${4:mygroup/colorBycolname} )) +
geom_bar(stat="identity", position="dodge") +
scale_fill_brewer(palette="Dark2") +
ggtitle("${5:My Headline}") +
theme(plot.title=element_text(size=22))
snippet myg_scatterplot_basic
ggplot(${1:mydataframe}, aes(x=${2:myxcolname}, y=${3:myycolname})) +
geom_point()
snippet myg_scatterplot_setPointSize
ggplot(${1:mydataframe}, aes(x=${2:myxcolname}, y=${3:myycolname})) +
geom_point(size=${4:myPointSizeNumber})
snippet myg_scatterplot_setColorByDataValue
ggplot(${1:mydataframe}, aes(x=${2:myxcolname}, y=${3:myycolname})) +
geom_point(aes(color=${4:myColorBycolname})) + scale_color_gradient(low="${5:mylowcolor}", high="${6:myhighcolor}")
snippet myg_scatterplot_setColorBrewerPaletteByDataValue
ggplot(${1:mydataframe}, aes(x=${2:myxcolname}, y=${3:myycolname})) +
geom_point(aes(color=${4:myColorBycolname})) + scale_color_brewer(type="seq", palette="${5:mypalettechoice}")
snippet myg_scatterplot_groupAndAutoAnnotateByColor
# requires package directlabels
library("directlabels")
myplot <- ggplot(${1:mydataframe}, aes(x=${2:myxcolname}, y=${3:myycolname}, color=${4:mygroupingcol})) + geom_point()
direct.label(myplot, "smart.grid")
snippet myg_scatterplot_addJitter
+ geom_point(position="jitter")
snippet myg_line_basic
ggplot(${1:mydataframe}, aes(x=${2:myxcolname}, y=${3:myycolname})) +
geom_line()
snippet myg_line_linesByCategory
ggplot(${1:mydataframe}, aes(x=${2:myxcolname}, y=${3:myycolname})) +
geom_line(aes(color=${4:mycolnameForEachLine}))
snippet myg_line_goupAndAutoAnnotateByColor
# requires package directlabels; code via https://goo.gl/jpSDtw
library("directlabels")
myplot <- ggplot(${1:mydataframe}, aes(x=${2:myxcolname}, y=${3:myycolname})) + geom_line(aes(color=${4:mycolnameForEachLine}))
direct.label(myplot, list(last.points, hjust = 0.7, vjust = 1))
snippet myg_add_horizontalLine
+ geom_hline(yintercept=${1:theYintercept})
snippet myg_add_vertictalLine
+ geom_vline(xintercept=${1:theXintercept})
snippet myg_headline_setTextAndSize
+ ggtitle("${1:myheadlinetext}") +
theme(plot.title = element_text(size = ${2:myintegerPointSize}))
snippet myg_headline_makeBold
+ theme(plot.title = element_text(face="bold"))
snippet myg_Xaxis_setTitle
+ xlab("${1:My x-axis title text}")
snippet myg_Yaxis_setTitle
+ ylab("${1:My y-axis title text}")
snippet myg_Xaxis_setCatVariableLabels
+ scale_x_discrete(labels=${1:myvectoroflabels})
snippet myg_Yaxis_setContVariableLabels
+ scale_y_continuous(breaks=${1:myvectorofbreaks})
snippet myg_Yaxis_setLimits
+ ylim(${1:myYmin}, ${2:myYmax})
snippet myg_Xaxis_rotateLabels45degrees
+ theme(axis.text.x= element_text(angle=45, hjust = 1.3, vjust = 1.2))
snippet myg_Yaxis_rotateTitle
+ theme(axis.title.y = element_text(angle = 0))
snippet myg_legend_off
+ theme(legend.position = "none")
snippet myg_multiplots_1factor_2columns
+ facet_wrap(~ ${1:mycolname}, ncol=2)
snippet myg_multiplots_2factors_2columns
+ facet_wrap(${1:mycolname1} ~ ${2:mycolname2}, ncol=2)
snippet myg_multiplots_1factor_2rows
+ facet_wrap(~ ${1:mycolname}, nrow=2)
snippet myg_multiplots_2factors_2rows
+ facet_wrap(${1:mycolname1} ~ ${2:mycolname2}, nrow=2)
snippet myg_multiplots_2factors
+ facet_grid(${1:mycolname1} ~ ${2:mycolname2})
snippet myg_multiplots_unrelatedPlots_1column
# gridExtra package required; enter any number of existing plots, each separated by a comma
gridExtra::grid.arrange(${1:plot1}, ${2:plot2}, ${3:plot3})
snippet myg_multiplots_unrelatedPlots_SetNumColumns
# gridExtra package required; enter any number of existing plots, each separated by a comma
gridExtra::grid.arrange(${1:plot1}, ${2:plot2}, ${3:plot3}, ncol=${4:numcolumns})
snippet myg_annotate_addByXYposition
+ annotate("text", x=${1:myxposition}, y=${2:myyposition}, label="${3:My text}")
snippet myg_corrplot
# requires package corrplot
mycorrelationmatrix <- cor(${1:mymatrix})
col <- colorRampPalette(c("#BB4444", "#EE9988", "#FFFFFF", "#77AADD", "#4477AA"))
corrplot::corrplot(mycorrelationmatrix, method="shade", shade.col=NA, tl.col="black", tl.srt=45, col=col(200), addCoef.col="black")
snippet myg_interactive_histogram
# requires package ggvis
library(ggvis)
${1:mydataframe} %>%
ggvis(x = ~${2:mydatacolumn}, fill := "gray") %>%
layer_histograms(width = input_slider(min=${3:mysliderMinValue}, max=${4:mysliderMax}, value= ${5:mysliderStartingValue}, label = "Bin Width"))
# end Computerworld ggplot2 code snippets
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
facet labeller