Title: | Miscellaneous Functions for 'ggplot2' |
---|---|
Description: | Useful functions and utilities for 'ggplot' object (e.g., geometric layers, themes, and utilities to edit the object). |
Authors: | Guangchuang Yu [aut, cre, cph] , Shuangbin Xu [aut] |
Maintainer: | Guangchuang Yu <[email protected]> |
License: | Artistic-2.0 |
Version: | 0.1.7 |
Built: | 2024-10-24 03:33:35 UTC |
Source: | https://github.com/yulab-smu/ggfun |
This operator attaches annotation data to a ggtree or ggsc graphic object
p %<+% data
p %<+% data
p |
ggplot2 object, such as ggtree or ggsc graphic object. |
data |
data.frame, which must contains a column of |
ggplot object with annotation data added
this element is used to control the line color of panel.grid.major/minor.x or panel.grid.major/minor.y
element_blinds( colour = c("white", "grey60"), axis, color = NULL, inherit.blank = FALSE )
element_blinds( colour = c("white", "grey60"), axis, color = NULL, inherit.blank = FALSE )
colour |
the colour of rectangular, default is c('white', 'grey60'). |
axis |
character, require, option is |
color |
Color is an alias for colour |
inherit.blank |
Should this element inherit the existence of an
|
library(ggplot2) df <- data.frame( x = rep(c(2, 5, 7, 9, 12), 2), y = rep(c(1, 2), each = 5), z = factor(rep(1:5, each = 2)), w = rep(diff(c(0, 4, 6, 8, 10, 14)), 2) ) ggplot(df, aes(x, y)) + geom_tile(aes(fill = z), colour = 'grey50') + theme(panel.grid.major.y = element_blinds(color= c('white', 'grey'), axis='y'))
library(ggplot2) df <- data.frame( x = rep(c(2, 5, 7, 9, 12), 2), y = rep(c(1, 2), each = 5), z = factor(rep(1:5, each = 2)), w = rep(diff(c(0, 4, 6, 8, 10, 14)), 2) ) ggplot(df, aes(x, y)) + geom_tile(aes(fill = z), colour = 'grey50') + theme(panel.grid.major.y = element_blinds(color= c('white', 'grey'), axis='y'))
round rectangle borders and backgrounds
element_roundrect( fill = NULL, colour = NULL, size = NULL, linetype = NULL, color = NULL, r = grid::unit(0.1, "snpc"), inherit.blank = FALSE )
element_roundrect( fill = NULL, colour = NULL, size = NULL, linetype = NULL, color = NULL, r = grid::unit(0.1, "snpc"), inherit.blank = FALSE )
fill |
Fill colour. |
colour , color
|
Line/border colour. Color is an alias for colour. |
size |
text size in pts. |
linetype |
Line type. An integer (0:8), a name (blank, solid, dashed, dotted, dotdash, longdash, twodash), or a string with an even number (up to eight) of hexadecimal digits which give the lengths in consecutive positions in the string. |
r |
the radius of the rounded corners, a |
inherit.blank |
Should this element inherit the existence of an
|
library(ggplot2) p <- ggplot(mpg, aes(displ, cty)) + geom_point() p <- p + facet_grid(cols = vars(cyl)) p <- p + theme(strip.background=element_roundrect(fill="grey40", color=NA, r=0.15)) p p2 <- ggplot(mtcars, aes(mpg, disp, color=factor(cyl), size=cyl)) + geom_point() p2 + theme(legend.background=element_roundrect(color="#808080", linetype=2))
library(ggplot2) p <- ggplot(mpg, aes(displ, cty)) + geom_point() p <- p + facet_grid(cols = vars(cyl)) p <- p + theme(strip.background=element_roundrect(fill="grey40", color=NA, r=0.15)) p p2 <- ggplot(mtcars, aes(mpg, disp, color=factor(cyl), size=cyl)) + geom_point() p2 + theme(legend.background=element_roundrect(color="#808080", linetype=2))
add a facet label to a ggplot or change facet label of a ggplot
facet_set(label, side = "t", angle = NULL)
facet_set(label, side = "t", angle = NULL)
label |
a character or a named vector to label the plot |
side |
to label the plot at which side, either 't' (top) or 'r' (right) |
angle |
angle of the facet label. Default is 0 for side='t' and -90 for side='r'. |
a ggplot with facet label
ggplot2 layer of birthday cake
geom_cake(mapping = NULL, data = NULL, ...)
geom_cake(mapping = NULL, data = NULL, ...)
mapping |
aes mapping |
data |
data |
... |
additional parameters |
ggplot2 layer
Guangchuang Yu
library(ggplot2) ggplot(mtcars, aes(mpg, disp)) + geom_cake() library(ggplot2) ggplot(mtcars, aes(mpg, disp)) + geom_cake()
library(ggplot2) ggplot(mtcars, aes(mpg, disp)) + geom_cake() library(ggplot2) ggplot(mtcars, aes(mpg, disp)) + geom_cake()
draw rectangle boxes as scatter points
geom_scatter_rect( mapping = NULL, data = NULL, asp = 0.6, width = 0.8, height = NULL, ... )
geom_scatter_rect( mapping = NULL, data = NULL, asp = 0.6, width = 0.8, height = NULL, ... )
mapping |
aesthetic mapping, default is NULL |
data |
input data, default is NULL |
asp |
aspect ration of rectangle box (height vs width), only works for height is missing |
width |
width of the rectangles, default is 0.8 |
height |
height of the rectangles |
... |
additional parameters passed to 'geom_rect' |
Guangchuang Yu
geom_segment_c supports coloring segment with continuous colors
geom_segment_c( mapping = NULL, data = NULL, position = "identity", lineend = "butt", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, arrow = NULL, arrow.fill = NULL, ... )
geom_segment_c( mapping = NULL, data = NULL, position = "identity", lineend = "butt", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, arrow = NULL, arrow.fill = NULL, ... )
mapping |
aes mapping |
data |
data |
position |
position |
lineend |
lineend |
na.rm |
logical |
show.legend |
logical |
inherit.aes |
logical |
arrow |
specification for arrow heads, as created by arrow(). |
arrow.fill |
fill color to usse for the arrow head (if closed). |
... |
additional parameter |
add segment layer
Guangchuang Yu
set.seed(2019-06-28) d = data.frame(x = rnorm(10), xend = rnorm(10), y = rnorm(10), yend = rnorm(10), v1 = rnorm(10), v2 = rnorm(10)) library(ggplot2) ggplot(d) + geom_segment_c(aes(x = x, xend = xend, y=y, yend =yend, col0 = v1, col1 = v2)) + scale_color_viridis_c(name = "continuous colored lines") + theme_minimal() + theme(legend.position=c(.2, .85)) + xlab(NULL) + ylab(NULL)
set.seed(2019-06-28) d = data.frame(x = rnorm(10), xend = rnorm(10), y = rnorm(10), yend = rnorm(10), v1 = rnorm(10), v2 = rnorm(10)) library(ggplot2) ggplot(d) + geom_segment_c(aes(x = x, xend = xend, y=y, yend =yend, col0 = v1, col1 = v2)) + scale_color_viridis_c(name = "continuous colored lines") + theme_minimal() + theme(legend.position=c(.2, .85)) + xlab(NULL) + ylab(NULL)
ggplot2 layer of triangle
geom_triangle(mapping = NULL, data = NULL, ...)
geom_triangle(mapping = NULL, data = NULL, ...)
mapping |
aes mapping |
data |
data |
... |
additional parameters |
ggplot2 layer
Shipeng Guo
library(ggplot2) ggplot(mtcars, aes(mpg, disp)) + geom_triangle()
library(ggplot2) ggplot(mtcars, aes(mpg, disp)) + geom_triangle()
layer of scatter points for volcano plot to visualize differential genes
geom_volpoint( mapping = NULL, data = NULL, log2FC_cutoff = 2, p_cutoff = 1e-05, ... )
geom_volpoint( mapping = NULL, data = NULL, log2FC_cutoff = 2, p_cutoff = 1e-05, ... )
mapping |
aesthetic mapping |
data |
input data set |
log2FC_cutoff |
cutoff values for log2FC |
p_cutoff |
cutoff values p-value or adjusted p-value |
... |
additional paramters passed to the layer |
a ggplot
extract aes mapping, compatible with ggplot2 < 2.3.0 & > 2.3.0
get_aes_var(mapping, var)
get_aes_var(mapping, var)
mapping |
aes mapping |
var |
variable |
mapped var
Guangchuang Yu
extract legend from a plot
get_legend(plot)
get_legend(plot)
plot |
a gg or gtable object |
a 'gtable' object of the legend
Guangchuang Yu
extract data from a 'gg' plot
get_plot_data(plot, var = NULL, layer = NULL)
get_plot_data(plot, var = NULL, layer = NULL)
plot |
a 'gg' plot object |
var |
variables to be extracted |
layer |
specific layer to extract the data |
a data frame of selected variables
Guangchuang Yu
convert a ggbreak object to a ggplot object
ggbreak2ggplot(plot)
ggbreak2ggplot(plot)
plot |
a ggbreak object |
a ggplot object
Guangchuang Yu
add manual setting legend
gglegend(mapping, data, geom, p = NULL)
gglegend(mapping, data, geom, p = NULL)
mapping |
aes mapping for the 'geom'. The first mapping should be the one for the legend, while others maybe needed for the 'geom' (e.g., label for geom_text). |
data |
input data frame. If users want to mapping 'VALUE' to 'colour', the input data should contains 'VALUE' and 'colour' (actual value, e.g., 'red' and 'blue') variable. |
geom |
a geom to plot the data for generating the legend and the geom will be plotted invisible. |
p |
a ggplot object. If NULL, the 'last_plot()' will be used. |
add additional legend to a ggplot
a ggplot object
Guangchuang Yu
library(ggplot2) p <- ggplot(mtcars, aes(mpg, disp)) + geom_point() data <- data.frame(colour = c("red", "blue"), VALUE = c("A", "B")) gglegend(aes(colour = VALUE, label=VALUE), data, geom_text, p)
library(ggplot2) p <- ggplot(mtcars, aes(mpg, disp)) + geom_point() data <- data.frame(colour = c("red", "blue"), VALUE = c("A", "B")) gglegend(aes(colour = VALUE, label=VALUE), data, geom_text, p)
identify node by interactive click
## S3 method for class 'gg' identify(x = last_plot(), col = "auto", ...)
## S3 method for class 'gg' identify(x = last_plot(), col = "auto", ...)
x |
tree view |
col |
selected columns to extract. Default is "auto" which will select all columns for 'ggplot' object and 'node' column for 'ggtree' object |
... |
additional parameters, normally ignored |
closest data point
Guangchuang Yu
check whether a plot is a ggbreak object (including 'ggbreak', 'ggwrap' and 'ggcut' that defined in the 'ggbreak' package)
is.ggbreak(plot)
is.ggbreak(plot)
plot |
a plot obejct |
logical value
Guangchuang Yu
test whether input object is produced by ggtree function
is.ggtree(x)
is.ggtree(x)
x |
object |
TRUE or FALSE
Guangchuang Yu
draw border for each of the ggplot legends
keybox(p, grob = "roundrect", gp = NULL)
keybox(p, grob = "roundrect", gp = NULL)
p |
a ggplot object |
grob |
one of 'rect' or 'roundrect' |
gp |
graphic parameter |
grob object
Guangchuang Yu
library(ggplot2) p <- ggplot(mtcars, aes(mpg, disp, color=factor(cyl), size=cyl)) + geom_point() keybox(p, 'roundrect', gp = gpar(col = '#808080', lty = "dashed"))
library(ggplot2) p <- ggplot(mtcars, aes(mpg, disp, color=factor(cyl), size=cyl)) + geom_point() keybox(p, 'roundrect', gp = gpar(col = '#808080', lty = "dashed"))
setting font for ggplot (axis text, label, title, etc.)
set_font(p, family = "sans", fontface = NULL, size = NULL, color = NULL)
set_font(p, family = "sans", fontface = NULL, size = NULL, color = NULL)
p |
ggplot object |
family |
font fammily |
fontface |
font face |
size |
font size |
color |
font color |
TableGrob object
Guangchuang Yu
library(grid) library(ggplot2) d <- data.frame(x=rnorm(10), y=rnorm(10), lab=LETTERS[1:10]) p <- ggplot(d, aes(x, y)) + geom_text(aes(label=lab), size=5) set_font(p, family="Times", fontface="italic", color='firebrick')
library(grid) library(ggplot2) d <- data.frame(x=rnorm(10), y=rnorm(10), lab=LETTERS[1:10]) p <- ggplot(d, aes(x, y)) + geom_text(aes(label=lab), size=5) set_font(p, family="Times", fontface="italic", color='firebrick')
override point legend set by 'aes(shape = I(shape))'
set_point_legend_shape(plot)
set_point_legend_shape(plot)
plot |
a 'gg' plot object |
an updated plot
Guangchuang Yu
filter data for tree annotation layer
td_filter(..., .f = NULL)
td_filter(..., .f = NULL)
... |
Expressions that return a logical value. |
.f |
a function (if any, defaults to NULL) that pre-operate the data |
The 'td_filter()' function returns another function that can be used to subset ggtree() plot data. The function can be passed to the 'data' parameter of geom layer to perform subsetting. All rows that satisy your conditions will be retained.
A function to filter ggtree plot data using conditions defined by '...'.
Guangchuang Yu
For more detailed demonstration of this function, please refer to chapter 12.5.1 of Data Integration, Manipulation and Visualization of Phylogenetic Trees http://yulab-smu.top/treedata-book/index.html by Guangchuang Yu.
## Not run: tree <- rtree(30) ## similar to 'ggtree(tree) + geom_tippoint()' ggtree(tree) + geom_point(data = td_filter(isTip)) ## End(Not run)
## Not run: tree <- rtree(30) ## similar to 'ggtree(tree) + geom_tippoint()' ggtree(tree) + geom_point(data = td_filter(isTip)) ## End(Not run)
mutate data for tree annotation layer
td_mutate(..., .f = NULL)
td_mutate(..., .f = NULL)
... |
additional parameters that pass to dplyr::mutate |
.f |
a function (if any, defaults to NULL) that pre-operate the data |
The 'td_mutate()' function returns another function that can be used to mutate ggtree() plot data. The function can be passed to the 'data' parameter of geom layer to perform adding new variables and preserving existing ones.
A function to mutate ggtree plot data
flatterns a list-column of data frame
td_unnest(cols, ..., .f = NULL)
td_unnest(cols, ..., .f = NULL)
cols |
columns to unnest |
... |
additional parameters that pass to tidyr::unnest |
.f |
a function (if any, defaults to NULL) that pre-operate the data |
The 'td_unnest' function returns another function that can be used to unnest ggtree() plot data. The function can be passed to the 'data' parameter of a geom layer to flattern list-cloumn tree data.
A function to unnest ggtree plot data
Guangchuang Yu
For demonstration of this function, please refer to chapter 12.5.2 of Data Integration, Manipulation and Visualization of Phylogenetic Trees http://yulab-smu.top/treedata-book/index.html by Guangchuang Yu.
the theme of blind-like
theme_blinds(colour = c("white", "grey"), axis = "y", ...)
theme_blinds(colour = c("white", "grey"), axis = "y", ...)
colour |
the colour of rectangular, default is c('white', 'grey60'). |
axis |
character which grid of axis will be filled, default is 'y'. |
... |
additional parameters that passed to |
ggplot2 theme
library(ggplot2) iris |> tidyr::pivot_longer( cols = !Species, names_to = 'var', values_to = 'value' ) |> ggplot( aes(x=var, y=Species, color=value, size=value) ) + geom_point() -> p p + theme_blinds( colour = c('grey90', 'white'), axis = 'y', axis.line.y=element_line() ) p + theme_blinds( colour = c('grey90', 'white'), axis = 'x', axis.line.x = element_line() )
library(ggplot2) iris |> tidyr::pivot_longer( cols = !Species, names_to = 'var', values_to = 'value' ) |> ggplot( aes(x=var, y=Species, color=value, size=value) ) + geom_point() -> p p + theme_blinds( colour = c('grey90', 'white'), axis = 'y', axis.line.y=element_line() ) p + theme_blinds( colour = c('grey90', 'white'), axis = 'x', axis.line.x = element_line() )
theme format painter
theme_fp(x, i)
theme_fp(x, i)
x |
ggplot object to provide theme format |
i |
the element of a theme provided by |
It applies theme element (i) from a ggplot (x) to another ggplot object
theme element
Guangchuang Yu and Shuangbin Xu
A theme that has no margin
theme_no_margin(...)
theme_no_margin(...)
... |
additional parameters that passed to theme() |
ggplot2 theme
Guangchuang Yu
A theme that only show the plot panel
theme_nothing(base_size = 11, base_family = "")
theme_nothing(base_size = 11, base_family = "")
base_size |
font size |
base_family |
font family |
ggplot2 theme
Guangchuang Yu
A theme that only show y-axis
theme_noxaxis(color = "black", ...) theme_noyaxis(color = "black", ...) theme_noaxis(...)
theme_noxaxis(color = "black", ...) theme_noyaxis(color = "black", ...) theme_noaxis(...)
color |
color of y-axis |
... |
additional parameters that passed to theme() |
ggplot2 theme
Guangchuang Yu
the theme of blind-like alias of theme_blinds
theme_stamp(colour = c("white", "grey"), axis = "y", ...)
theme_stamp(colour = c("white", "grey"), axis = "y", ...)
colour |
the colour of rectangular, default is c('white', 'grey60'). |
axis |
character which grid of axis will be filled, default is 'y'. |
... |
additional parameters that passed to |
transparent background theme
theme_transparent(...)
theme_transparent(...)
... |
additional parameter to tweak the theme |
ggplot object
Guangchuang Yu with contributions from Hugo Gruson
volcano plot
volplot(data, mapping, log2FC_cutoff = 2, p_cutoff = 1e-05, ...)
volplot(data, mapping, log2FC_cutoff = 2, p_cutoff = 1e-05, ...)
data |
input data set |
mapping |
aesthetic mapping |
log2FC_cutoff |
cutoff values for log2FC |
p_cutoff |
cutoff values p-value or adjusted p-value |
... |
additional paramters passed to the 'geom_volpoint' layer |
a ggplot
extract x or y ranges of a ggplot
yrange(gg, type = "limit", region = "panel") xrange(gg, type = "limit", region = "panel") ggrange(gg, var, type = "limit", region = "panel")
yrange(gg, type = "limit", region = "panel") xrange(gg, type = "limit", region = "panel") ggrange(gg, var, type = "limit", region = "panel")
gg |
a ggplot object |
type |
one of 'limit' or 'range', if 'region == "plot"', to extract plot limit or plot data range |
region |
one of 'panel' or 'plot' to indicate extracting range based on the plot panel (scale expand will be counted) or plot data (scale expand will not be counted) |
var |
either 'x' or 'y' |
range of selected axis
Guangchuang Yu