File: add_legend.Rd

package info (click to toggle)
r-cran-ggvis 0.4.7%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 1,716 kB
  • sloc: javascript: 7,373; sh: 25; makefile: 2
file content (120 lines) | stat: -rw-r--r-- 3,738 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/guide_legend.R
\name{add_legend}
\alias{add_legend}
\alias{hide_legend}
\title{Add a vega legend specification to a ggvis plot}
\usage{
add_legend(
  vis,
  scales = NULL,
  orient = "right",
  title = NULL,
  format = NULL,
  values = NULL,
  properties = NULL
)

hide_legend(vis, scales)
}
\arguments{
\item{vis}{A ggvis object.}

\item{scales}{The name of one or more scales for which to add a legend.
Typically one of "size", "shape", "fill", "stroke", although custom scale
names may also be used. Multiple names can also be used, like
\code{c("fill", "shape")}.}

\item{orient}{The orientation of the legend. One of "left" or "right". This
determines how the legend is positioned within the scene. The default is
"right".}

\item{title}{A title for the legend. By default, it uses the name the fields
used in the legend. Use \code{""} to suppress the title.}

\item{format}{The formatting pattern for axis labels. Vega uses D3's format
pattern.}

\item{values}{Explicitly set the visible legend values.}

\item{properties}{Optional mark property definitions for custom legend
styling. Should be an object created by \code{\link{legend_props}}, with
properties for title, label, symbols, gradient, legend.}
}
\description{
Axis specifications allow you to either override the default legends,
or supply additional legends.
}
\details{
More information about axes can be found in the "axes and legends" vignettes.
}
\section{Compared to ggplot2}{


In ggplot2, legend (and axis) properties are part of the scales
specification. In vega, they are separate, which allows the specification
of multiple legends, and more flexible linkage between scales and legends.
}

\examples{
mtcars \%>\% ggvis(x = ~wt, y = ~mpg, fill = ~cyl) \%>\%
  layer_points() \%>\%
  add_legend("fill", title = "Cylinders")

# Suppress legend with hide_legend
mtcars \%>\% ggvis(x = ~wt, y = ~mpg, fill = ~cyl) \%>\%
  layer_points() \%>\%
  hide_legend("fill")

# Combining two properties in one legend
mtcars \%>\%
  ggvis(x = ~wt, y = ~mpg, fill = ~factor(cyl), shape = ~factor(cyl)) \%>\%
  layer_points() \%>\%
  add_legend(c("fill", "shape"))

# Control legend properties with a continuous legend, with x and y position
# in pixels.
mtcars \%>\% ggvis(x = ~wt, y = ~mpg, fill = ~cyl) \%>\%
  layer_points() \%>\%
  add_legend("fill", title = "Cylinders",
    properties = legend_props(
      title = list(fontSize = 16),
      labels = list(fontSize = 12, fill = "#00F"),
      gradient = list(stroke = "red", strokeWidth = 2),
      legend = list(x = 500, y = 50)
    )
  )

# Control legend properties with a categorical legend, with x and y position
# in the scaled data space.
mtcars \%>\% ggvis(x = ~wt, y = ~mpg, fill = ~factor(cyl)) \%>\%
  layer_points() \%>\%
  add_legend("fill", title = "Cylinders",
    properties = legend_props(
      title = list(fontSize = 16),
      labels = list(fontSize = 14, dx = 5),
      symbol = list(stroke = "black", strokeWidth = 2,
        shape = "square", size = 200),
      legend = list(
        x = scaled_value("x", 4.5),
        y = scaled_value("y", 30)
      )
    )
  )

# Control legend position using x_rel and y_rel which specify relative
# position, going from 0 to 1. (0, 0) is the bottom-left corner, and
# (1, 1) is the upper-right corner. The values control the position of
# the upper-left corner of the legend.
mtcars \%>\% ggvis(x = ~wt, y = ~mpg, fill = ~cyl) \%>\%
  layer_points() \%>\%
  add_relative_scales() \%>\%
  add_legend("fill", title = "Cylinders",
    properties = legend_props(
      legend = list(
        x = scaled_value("x_rel", 0.8),
        y = scaled_value("y_rel", 1)
      )
    )
  )
}