All Projects → r-lib → Scales

r-lib / Scales

Licence: other
Tools for ggplot2 scales

Programming Languages

r
7636 projects

Labels

Projects that are alternatives of or similar to Scales

figpatch
Easily Arrange Images with Patchwork Alongside ggplot2 Figures.
Stars: ✭ 46 (-82.1%)
Mutual labels:  ggplot2
microbiomeViz
Visualize microbiome data with black magic ggtree
Stars: ✭ 55 (-78.6%)
Mutual labels:  ggplot2
30diasdegraficos
30 tipos de gráficos hechos con R, con datos y código reproducible.
Stars: ✭ 47 (-81.71%)
Mutual labels:  ggplot2
scclusteval
Single Cell Cluster Evaluation
Stars: ✭ 57 (-77.82%)
Mutual labels:  ggplot2
DataViz-Teaching
📈 Visualizations for DataViz Teaching
Stars: ✭ 29 (-88.72%)
Mutual labels:  ggplot2
learning R
List of resources for learning R
Stars: ✭ 32 (-87.55%)
Mutual labels:  ggplot2
dataviz
Course materials for Kieran Healy's rstudio::conf 2020 data visualization workshop
Stars: ✭ 75 (-70.82%)
Mutual labels:  ggplot2
pencil-scribbles
Create pencil effect drawings from pictures using R
Stars: ✭ 30 (-88.33%)
Mutual labels:  ggplot2
ggchicklet
🀫 Create Chicklet (Rounded Segmented Column) Charts
Stars: ✭ 130 (-49.42%)
Mutual labels:  ggplot2
Rstudio TableContest 2020
📺 Table showing an "Overview and Series Trends of the Best TV Shows on IMDb" – My Contribution to the Rstudio Table Contest 2020
Stars: ✭ 16 (-93.77%)
Mutual labels:  ggplot2
BuenColors
R package of colors for the Buenrostro Lab
Stars: ✭ 53 (-79.38%)
Mutual labels:  ggplot2
SWDchallenge
📈 My contributions to the #SWDchallenge
Stars: ✭ 20 (-92.22%)
Mutual labels:  ggplot2
ggChernoff
R package for drawing Chernoff faces in ggplot2
Stars: ✭ 28 (-89.11%)
Mutual labels:  ggplot2
ggbash
A simpler ggplot2 syntax, saving half of your typing.
Stars: ✭ 79 (-69.26%)
Mutual labels:  ggplot2
layer
Create stacked tilted maps
Stars: ✭ 97 (-62.26%)
Mutual labels:  ggplot2
cusumcharter
Easier CUSUM control charts. Returns simple CUSUM statistics, CUSUMs with control limit calculations, and function to generate faceted CUSUM Control Charts
Stars: ✭ 17 (-93.39%)
Mutual labels:  ggplot2
parcours-r
Valise pédagogique pour la formation à R
Stars: ✭ 25 (-90.27%)
Mutual labels:  ggplot2
jcolors
A set of color palettes I like (or can at least tolerate)
Stars: ✭ 24 (-90.66%)
Mutual labels:  ggplot2
flagfillr
Use flags as fills for ggplot2 maps. (WIP)
Stars: ✭ 23 (-91.05%)
Mutual labels:  ggplot2
rworkshops
Materials for R Workshops
Stars: ✭ 43 (-83.27%)
Mutual labels:  ggplot2

scales

CRAN status R build status Codecov test coverage

One of the most difficult parts of any graphics package is scaling, converting from data values to perceptual properties. The inverse of scaling, making guides (legends and axes) that can be used to read the graph, is often even harder! The scales packages provides the internal scaling infrastructure used by ggplot2, and gives you tools to override the default breaks, labels, transformations and palettes.

Installation

# Scales is installed when you install ggplot2 or the tidyverse.
# But you can install just scales from CRAN:
install.packages("scales")

# Or the development version from Github:
# install.packages("devtools")
devtools::install_github("r-lib/scales")

Usage

Breaks and labels

The most common use of the scales package is to customise to control the appearance of axis and legend labels. Use a break_ function to control how breaks are generated from the limits, and a label_ function to control how breaks are turned in to labels.

library(ggplot2)
library(dplyr, warn.conflicts = FALSE)
library(lubridate, warn.conflicts = FALSE)

txhousing %>% 
  mutate(date = make_date(year, month, 1)) %>% 
  group_by(city) %>% 
  filter(min(sales) > 5e2) %>% 
  ggplot(aes(date, sales, group = city)) + 
  geom_line(na.rm = TRUE) + 
  scale_x_date(
    NULL,
    breaks = scales::breaks_width("2 years"), 
    labels = scales::label_date("'%y")
  ) + 
  scale_y_log10(
    "Total sales",
    labels = scales::label_number_si()
  )


economics %>% 
  filter(date < ymd("1970-01-01")) %>% 
  ggplot(aes(date, pce)) + 
  geom_line() + 
  scale_x_date(NULL,
    breaks = scales::breaks_width("3 months"), 
    labels = scales::label_date_short()
  ) + 
  scale_y_continuous("Personal consumption expenditures",
    breaks = scales::breaks_extended(8),
    labels = scales::label_dollar()  
  )

Generally, I don’t recommend running library(scales) because when you type (e.g.) scales::label_ autocomplete will provide you with a list of labelling functions to job your memory.

Advanced features

Scales colour palettes are used to power the scales in ggplot2, but you can use them in any plotting system. The following example shows how you might apply them to a base plot.

library(scales)
# pull a list of colours from any palette
viridis_pal()(4)
#> [1] "#440154FF" "#31688EFF" "#35B779FF" "#FDE725FF"

# use in combination with baseR `palette()` to set new defaults
palette(brewer_pal(palette = "Set2")(4))
par(mar = c(5, 5, 1, 1))
plot(Sepal.Length ~ Sepal.Width, data = iris, col = Species, pch = 20)

scales also gives users the ability to define and apply their own custom transformation functions for repeated use.

# use trans_new to build a new transformation
logp3_trans <- trans_new(
  name = "logp",
  trans = function(x) log(x + 3),
  inverse = function(x) exp(x) - 3,
  breaks = log_breaks()
)

dsamp <- sample_n(diamonds, 100)
ggplot(dsamp, aes(carat, price, colour = color)) +
  geom_point() + 
  scale_y_continuous(trans = logp3_trans)

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].