{"id":14068209,"url":"https://github.com/m-jahn/lattice-tools","last_synced_at":"2025-06-26T21:36:16.068Z","repository":{"id":157987439,"uuid":"258203945","full_name":"m-jahn/lattice-tools","owner":"m-jahn","description":"Panel functions and wrappers to extend the R lattice universe","archived":false,"fork":false,"pushed_at":"2023-11-26T19:51:47.000Z","size":8831,"stargazers_count":11,"open_issues_count":0,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-05-20T15:15:13.075Z","etag":null,"topics":["helper-functions","lattice-plots","r-package","r-plots","r-programming"],"latest_commit_sha":null,"homepage":null,"language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/m-jahn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-04-23T13:05:14.000Z","updated_at":"2025-04-10T14:21:11.000Z","dependencies_parsed_at":null,"dependency_job_id":"20793636-3095-4ced-8066-73cfb664c731","html_url":"https://github.com/m-jahn/lattice-tools","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/m-jahn/lattice-tools","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/m-jahn%2Flattice-tools","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/m-jahn%2Flattice-tools/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/m-jahn%2Flattice-tools/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/m-jahn%2Flattice-tools/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/m-jahn","download_url":"https://codeload.github.com/m-jahn/lattice-tools/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/m-jahn%2Flattice-tools/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262145387,"owners_count":23265909,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["helper-functions","lattice-plots","r-package","r-plots","r-programming"],"created_at":"2024-08-13T07:06:01.590Z","updated_at":"2025-06-26T21:36:16.043Z","avatar_url":"https://github.com/m-jahn.png","language":"R","funding_links":[],"categories":["R"],"sub_categories":[],"readme":"lattice-tools\n================\nMichael Jahn,\n2023-09-05\n\n\u003c!-- badges start --\u003e\n\n[![R build\nstatus](https://github.com/m-jahn/lattice-tools/workflows/R-CMD-check/badge.svg)](https://github.com/m-jahn/lattice-tools/actions)\n[![Maintenance](https://img.shields.io/badge/Maintained%3F-yes-green.svg)](https://github.com/m-jahn)\n![GitHub\nissues](https://img.shields.io/github/issues/m-jahn/lattice-tools)\n![GitHub last\ncommit](https://img.shields.io/github/last-commit/m-jahn/lattice-tools)\n![Platform](https://img.shields.io/badge/platform-all-green)\n\u003c!-- badges end --\u003e\n\n------------------------------------------------------------------------\n\nPanel functions and wrappers that extend the R lattice universe\n\n## Description\n\nThis package contains panel functions and themes for the R `lattice`\npackage, a general purpose plotting package from Deepayan Sarkar.\nLattice’s functionality is comparable to the popular `ggplot2` package\nbut has a slightly different look and feel. The functions in this\npackage adhere as far as possible to the `lattice` conventions but might\nin some cases deviate from default behavior. However, some care was\ntaken to replicate the original lattice behavior so that users can apply\ngrouping and paneling as they are used to. Feel free to copy, fork or\nsource functions that you find useful. Contributions welcome!\n\n## Installation\n\nTo install the package directly from github, use this function from the\n`devtools` package in your R session:\n\n``` r\nrequire(devtools)\ndevtools::install_github(\"https://github.com/m-jahn/lattice-tools\")\n```\n\n## Panel functions\n\nThese functions extend or simplify the `panel.function` landscape for\n`lattice`.\n\n### panel.annotate\n\nDraw summary text labels in lattice plots. This panel function allows to\ndraw text labels such as the mean on arbitrary groups of data points.\nText labels will be drawn for groups of identical x values with optional\nsubsetting by grouping or paneling variables. This function complements\n`panel.errbars()` and `panel.barplot` as it supports drawing labels\nbeside each other for different groups.\n\n``` r\nlibrary(lattice)\nlibrary(latticetools)\ndata(mtcars)\n\n# annotate mean values\nxyplot(mpg ~ factor(cyl) | factor(vs), mtcars,\n  lwd = 2, pch = 19, offset = 2, digits = 1,\n  panel = function(x, y, ...) {\n    panel.errbars(x, y, ...)\n    panel.annotate(x, y, ...)\n  }\n)\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-3-1.png)\u003c!-- --\u003e\n\n``` r\n# works also with grouping variable. Takes the same arguments\n# \"beside\" and \"ewidth\" as panel.errbars() and panel.barplot()\n# to plot labels for different groups aside of each other\nxyplot(mpg ~ factor(cyl) | factor(vs), mtcars,\n  lwd = 2, pch = 19, groups = gear, digits = 1,\n  offset = 3, beside = TRUE,\n  panel = function(x, y, ...) {\n    panel.errbars(x, y, ...)\n    panel.annotate(x, y, ...)\n  }\n)\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-3-2.png)\u003c!-- --\u003e\n\n### panel.arrowbox\n\nPanel function to draw boxes with arrow head from 2 XY coordinates and\ndirection argument. Each argument can be a vector of same length so that\nmultiple arrows can be drawn at once. Is internally used in\n`panel.geneplot`.\n\n``` r\nlibrary(lattice)\n\nxyplot(1:3 ~ 4:6, col = \"#0080ff\",\n  panel = function(x, y, ...) {\n    panel.arrowbox(x0 = c(4, 5), y0 = c(2.5, 1),\n      x1 = c(5, 6), y1 = c(3, 1.5),\n      direction = c(1, -1), ...)\n  }\n)\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-4-1.png)\u003c!-- --\u003e\n\n### panel.barplot\n\nDraw a barplot with error bars in lattice plots. This panel function\nallows to draw barplots with error bars for arbitrary groups of data\npoints. Error bars will be drawn for groups of identical x values with\noptional subsetting by grouping or paneling variables. This function is\nvery similar to `panel.errbars` only with bars instead of points.\n\n``` r\n# mean and stdev error bars are drawn for\n# common x values\nxyplot(mpg ~ factor(cyl), mtcars, lwd = 2, \n  panel = function(x, y, ...) {\n    panel.barplot(x, y, ...)\n  }\n)\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-5-1.png)\u003c!-- --\u003e\n\n``` r\n# using the same variable for x and grouping will\n# result in typical lattice behavior\nxyplot(mpg ~ factor(cyl), mtcars, \n  groups = cyl, lwd = 2,\n  panel = function(x, y, ...) {\n    panel.barplot(x, y, ...)\n  }\n)\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-5-2.png)\u003c!-- --\u003e\n\n``` r\n# we can also use different variables for the x var, grouping,\n# and paneling. As a visual control that error bars are drawn\n# for the correct groups we overlay the single data points.\nxyplot(mpg ~ factor(cyl) | factor(vs), mtcars,\n  groups = gear, lwd = 2, auto.key = list(columns = 3),\n  panel = function(x, y, ...) {\n    panel.barplot(x, y, beside = TRUE, draw_points = TRUE, ...)\n  }\n)\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-5-3.png)\u003c!-- --\u003e\n\n``` r\n# alternatively, means and error margins can be supplied directly. \n# In this case means are supplied as unique combinations\n# of y and x while error_margin is a separate vector with same length as y.\nmtcars_means \u003c- data.frame(\n  cyl = sort(unique(mtcars$cyl)),\n  mpg = with(mtcars, tapply(mpg, cyl, mean)),\n  stdev = with(mtcars, tapply(mpg, cyl, sd))\n)\n\n# you might have to adjust the y-scale as it is determined from the\n# range of the y variable only, ignoring the extension through error bars.\nxyplot(mpg ~ factor(cyl), mtcars_means,\n  error_margin = mtcars_means$stdev,\n  ylim = c(9, 36), groups = cyl,\n  lwd = 2, pch = 19, cex = 1.5,\n  panel = function(x, y, ...) {\n    panel.barplot(x, y, ...)\n  }\n)\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-5-4.png)\u003c!-- --\u003e\n\n``` r\n# if you supply a two column matrix as the error_margin argument,\n# error bars with different lower and upper bounds can be drawn\nerror_mat \u003c- matrix(ncol = 2, 1:6)\nxyplot(mpg ~ factor(cyl), mtcars_means,\n  error_margin = error_mat, twoway = TRUE, fill = NA,\n  ylim = c(9, 36), groups = cyl,\n  lwd = 2, pch = 19, cex = 1.5,\n  panel = function(x, y, ...) {\n    panel.barplot(x, y, ...)\n  }\n)\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-5-5.png)\u003c!-- --\u003e\n\n### panel.beeswarm\n\nPanel function for beeswarm plots. This panel function works essentially\nlike a stripplot, but instead of randomly scattering a variable produces\na regular, grid-like pattern of points. Currently only the X variable is\ntransformed. Continuously distributed data points can be optionally\ndiscretized, both for X and for Y variable.\n\n``` r\n# simple example\ndf \u003c- data.frame(\n  Y = sample(1:10, 60, replace = TRUE), \n  X = factor(rep(1:3, each = 20))\n)\n\n# beeswarm plot\nxyplot(Y ~ X, df, groups = X, panel = panel.beeswarm)\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-6-1.png)\u003c!-- --\u003e\n\n``` r\n# but with continuous Y variable, it doesn't work as expected\ndf$Y \u003c- rnorm(60)\nxyplot(Y ~ X, df, groups = X, panel = panel.beeswarm)\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-6-2.png)\u003c!-- --\u003e\n\n``` r\n# for this purpose we can bin the Y variable into groups\nxyplot(Y ~ X, df, groups = X, \n  panel = function(x, y, ...) {\n    panel.beeswarm(x, y, bin_y = TRUE, breaks_y = 10, ...)\n})\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-6-3.png)\u003c!-- --\u003e\n\n``` r\n# the breaks for Y bins are computed for each panel independently.\n# we can also supply fixed bins via the 'breaks_y' argument\n# to obtain the same binning for each panel\nxyplot(Y ~ factor(rep(1, length(Y))) | X, df, groups = X,\n  panel = function(x, y, ...) {\n    panel.beeswarm(x, y, bin_y = TRUE,\n      breaks_y = seq(-4, 4, length.out = 20), ...)\n})\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-6-4.png)\u003c!-- --\u003e\n\n### panel.directlabels\n\nPoint labels for scatterplots. Draw text labels for all points of a\nscatterplot using internal functionality from the `directlabels`\npackage. Note: an alternative panel function, `panel.repellabels`, is a\nwrapper using ggrepel calculated label positions instead. The same\nbehavior can be achieved by using “ggrepel” for the `positioning`\nargument.\n\nIn contrast to the original `directlabels` package, *every point* is\nlabeled instead of groups of points. Labels are also independent from\nthe grouping variable, so that e.g. colors indicate one grouping\nvariable and labels another. By default, labels adapt the graphical\nparameters of the higher level plot, including coloring according to\ngroups. However, many parameters can be customized.\n\n``` r\nlibrary(grid)\nlibrary(lattice)\n\ndata(\"mtcars\")\nmtcars$car \u003c- rownames(mtcars)\n\n# A standard example using lattice grouping and paneling;\n# We can also draw boxes around labels and change label size\nxyplot(mpg ~ wt | factor(cyl), mtcars,\n  groups = cyl, pch = 19, labels = mtcars$car,\n  as.table = TRUE, layout = c(3, 1), cex = 0.6,\n  panel = function(x, y, ...) {\n    panel.xyplot(x, y, ...)\n    panel.directlabels(x, y, draw_box = TRUE, box_line = TRUE, ...)\n  }\n)\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-7-1.png)\u003c!-- --\u003e\n\n``` r\n# A similar plot but without grouping. This requires explicit\n# use of subscripts\nxyplot(mpg ~ wt | factor(cyl), mtcars,\n  pch = 19, labels = mtcars$car,\n  as.table = TRUE, layout = c(3, 1), cex = 0.6,\n  panel = function(x, y, subscripts, ...) {\n    panel.xyplot(x, y, ...)\n    panel.directlabels(x, y, subscripts = subscripts, \n      draw_box = TRUE, box_fill = \"white\", ...)\n  }\n)\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-7-2.png)\u003c!-- --\u003e\n\n``` r\n# An example without panels and more groups\nxyplot(mpg ~ wt, mtcars,\n  groups = hp, pch = 19, \n  labels = mtcars$wt, cex = 0.6,\n  panel = function(x, y, ...) {\n    panel.xyplot(x, y, ...)\n    panel.directlabels(x, y, draw_box = TRUE, box_line = TRUE, ...)\n  }\n)\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-7-3.png)\u003c!-- --\u003e\n\n### panel.dumbbell\n\nMake a dumbbell chart. Similar to a lollipop chart that shows a single\npoint supported by a horizontal or vertical line, the dumbbell chart\nshows the minimum and maximum of a distribution connected by a line. The\ndirection of the dumbbell (i.e. vertical or horizontal) can be specified\nmanually, or it is determined from the data automatically (the default).\n\n``` r\nlibrary(lattice)\ndata(mtcars)\n\n# vertical dumbbells\nxyplot(mpg ~ factor(gear), mtcars,\n  groups = gear,\n  pch = 19, lwd = 2,\n  panel = function(x, y, ...) {\n    panel.dumbbell(x, y, ...)\n  }\n)\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-8-1.png)\u003c!-- --\u003e\n\n``` r\n# horizontal dumbbells\nxyplot(factor(gear) ~ mpg, mtcars,\n  groups = gear,\n  pch = 19, lwd = 2,\n  panel = function(x, y, ...) {\n    panel.dumbbell(x, y, ...)\n  }\n)\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-8-2.png)\u003c!-- --\u003e\n\n### panel.errbars\n\nDraw error bars in lattice plots. This panel function allows to draw\nsymbols with error bars for arbitrary groups of data points. Error bars\nwill be drawn for groups of identical x values with optional subsetting\nby grouping or paneling variables. This function is very similar to\n`panel.barplot` only with points instead of bars.\n\n``` r\nlibrary(lattice)\ndata(mtcars)\n\n# mean and stdev error bars are drawn for\n# common x values\nxyplot(mpg ~ factor(cyl), mtcars, \n  lwd = 2, pch = 19, cex = 1.5,\n  panel = function(x, y, ...) {\n    panel.errbars(x, y, ...)\n  }\n)\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-9-1.png)\u003c!-- --\u003e\n\n``` r\n# using the same variable for x and grouping will\n# result in typical lattice behavior\nxyplot(mpg ~ factor(cyl), mtcars,\n  groups = cyl, lwd = 2, pch = 19, cex = 1.5,\n  panel = function(x, y, ...) {\n    panel.errbars(x, y, ...)\n  }\n)\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-9-2.png)\u003c!-- --\u003e\n\n``` r\n# we can also use different variables for the x var, grouping,\n# and paneling.\nxyplot(mpg ~ factor(cyl) | factor(vs), mtcars,\n  groups = gear, lwd = 2, pch = 19, cex = 1.5,\n  panel = function(x, y, ...) {\n    panel.errbars(x, y, beside = TRUE, ...)\n  }\n)\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-9-3.png)\u003c!-- --\u003e\n\n``` r\n# alternatively, means and error margins can be supplied directly. \n# In this case means are supplied as unique combinations\n# of y and x while error_margin is a separate vector with same length as y.\nmtcars_means \u003c- data.frame(\n  cyl = sort(unique(mtcars$cyl)),\n  mpg = with(mtcars, tapply(mpg, cyl, mean)),\n  stdev = with(mtcars, tapply(mpg, cyl, sd))\n)\n\n# you might have to adjust the yscale as it is determined from the\n# range of the y variable only, ignoring the extension through error bars.\nxyplot(mpg ~ factor(cyl), mtcars_means,\n  error_margin = mtcars_means$stdev,\n  ylim = c(9, 36), groups = cyl,\n  lwd = 2, pch = 19, cex = 1.5,\n  panel = function(x, y, ...) {\n    panel.errbars(x, y, ...)\n  }\n)\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-9-4.png)\u003c!-- --\u003e\n\n``` r\n# if you supply a two column matrix as the error_margin argument,\n# error bars with different lower and upper bounds can be drawn\nerror_mat \u003c- matrix(ncol = 2, 1:6)\n  xyplot(mpg ~ factor(cyl), mtcars_means,\n  error_margin = error_mat,\n  ylim = c(9, 36), groups = cyl,\n  lwd = 2, pch = 19, cex = 1.5,\n  panel = function(x, y, ...) {\n    panel.errbars(x, y, ...)\n  }\n)\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-9-5.png)\u003c!-- --\u003e\n\n### panel.geneplot\n\nPlot genes along a linear axis. This panel function allows to draw genes\nwith start and end coordinates as main input. Optional vectors for\n`gene_name` or `gene_strand` must have same length as `x`, `y`. This\nfunction supports paneling and grouping (e.g. by gene functional\ncategory) just as regular panel functions.\n\n``` r\nlibrary(lattice)\n\n# table with dummdy genetic loci\ngenes \u003c- data.frame(\n  gene_name = c(\"abc\", \"def\", \"ghi\", \"jkl\"),\n  gene_strand = c(\"+\", \"+\", \"+\", \"-\"),\n  gene_start = c(123, 178, 245, 310),\n  gene_end = c(167, 233, 297, 354)\n)\n \n# plot genes on a linear map\nxyplot(gene_end ~ gene_start, genes,\n  groups = gene_strand,\n  scales = list(y = list(draw = FALSE)),\n  xlim = c(80, 380), ylim = c(-3,2),\n  xlab = \"\", ylab = \"\",\n  gene_strand = genes[[\"gene_strand\"]],\n  gene_name = genes[[\"gene_name\"]],\n  panel = function(x, y, ...) {\n    panel.grid(h = -1, v = -1, col = grey(0.9))\n    panel.geneplot(x, y, arrows = TRUE, ...)\n  }\n)\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-10-1.png)\u003c!-- --\u003e\n\n``` r\n# same example with customized arrows\nxyplot(gene_end ~ gene_start, genes,\n  groups = gene_strand,\n  scales = list(y = list(draw = FALSE)),\n  xlim = c(80, 380), ylim = c(-3,2),\n  xlab = \"\", ylab = \"\",\n  gene_strand = genes[[\"gene_strand\"]],\n  gene_name = genes[[\"gene_name\"]],\n  panel = function(x, y, ...) {\n    panel.grid(h = -1, v = -1, col = grey(0.9))\n    panel.geneplot(x, y, arrows = TRUE, offset = 0.5,\n      height = 0.6, rot_labels = 0, tip = 3, col_labels = 1, ...)\n  }\n)\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-10-2.png)\u003c!-- --\u003e\n\n### panel.key\n\nDraw custom keys in lattice plots. This panel function allows to draw a\nkey (legend) inside a lattice panel, with more customization options\nthan the lattice default.\n\n``` r\nlibrary(lattice)\ndata(mtcars)\n\n# Two examples for a custom lattice key inside a panel.\n# The first takes all arguments from the \n# top-level plotting function.\n# The second has custom arguments.\nxyplot(mpg ~ 1/wt | factor(vs), mtcars,\n  groups = carb, pch = 19,\n  panel = function(x, y, ...) {\n    panel.xyplot(x, y, ...)\n    panel.key(...)\n    panel.key(labels = letters[1:5], which.panel = 2, \n      corner = c(0.9, 0.1), col = 1:5, pch = 1:5)\n  }\n)\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-11-1.png)\u003c!-- --\u003e\n\n### panel.lollipop\n\nMake a lollipop chart. This panel function creates lollipop charts. A\nlollipo chart is simply an XY-plot where the single points are supported\nby a horizontal or vertical line originating from one of the axes, or a\nspecified threshold.\n\n``` r\nlibrary(lattice)\ndata(mtcars)\nmtcars$car \u003c- rownames(mtcars)\n\nxyplot(mpg ~ factor(car, car[order(mpg)]), mtcars[1:10, ],\n  pch = 19, xlab = \"\", lwd = 2,\n  scales = list(x = list(rot = 35)),\n  panel = function(x, y, ...) {\n    panel.lollipop(x, y, ...)\n  }\n)\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-12-1.png)\u003c!-- --\u003e\n\n``` r\n# with grouping, and lollipops hanging from top\nxyplot(mpg ~ factor(car, car[order(mpg)]), mtcars[1:10, ],\n  groups = gear, pch = 19, xlab = \"\", lwd = 2,\n  scales = list(x = list(rot = 35)),\n  panel = function(x, y, ...) {\n    panel.lollipop(x, y, origin = \"top\", ...)\n  }\n)\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-12-2.png)\u003c!-- --\u003e\n\n### panel.piechart\n\nDraw pie and ring charts in lattice plots. This panel function allows to\ndraw pie charts (or rings) while still being able to use the typical\nlattice way of subsetting data. The function can be used within\n`xyplot()` but only one variable needs to be supplied (`x`). Grouping is\nsupported in the sense that the `x` variable is aggregated (summed up)\nover each unique group.\n\n``` r\nlibrary(grid)\nlibrary(lattice)\n\ndata(\"USMortality\")\n\n# A simple example using lattice paneling\nxyplot( ~ Rate | Sex, USMortality,\n  main = \"US mortality rates by sex\",\n  scales = list(draw = FALSE), cex = 0.7,\n  panel = function(x, ...) {\n    panel.piechart(x, ...)\n  }\n)\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-13-1.png)\u003c!-- --\u003e\n\n``` r\n# A more advanced example using grouping and\n# adjusting graphical parameters. The main variable \n# 'x' is now summed up for each value of 'groups'\nxyplot( ~ Rate | Sex, USMortality,\n  groups = gsub(\" \", \"\\n\", Cause), \n  col = heat.colors(10),\n  border = grey(0.3), cex = 0.7,\n  main = \"US mortality rates by sex\",\n  scales = list(draw = FALSE),\n  panel = function(x, ...) {\n    panel.piechart(x, diameter_sector = 0.1, ...)\n  }\n)\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-13-2.png)\u003c!-- --\u003e\n\n### panel.pvalue\n\nCalculate and draw p-values in lattice plots. This panel function allows\nto overlay p-values obtained from a statistical significance test on\nplots, together with “significance” symbols such as stars as it is often\nin encountered in the scientific literature. By default, the function\napplies Student’s (2-sided) `t.test` to each pair of a unique X variable\nand a standard. By default, the standard is the first unique value of X,\nbut can be specified manually.\n\n``` r\nlibrary(lattice)\ndata(mtcars)\n\n# p-values are calculated between groups of x, grouping is ignored\nxyplot(mpg ~ factor(cyl), mtcars, groups = cyl, pch = 19, cex = 0.7,\n  panel = function(x, y, ...) {\n    panel.stripplot(x, y, jitter.data = TRUE, \n      horizontal = FALSE, amount = 0.15, ...)\n    panel.pvalue(x, y, std = 1, symbol = TRUE,\n      pvalue = TRUE, offset = 6)\n})\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-14-1.png)\u003c!-- --\u003e\n\n### panel.quadrants\n\nDraw quadrants and quadrant statistics in lattice plots. This panel\nfunction allows to draw custom quadrants and display additional quadrant\nstatistics as often used in biomedical sciences. Grouping is ignored.\n\n``` r\nlibrary(lattice)\ndata(mtcars)\n\n# Default behavior for quadrants is to split x and y data\n# at the respective median, and plot percentage of points \n# per quandrant\nxyplot(mpg ~ 1/wt | factor(vs), mtcars,\n  groups = carb, pch = 19, cex = 1,\n  panel = function(x, y, ...) {\n    panel.xyplot(x, y, ...)\n    panel.quadrants(x, y, ...)\n  }\n)\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-15-1.png)\u003c!-- --\u003e\n\n### panel.repellabels\n\nDraw text labels for all points of a scatterplot using internal\nfunctionality from the `ggrepel` package. Note: an alternative panel\nfunction, `panel.directlabels`, carries most of the functionality of\nthis function, but uses `directlabels` to calculate label positions\ninstead. The same behavior can be achieved by using `panel.directlabels`\nwith “ggrepel” for the `positioning` argument.\n\nBy default, labels adapt the graphical parameters of the higher level\nplot, including coloring according to groups. However, many parameters\ncan be customized.\n\n``` r\nlibrary(grid)\nlibrary(lattice)\n\ndata(\"mtcars\")\nmtcars$car \u003c- rownames(mtcars)\n\n# A standard example using lattice grouping and paneling;\n# We can also draw boxes around labels and change label size\nxyplot(mpg ~ wt | factor(cyl), mtcars,\n  groups = cyl, pch = 19, labels = mtcars$car,\n  as.table = TRUE, layout = c(3, 1), cex = 0.6,\n  panel = function(x, y, ...) {\n    panel.xyplot(x, y, ...)\n    panel.repellabels(x, y, draw_box = TRUE, box_line = TRUE, ...)\n  }\n)\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-16-1.png)\u003c!-- --\u003e\n\n``` r\n# A similar plot with panels, but without grouping.\n# This requires explicit use of subscripts\nxyplot(mpg ~ wt | factor(cyl), mtcars,\n  pch = 19, labels = mtcars$car,\n  as.table = TRUE, layout = c(3, 1), cex = 0.6,\n  panel = function(x, y, subscripts, ...) {\n    panel.xyplot(x, y, ...)\n    panel.repellabels(x, y, subscripts = subscripts,\n      draw_box = TRUE, box_fill = \"white\", ...)\n  }\n)\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-16-2.png)\u003c!-- --\u003e\n\n``` r\n# An example without panels and more groups\nxyplot(mpg ~ wt, mtcars,\n  groups = hp, pch = 19,\n  labels = mtcars$wt, cex = 0.6,\n  panel = function(x, y, ...) {\n    panel.xyplot(x, y, ...)\n    panel.repellabels(x, y, draw_box = TRUE, box_line = TRUE, ...)\n  }\n)\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-16-3.png)\u003c!-- --\u003e\n\n### panel.symbols\n\nPlot a grouping variable encoded by symbols. This panel function allows\nto plot one additional grouping variable encoded by symbols (‘pch’)\ninstead of just the default grouping by color. Inspired by\n`panel.bubbleplot` from package `tactile`.\n\n``` r\nlibrary(lattice)\ndata(mtcars)\n\n# first grouping variable for colors, second for symbols (z)\nxyplot(mpg ~ factor(cyl), mtcars,\n  groups = gear, z = mtcars$cyl,\n  panel = function(x, y, z, ...) {\n    panel.symbols(x, y, z, ...)\n  }\n)\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-17-1.png)\u003c!-- --\u003e\n\n### panel.violinscatter\n\nCombine violin plot and scatter plot. This panel function combines\nviolin and scatter plot to a violin-shaped scatter plot. By default, the\nunderlying violin is plotted with the generic `panel.violin` function.\nOn top of that are the points drawn from which the violin was\ncalculated. To achieve this, each point is jittered randomly on the X\n(Y) axis according to the kernel density estimate of the Y (X) axis. All\nparameters that work for `panel.violin` can be used with this function\ntoo (e.g. to control the kernel density function). In addition to that,\nthis function supports grouping by color and is therefore more flexible\nthan the canonical `panel.violin`.\n\n``` r\n# use singer data from lattice\nlibrary(lattice)\ndata(singer)\nsinger$voice.part \u003c- gsub(\" [12]\", \"\", as.character(singer$voice.part))\nsinger$voice.part \u003c- factor(singer$voice.part, c(\"Soprano\", \"Alto\", \"Tenor\", \"Bass\"))\n\n# example with grouping\nxyplot(height ~ voice.part, singer, groups = voice.part,\n  horizontal = FALSE, pch = 19,\n  panel = function(x, y, ...) {\n    panel.violinscatter(x, y, ...)\n})\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-18-1.png)\u003c!-- --\u003e\n\n``` r\n# same plot but horizontal orientation\nxyplot(voice.part ~ height, singer, groups = voice.part,\n  horizontal = TRUE, pch = 19,\n  panel = function(x, y, ...) {\n    panel.violinscatter(x, y, ...)\n})\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-18-2.png)\u003c!-- --\u003e\n\n``` r\n# example with more and non-discrete data points\ndf \u003c- data.frame(\n  sample = factor(rep(c(\"A\", \"B\", \"C\"), each = 300)),\n  variable = c(rnorm(300, 0, 3), rnorm(300, 1, 2), rnorm(300, 3, 3))\n)\n\nxyplot(variable ~ sample, df,\n  horizontal = FALSE, pch = 19, cex = 0.4,\n  panel = function(x, y, ...) {\n    panel.violinscatter(x, y, ...)\n})\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-18-3.png)\u003c!-- --\u003e\n\n## Other functions\n\n### custom.ggplot\n\nCustom `ggplot2` like theme for lattice plots. Graphical parameters are\npassed down to `ggplot2like()`, which passes them down to\n`simpleTheme()`.\n\n``` r\nlibrary(lattice)\ndata(mtcars)\n\nxyplot(mpg ~ factor(carb) | gear, mtcars,\n  groups = carb, auto.key = list(columns = 3),\n  par.settings = custom.ggplot(),\n  panel = function(x, y, ...) {\n    panel.grid(h = -1, v = -1, col = \"white\")\n    panel.xyplot(x, y, ...)\n    panel.lmline(x, y, ...)\n  }\n)\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-19-1.png)\u003c!-- --\u003e\n\n### custom.lattice\n\nCustom grey theme for lattice plots. Some graphical parameters can be\npassed to override the defaults. Size parameters are relative.\n\n``` r\nlibrary(lattice)\ndata(mtcars)\n\nxyplot(mpg ~ factor(carb) | gear, mtcars,\n  groups = carb, auto.key = list(columns = 3),\n  par.settings = custom.lattice(),\n  panel = function(x, y, ...) {\n    panel.grid(h = -1, v = -1, col = grey(0.95))\n    panel.xyplot(x, y, ...)\n    panel.lmline(x, y, ...)\n  }\n)\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-20-1.png)\u003c!-- --\u003e\n\n### custom.colorblind\n\nColorblind-safe grey lattice theme. Colorblind-safe color scale with 7\ncolors was adapted from R color brewer, see\n`RColorBrewer::brewer.pal(8, \"Dark2\"))`. Color blind safe colors are\ndistinguishable for most common types of color blindness (deuterotopia,\ndeuteroanomaly), yet still look good for non color blind people. Some\ngraphical parameters can be passed to override the defaults. Size\nparameters are relative.\n\n``` r\nlibrary(lattice)\ndata(mtcars)\n\nxyplot(mpg ~ factor(carb) | gear, mtcars,\n  groups = carb, auto.key = list(columns = 3),\n  par.settings = custom.colorblind(),\n  panel = function(x, y, ...) {\n    panel.grid(h = -1, v = -1, col = grey(0.95))\n    panel.xyplot(x, y, ...)\n    panel.lmline(x, y, ...)\n  }\n)\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-21-1.png)\u003c!-- --\u003e\n\n### custom_splom\n\nCustom scatterplot matrix (SPLOM). A wrapper function around lattice\n‘splom’ with different upper and lower panels. A scatterplot matrix is a\ntiled plot where all variables of a data frame are plotted against each\nother.\n\n``` r\nlibrary(lattice)\ndata(mtcars)\n\n# Draw a scatterplot matrix of all variables of a \n# data frame against each other.\ncustom_splom(mtcars[1:5])\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-22-1.png)\u003c!-- --\u003e\n\n``` r\n# We can customize the scatterplot\ncustom_splom(\n  mtcars[1:5],\n  col_palette = c(\"#9FA2FF\", \"#F1F1F1\", \"#BAAE00\"),\n  pscales = 10,\n  xlab = \"data points\", ylab = \"regression\",\n  pch = 1, col = 1, cex = 0.6\n)\n```\n\n![](vignettes/README_files/figure-gfm/unnamed-chunk-22-2.png)\u003c!-- --\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fm-jahn%2Flattice-tools","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fm-jahn%2Flattice-tools","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fm-jahn%2Flattice-tools/lists"}