This code will walk you through three examples of how to use RColorBrewer to color your Plotly graphs.
For the following examples load the RColorBrewer and plotly packages:
library(RColorBrewer)
library(plotly)
## Loading required package: RCurl
## Loading required package: bitops
## Loading required package: RJSONIO
## Loading required package: ggplot2
The first example uses the Paired ColorBrewer palette in a bar graph that displays the price of six meals.
First, enter the meal (x) and price (y) data for the six meals. Then define marker with the color brewer palette that you want to use; the syntax is brewer.pal(n_palette, “Palette_Name”), where n_palette is the number of colors from the palette that you want to use. In this example, we used six colors so that each meal is represented by a different color. The Paired palette presents a light shade of a color followed by a darker shade of that same hue. So in this example, breakfast 1 & 2 will be a similar hue with breakfast 1 in a light shade and breakfast 2 in a dark shade.
py <- plotly()
data_bar <- list(x = c("Breakfast 1", "Breakfast 2",
"Lunch 1", "Lunch 2",
"Dinner 1", "Dinner 2"),
y = c(7.72, 8.5, 12.22, 14.89, 27.02, 17.23),
type = "bar",
marker = list(color = brewer.pal(6, "Paired"))
)
Add labels and layout information,
layout_bar <- list(title = "Price of Meals",
xaxis = list(title = "Meal"),
yaxis = list(title = "Price ($)")
)
then send the data to Plotly!
response <- py$plotly(data = data_bar,
kwargs = list(layout = layout_bar,
filename = "Your_Filename",
fileopt = "overwrite")
)
This next example is a scatterplot (created from 100 random data points). Here we’ll use colorRampPalette() with the Spectral palette so each of the 100 points is a unique color. For a scatterplot, we want to set mode = “marker” and type = “scatter”. We then set the color with the marker variable. Set colorRampPalette(brewer.pal(n_palette, “palette_name”))(n_plot), where n_palette is the number of colors from the palette that you want to use and n_plot is the number of colors you want in your plot. For this example, we’ll use the Spectral palette. The Spectral palette has a max of 11 colors. We’ll use the full palette so n_palette = 11. We want each of the 100 points to be a unique color so n_plot = 100.
py <- plotly()
data_scat <- list(x = 1:100,
y = rnorm(100, mean = 1, sd = 5),
mode = "markers",
marker = list(color = colorRampPalette(brewer.pal(11,"Spectral"))(100),
size = 10,
line = list(width = 1)
),
type = "scatter"
)
Add any layout information and send to Plotly!
layout_scat <- list(title = "ROYGB Scatter",
plot_bgcolor = "black")
response <- py$plotly(data = data_scat,
kwargs = list(layout = layout_scat,
filename = "Your_Filename",
fileopt = "overwrite")
)
The final plot is an example of using RColoBrewer with ggplot syntax to make a plotly graph. We’ll make a line graph with one of the sequential palettes: Greens.
Create the data structure:
py <- plotly()
data_line <- data.frame(Quality = factor(c("Good", "Better", "Best",
"Good", "Better", "Best",
"Good", "Better", "Best",
"Good", "Better", "Best"),
levels = c("Good","Better","Best")
),
Items = factor(c("Item1", "Item2", "Item3", "Item4")),
Price = c(14.00, 17.23, 32.09,
11.80, 14.20, 39.99,
19.82, 18.20, 42.44,
14.99, 37.20, 58.0)
)
Create the graph with ggplot by adding the data structure then define the x variable, y variable, and color variable.
ggplot(data = data_line,
aes(x = Items,
y = Price,
group = Quality,
colour = Quality)) +
geom_line(size = 2) +
geom_point(size = 3) +
scale_colour_brewer(palette = "Purples")
Add layout information and send to Plotly!
layout_lines <- list(title = "Price and Quality of Clothing Items",
xaxis = list(title = "Clothing Items"),
yaxis = list(title = "Price ($)"))
py$ggplotly(kwargs = list(layout = layout_lines,
filename = "Your_Filename",
fileopt = "overwrite")
)