geom_fruit: plot tree with associated data in another method.

View source: R/geom_fruit.R

geom_fruitR Documentation

plot tree with associated data in another method.

Description

'geom_fruit()' can automatically re-arrange the input 'data' according to the tree structure. It can present the associated data on the external panels of the tree using the 'geom' function defined in 'ggplot2' or other ggplot2-based packages with aesthetic 'mapping' and other parameters, and it will align the external layers in the outer ring of circular layout tree or with rectangular layout tree side by side. Note: the tree should be created by 'ggtree'.

Usage

geom_fruit(
  mapping,
  data = NULL,
  geom,
  offset = 0.03,
  pwidth = 0.2,
  position = "auto",
  inherit.aes = FALSE,
  grid.params = NULL,
  axis.params = list(axis = "none", text.angle = 0, text.size = 0.8, text = NULL, title =
    NULL, title.size = 3, title.height = 0.1, title.angle = 0, title.color = "black",
    nbreak = 4, line.size = 0.2, line.color = "grey", line.alpha = 1, limits = NULL, ...),
  ...
)

fruit_plot(
  p,
  data = NULL,
  geom,
  mapping,
  offset = 0.03,
  pwidth = 0.2,
  position = "auto",
  ...
)

Arguments

mapping

aes mapping for 'geom'

data

data to plot by 'geom', the column contained tree tip labels should be as y in mapping.

geom

geom function to plot the data.

offset

numeric, distance between external layers or between tree and external layers, default is 0.03, meaning the 0.03 times of x range of tree (0.03 * xrange of tree).

pwidth

numeric, the width of external geometric layer, default is 0.2, meaning the 0.2 times of x range of tree (0.2 * xrange of tree).

position

Position adjustment, either as a string, or the result of a call to a position adjustment function, default is 'auto', see details in the following.

inherit.aes

logical, If 'FALSE', overrides the default aesthetics, rather than combining with them. This is most useful for helper functions that define both data and aesthetics and shouldn't inherit behaviour from the default plot specification, default is FALSE.

grid.params

list, the parameters to control the attributes of grid lines, default is NULL, see the grid.params in the following.

grid.params control the attributes of grid line of external layer, it can be referred to the following parameters:

  • vline logical, whether add the vertical line, default is FALSE.

  • color color of line, default is grey.

  • size the width of line, default is 0.2.

  • alpha the colour transparency of line, default is 1.

  • lineend Line end style (round, butt, square), default is "butt".

  • linejoin Line end style (round, butt, square), default is "round".

  • linetype Type of line, default is 1.

axis.params

list, the parameters to control the attributes of pseudo axis, see the axis.params in the following.

axis.params control the attributes of axis, it can be referred to the following parameters:

  • axis character, add the axis, if it is set to "none", meaning don't display axis (default), "x" display the x axis, "y" display the y axis, "xy" display the two axis.

  • text vector, the text of axis x, default is NULL, it is only valid when the text of axis is single and x is discrete.

  • vjust numeric, A numeric specifying vertical justification, default is 0.5.

  • hjust numeric, A numeric specifying horizontal justification, default is 0.5.

  • text.angle numeric, the angle of axis text, default is 0.

  • text.size numeric, the size of axis text, default is 0.8.

  • title character, the title of panel or x-axis label, default is NULL, it is only valid when "x" axis exists.

  • title.size numeric, the size of title text, default is 3.

  • title.height numeric, the height of title text position more than tree, default is 0.1, it is relative to height of tree.

  • title.angle numeric, the angle of title text, default is 0.

  • title.color character, the color of title text, default is "black".

  • nbreak numeric, meaning the number of axis to break, integer giving the desired number of intervals. Non-integer values are rounded down. It is only valid when x is continuous, default is 4.

  • line.size numeric, the size of axis line, default is 0.2.

  • line.color character, the color of axis line color, default is "grey".

  • line.alpha numeric, the colour transparency of axis line, default is 1.

...

additional parameters for 'geom'

p

tree view

Details

The 'data' parameter is data.frame or tibble type, it is the same with the data for corresponding geometric layers, but it must contain one column of taxa labels of tree, it will be mapped to 'y' axis in 'mapping'. When 'data' is not provided, the associated data in tree data will be extracted automatically, and the 'y' axis don't need to be mapped.

The 'mapping' parameter is setting of aesthetic mappings created by 'aes()' or 'aes_()' of 'ggplot2', the 'y' should be assigned to the variable names of column of taxa labels in data.frame of 'data', only if the 'data' is not provided, see the above.

The 'geom' parameter is the geometric function defined in 'ggplot2' or other 'ggplot2-extension', e.g.

ggplot2 geom_bar,geom_col,geom_boxplot,geom_violin,geom_tile circular, rectangular
ggmsa geom_msa rectangular
ggstar geom_star circular, rectangular
ggimage geom_image,geom_phylopic circular, rectangular
ggpmisc geom_plot,geom_table circular, rectangular
ggridges geom_density_ridges circular, rectangular
ggtext geom_richtext circular, rectangular
...

if the 'geom' is 'geom_bar', 'geom_col', 'geom_boxplot', 'geom_violin', the 'orientation' should be specified to 'y'.

The default 'position' parameter is 'auto', it will guess and determine (hopefully) a suitable position for the specified geometric layer. That means using 'position_stackx()' for geom_bar(), 'position_dodgex()' for 'geom_violin()' and 'geom_boxplot()', and 'position_identityx()' for others (e.g. geom_point() and geom_tile() etc.). A geometric layer that has a position parameter should be compatible with geom_fruit(), as it allows using position functions defined in the ggtreeExtra package to adjust output layer position.

and the grid line also can be added using 'grid.params=list(...)'.

The axis line and text can be added using 'axis.params=list(axis="x",...)'.

The 'p' parameter only work when you use fruit_plot(), which is alias of geom_fruit().

Value

ggplot object

Author(s)

Shuangbin Xu and Guangchuang Yu

Examples

library(ggtree)
library(ggplot2)
library(ggstar)
set.seed(1024)
tr <- rtree(100)
dd = data.frame(id=tr$tip.label, value=abs(rnorm(100)))
dt = data.frame(id=tr$tip.label, group=c(rep("A",50),rep("B",50)))
p <- ggtree(tr, layout="circular") 

p1 <- p + 
      geom_fruit(
          data=dt,
          geom=geom_star,
          mapping=aes(y=id, fill=group),
          size=2.5,
          starstroke=0
      )
p2 <- p1 + 
      geom_fruit(
          data=dd, 
          geom=geom_col, 
          mapping=aes(x=value, y=id)
      )  

p <- p %<+% dd %<+% dt
p5 <- p + 
      geom_fruit(
          geom = geom_star,
          mapping = aes(y=id, fill=group),
          size = 2.5,
          starstroke = 0
      ) +
      geom_fruit(
          geom = geom_col,
          mapping = aes(x=value, y=id),
          pwidth = .3,
          axis.params = list(
                           axis = 'x',
                           text.size = 2,
                           nbreak = 2, 
                           text.angle = -40, 
                           vjust = 1, 
                           hjust = 0,
                           limits = c(0, 2)
                        ), 
          grid.params = list()
      )

xiangpin/ggadd documentation built on Nov. 6, 2024, 11:04 p.m.