0% found this document useful (0 votes)
81 views4 pages

If We Assign Variable Names

The document discusses how the plot_ly() function in R can be used to create visualizations by mapping data variables to visual properties like color, size, and shape. It provides examples of how plot_ly() automatically maps variables like diamond cut and clarity to properties that generate different chart types. The document also explains how arguments like color, stroke, and span in plot_ly() map variable values to visual ranges by default, but the I() function allows direct specification of visual range values when needed.

Uploaded by

Seshendra Vemuri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views4 pages

If We Assign Variable Names

The document discusses how the plot_ly() function in R can be used to create visualizations by mapping data variables to visual properties like color, size, and shape. It provides examples of how plot_ly() automatically maps variables like diamond cut and clarity to properties that generate different chart types. The document also explains how arguments like color, stroke, and span in plot_ly() map variable values to visual ranges by default, but the I() function allows direct specification of visual range values when needed.

Uploaded by

Seshendra Vemuri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

If we assign variable names (e.g.

, cut, clarity, etc) to visual properties


(e.g., x, y, color, etc) within plot_ly(), as done in Figure 2.1, it tries to find a
sensible geometric representation of that information for us. Shortly we’ll cover
how to specify these geometric representations (as well as other visual encodings)
to create different kinds of charts.
# create three visualizations of the diamonds dataset
plot_ly(diamonds, x = ~cut)
plot_ly(diamonds, x = ~cut, y = ~clarity)
plot_ly(diamonds, x = ~cut, color = ~clarity, colors = "Accent")
FIGURE 2.1: Three examples of visualizing categorical data with plot_ly(): (top)
mapping cut to x yields a bar chart, (middle) mapping cut & clarity to x & y yields
a heatmap, and (c) mapping cut & clarity to x & color yields a dodged bar chart.
The plot_ly() function has numerous arguments that are unique to the R package
(e.g., color, stroke, span, symbol, linetype, etc) and make it easier to encode data
variables (e.g., diamond clarity) as visual properties (e.g., color). By default, these
arguments map values of a data variable to a visual range defined by the plural
form of the argument. For example, in the bottom panel of 2.1, color is used to
map each level of diamond clarity to a different color, then colors is used to
specify the range of colors (which, in this case, the "Accent" color palette from
the RColorBrewer package, but one can also supply custom color codes or a
color palette function like colorRamp()). Figure 2.2 provides a visual diagram of
how this particular mapping works, but the same sort of idea can be applied to
other visual properties like size, shape, linetype, etc.

FIGURE 2.2: Mapping data values to a visual color range.

Since these arguments map data values to a visual range by default, you will
obtain unexpected results if you try to specify the visual range directly, as in the
top portion of Figure 2.3. If you want to specify the visual range directly, use
the I() function to declare this value to be taken ‘AsIs’, as in the bottom portion of
Figure 2.3. Throughout this book, you’ll see lots of examples that leverage these
arguments, especially in Chapter 3. Another good resource to learn more about
these arguments (especially their defaults) is the R documentation page available
by entering help(plot_ly) in your R console.
# doesn't produce black bars
plot_ly(diamonds, x = ~cut, color = "black")
# produces red bars with black outline
plot_ly(
diamonds,
x = ~cut,
color = I("red"),
stroke = I("black"),
span = I(2)
)

You might also like