Black Lives Matter. Please consider donating to Black Girls Code today.

Aggregations in R

How to use aggregates in R with Plotly.


New to Plotly?

Plotly is a free and open-source graphing library for R. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.

Introduction

Aggregates are a type of transform that can be applied to values in a given expression. Available aggregations are:

Function Description
count Returns the quantity of items for each group.
sum Returns the summation of all numeric values.
avg Returns the average of all numeric values.
median Returns the median of all numeric values.
mode Returns the mode of all numeric values.
rms Returns the rms of all numeric values.
stddev Returns the standard deviation of all numeric values.
min Returns the minimum numeric value for each group.
max Returns the maximum numeric value for each group.
first Returns the first numeric value for each group.
last Returns the last numeric value for each group.

Basic Example

library(plotly)

fig <- plot_ly(
  type = 'scatter',
  x = diamonds$cut,
  y = diamonds$price,
  mode = 'markers',
  transforms = list(
    list(
      type = 'aggregate',
      groups = diamonds$cut,
      aggregations = list(
        list(
          target = 'y', func = 'sum', enabled = T
        )
      )
    )
  )
)

fig