Chapter 2 Basic Charts
2.1 Scatter Plots
2.1.1 Basic Scatter Plot
library(plotly)
<- plot_ly(data = mtcars, x = ~hp, y= ~mpg, type = "scatter", mode = "markers")
fig fig
2.1.2 Styled Scatter Plots
library(plotly)
<- plot_ly(data = mtcars, x = ~hp, y= ~mpg, type = "scatter",
fig mode = "markers",
marker = list(size = 10,
color = 'rgba(255, 182, 193, .9)',
line = list(color = 'rgba(152, 0, 0, .8)',
width = 2)))
<- fig %>% layout(title = 'Styled Scatter',
fig yaxis = list(zeroline = FALSE),
xaxis = list(zeroline = FALSE))
fig
2.2 Line Plots
2.2.1 Basic Line Plot
library(plotly)
<- c(1:100)
x <- rnorm(100, mean = 0)
random_y <- data.frame(x, random_y)
data
<- plot_ly(data, x = ~x, y = ~random_y, type = 'scatter', mode = 'lines')
fig
fig
2.2.2 Line Plots Mode
library(plotly)
<- rnorm(100, mean = 5)
trace_0 <- rnorm(100, mean = 0)
trace_1 <- rnorm(100, mean = -5)
trace_2 <- c(1:100)
x
<- data.frame(x, trace_0, trace_1, trace_2)
data
<- plot_ly(data, x = ~x, y = ~trace_0, name = 'trace 0', type = 'scatter', mode = 'lines')
fig <- fig %>% add_trace(y = ~trace_1, name = 'trace 1', mode = 'lines+markers')
fig <- fig %>% add_trace(y = ~trace_2, name = 'trace 2', mode = 'markers')
fig
fig
2.2.3 Density Plot
library(plotly)
<- with(diamonds, tapply(price, INDEX = cut, density))
dens <- data.frame(
df x = unlist(lapply(dens, "[[", "x")),
y = unlist(lapply(dens, "[[", "y")),
cut = rep(names(dens), each = length(dens[[1]]$x))
)
<- plot_ly(df, x = ~x, y = ~y, color = ~cut)
fig <- fig %>% add_lines()
fig
fig
2.3 Bar Charts
2.3.1 Basic Bar Chart
library(plotly)
<- plot_ly(
fig x = c("giraffes", "orangutans", "monkeys"),
y = c(20, 14, 23),
name = "SF Zoo",
type = "bar"
)
fig
2.3.2 Grouped Bar Chart
library(plotly)
<- c("giraffes", "orangutans", "monkeys")
Animals <- c(20, 14, 23)
SF_Zoo <- c(12, 18, 29)
LA_Zoo <- data.frame(Animals, SF_Zoo, LA_Zoo)
data
<- plot_ly(data, x = ~Animals, y = ~SF_Zoo, type = 'bar', name = 'SF Zoo')
fig <- fig %>% add_trace(y = ~LA_Zoo, name = 'LA Zoo')
fig <- fig %>% layout(yaxis = list(title = 'Count'), barmode = 'group')
fig
fig
2.3.3 Stacked Bar Chart
library(plotly)
<- c("giraffes", "orangutans", "monkeys")
Animals <- c(20, 14, 23)
SF_Zoo <- c(12, 18, 29)
LA_Zoo <- data.frame(Animals, SF_Zoo, LA_Zoo)
data
<- plot_ly(data, x = ~Animals, y = ~SF_Zoo, type = 'bar', name = 'SF Zoo')
fig <- fig %>% add_trace(y = ~LA_Zoo, name = 'LA Zoo')
fig <- fig %>% layout(yaxis = list(title = 'Count'), barmode = 'stack')
fig
fig
2.3.4 Colored and Styled Bar Chart
library(plotly)
<- c(1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012)
x <- c(219, 146, 112, 127, 124, 180, 236, 207, 236, 263, 350, 430, 474, 526, 488, 537, 500, 439)
roW <- c(16, 13, 10, 11, 28, 37, 43, 55, 56, 88, 105, 156, 270, 299, 340, 403, 549, 499)
China <- data.frame(x, roW, China)
data
<- plot_ly(data, x = ~x, y = ~roW, type = 'bar', name = 'Rest of the World',
fig marker = list(color = 'rgb(55, 83, 109)'))
<- fig %>% add_trace(y = ~China, name = 'China', marker = list(color = 'rgb(26, 118, 255)'))
fig <- fig %>% layout(title = 'US Export of Plastic Scrap',
fig xaxis = list(
title = "",
tickfont = list(
size = 14,
color = 'rgb(107, 107, 107)')),
yaxis = list(
title = 'USD (millions)',
titlefont = list(
size = 16,
color = 'rgb(107, 107, 107)'),
tickfont = list(
size = 14,
color = 'rgb(107, 107, 107)')),
legend = list(x = 0, y = 1, bgcolor = 'rgba(255, 255, 255, 0)', bordercolor = 'rgba(255, 255, 255, 0)'),
barmode = 'group', bargap = 0.15, bargroupgap = 0.1)
fig
2.4 Pie Charts
2.4.1 Basic Pie Chart
library(plotly)
<- data.frame("Categorie"=rownames(USPersonalExpenditure), USPersonalExpenditure)
USPersonalExpenditure <- USPersonalExpenditure[,c('Categorie', 'X1960')]
data
<- plot_ly(data, labels = ~Categorie, values = ~X1960, type = 'pie')
fig <- fig %>% layout(title = 'United States Personal Expenditures by Categories in 1960',
fig xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
fig
2.4.2 Donut Chart
library(plotly)
library(dplyr)
# Get Manufacturer
$manuf <- sapply(strsplit(rownames(mtcars), " "), "[[", 1)
mtcars
<- mtcars
df <- df %>% group_by(manuf)
df <- df %>% summarize(count = n())
df <- df %>% plot_ly(labels = ~manuf, values = ~count)
fig <- fig %>% add_pie(hole = 0.6)
fig <- fig %>% layout(title = "Donut charts using Plotly", showlegend = F,
fig xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
fig
2.5 Bubble Charts
2.5.1 Simple Bubble Chart
library(plotly)
<- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/school_earnings.csv")
data
<- plot_ly(data, x = ~Women, y = ~Men, text = ~School, type = 'scatter', mode = 'markers',
fig marker = list(size = ~Gap, opacity = 0.5))
<- fig %>% layout(title = 'Gender Gap in Earnings per University',
fig xaxis = list(showgrid = FALSE),
yaxis = list(showgrid = FALSE))
fig
2.5.2 Mapping a Color Variable (Continuous)
library(plotly)
<- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/school_earnings.csv")
data
<- plot_ly(data, x = ~Women, y = ~Men, text = ~School, type = 'scatter', mode = 'markers', color = ~Gap, colors = 'Reds',
fig marker = list(size = ~Gap, opacity = 0.5))
<- fig %>% layout(title = 'Gender Gap in Earnings per University',
fig xaxis = list(showgrid = FALSE),
yaxis = list(showgrid = FALSE))
fig
2.5.3 Mapping a Color Variable (Categorical)
library(plotly)
<- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/school_earnings.csv")
data
$State <- as.factor(c('Massachusetts', 'California', 'Massachusetts', 'Pennsylvania', 'New Jersey', 'Illinois', 'Washington DC',
data'Massachusetts', 'Connecticut', 'New York', 'North Carolina', 'New Hampshire', 'New York', 'Indiana',
'New York', 'Michigan', 'Rhode Island', 'California', 'Georgia', 'California', 'California'))
<- plot_ly(data, x = ~Women, y = ~Men, text = ~School, type = 'scatter', mode = 'markers', size = ~Gap, color = ~State, colors = 'Paired',
fig marker = list(opacity = 0.5, sizemode = 'diameter'))
<- fig %>% layout(title = 'Gender Gap in Earnings per University',
fig xaxis = list(showgrid = FALSE),
yaxis = list(showgrid = FALSE),
showlegend = FALSE)
fig
2.5.4 Styled Buble Chart
library(plotly)
<- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv")
data
<- data[which(data$year == 2007),]
data_2007 <- data_2007[order(data_2007$continent, data_2007$country),]
data_2007 <- 2.666051223553066e-05
slope $size <- sqrt(data_2007$pop * slope)
data_2007<- c('#4AC6B7', '#1972A4', '#965F8A', '#FF7070', '#C61951')
colors
<- plot_ly(data_2007, x = ~gdpPercap, y = ~lifeExp, color = ~continent, size = ~size, colors = colors,
fig type = 'scatter', mode = 'markers', sizes = c(min(data_2007$size), max(data_2007$size)),
marker = list(symbol = 'circle', sizemode = 'diameter',
line = list(width = 2, color = '#FFFFFF')),
text = ~paste('Country:', country, '<br>Life Expectancy:', lifeExp, '<br>GDP:', gdpPercap,
'<br>Pop.:', pop))
<- fig %>% layout(title = 'Life Expectancy v. Per Capita GDP, 2007',
fig xaxis = list(title = 'GDP per capita (2000 dollars)',
gridcolor = 'rgb(255, 255, 255)',
range = c(2.003297660701705, 5.191505530708712),
type = 'log',
zerolinewidth = 1,
ticklen = 5,
gridwidth = 2),
yaxis = list(title = 'Life Expectancy (years)',
gridcolor = 'rgb(255, 255, 255)',
range = c(36.12621671352166, 91.72921793264332),
zerolinewidth = 1,
ticklen = 5,
gridwith = 2),
paper_bgcolor = 'rgb(243, 243, 243)',
plot_bgcolor = 'rgb(243, 243, 243)')
fig
2.6 Sankey Diagram
2.6.1 Basic Sankey Diagram
library(plotly)
<- plot_ly(
fig type = "sankey",
orientation = "h",
node = list(
label = c("A1", "A2", "B1", "B2", "C1", "C2"),
color = c("blue", "blue", "blue", "blue", "blue", "blue"),
pad = 15,
thickness = 20,
line = list(
color = "black",
width = 0.5
)
),
link = list(
source = c(0,1,0,2,3,3),
target = c(2,3,3,4,4,5),
value = c(8,4,2,8,4,2)
)
)<- fig %>% layout(
fig title = "Basic Sankey Diagram",
font = list(
size = 10
)
)
fig
2.6.2 Style Sankey Diagram
library(plotly)
library(rjson)
<- "https://raw.githubusercontent.com/plotly/plotly.js/master/test/image/mocks/sankey_energy_dark.json"
json_file <- fromJSON(paste(readLines(json_file), collapse=""))
json_data
<- plot_ly(
fig type = "sankey",
domain = list(
x = c(0,1),
y = c(0,1)
),orientation = "h",
valueformat = ".0f",
valuesuffix = "TWh",
node = list(
label = json_data$data[[1]]$node$label,
color = json_data$data[[1]]$node$color,
pad = 15,
thickness = 15,
line = list(
color = "black",
width = 0.5
)
),
link = list(
source = json_data$data[[1]]$link$source,
target = json_data$data[[1]]$link$target,
value = json_data$data[[1]]$link$value,
label = json_data$data[[1]]$link$label
)
)<- fig %>% layout(
fig title = "Energy forecast for 2050<br>Source: Department of Energy & Climate Change, Tom Counsell via <a href='https://bost.ocks.org/mike/sankey/'>Mike Bostock</a>",
font = list(
size = 10,
color = 'white'
),xaxis = list(showgrid = F, zeroline = F, showticklabels = F),
yaxis = list(showgrid = F, zeroline = F, showticklabels = F),
plot_bgcolor = 'black',
paper_bgcolor = 'black'
)
fig