# create three visualizations of the diamonds dataset
plot_ly(diamonds, x = ~cut)
## No trace type specified:
##   Based on info supplied, a 'histogram' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#histogram
plot_ly(diamonds, x = ~cut, y = ~clarity)
## No trace type specified:
##   Based on info supplied, a 'histogram2d' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#histogram2d
plot_ly(diamonds, x = ~cut, color = ~clarity, colors = "Accent")
## No trace type specified:
##   Based on info supplied, a 'histogram' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#histogram
plot_ly(diamonds, x = ~cut, color = "black")
## No trace type specified:
##   Based on info supplied, a 'histogram' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#histogram
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels

## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
plot_ly(
diamonds,
x = ~cut,
color = I("red"),
stroke = I("black"),
span = I(2)
)
## No trace type specified:
##   Based on info supplied, a 'histogram' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#histogram
layout(
plot_ly(diamonds, x = ~cut),
title = "My beatiful histogram" )
## No trace type specified:
##   Based on info supplied, a 'histogram' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#histogram
diamonds %>%
plot_ly(x = ~cut) %>%
layout(title = "My beatiful histogram")
## No trace type specified:
##   Based on info supplied, a 'histogram' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#histogram
diamonds %>%
plot_ly() %>%
add_histogram(x = ~cut)
diamonds %>%
dplyr::count(cut) %>%
plot_ly() %>%
add_bars(x = ~cut, y = ~n)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
diamonds %>% plot_ly(x = ~cut) %>% add_histogram() %>% group_by(cut) %>% summarise(n = n()) %>% add_text(
text = ~scales::comma(n), y = ~n,
textposition = "top middle",
cliponaxis = FALSE
)
diamonds %>%
plot_ly(x = ~cut) %>%
add_histogram() %>%
group_by(cut) %>%
summarise(n = n()) %>%
plotly_data()
## # A tibble: 5 × 2
##   cut           n
##   <ord>     <int>
## 1 Fair       1610
## 2 Good       4906
## 3 Very Good 12082
## 4 Premium   13791
## 5 Ideal     21551
#p <- plot_ly(diamonds, x = ~cut, color = ~clarity, colors = "Accent")
#plotly_json(p)
p <- ggplot(diamonds, aes(x = log(price), color = clarity)) +
geom_freqpoly()
ggplotly(p)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
p <-ggplot(diamonds, aes(x = log(price), color = clarity)) + geom_freqpoly(stat = "density") + facet_wrap(~cut)
ggplotly(p)
library(plotly)
data(economics, package = "ggplot2")
# sort economics by psavert, just to # show difference between paths and lines
p <-economics %>%
arrange(psavert) %>%
plot_ly(x = ~date, y = ~psavert)
add_paths(p) 
add_lines(p)
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
econ <- economics %>%
    mutate(yr = year(date), mnth = month(date))
# One trace (more performant, but less interactive)
econ %>% group_by(yr) %>% plot_ly(x = ~mnth, y = ~uempmed) %>% add_lines(text = ~yr)
# Multiple traces (less performant, but more interactive)
plot_ly(econ, x = ~mnth, y = ~uempmed) %>% add_lines(color = ~ordered(yr))
# The split argument guarantees one trace per group level (regardless # of the variable type). This is useful if you want a consistent # visual property over multiple traces # plot_ly(econ, x = ~mnth, y = ~uempmed) %>% # add_lines(split = ~yr, color = I("black"))
set.seed(99) 
plot_ly() %>% add_trace(
type = "scatter",
mode = "markers+lines+text",
x=4:6,
y=4:6,
text = replicate(3, praise::praise("You are ${adjective}! ")),
textposition = "right",
hoverinfo = "text",
textfont = list(family = "Roboto Condensed", size = 16)
) %>%
layout(xaxis = list(range = c(3, 8)))
subplot(
plot_ly(mpg, x = ~cty, y = ~hwy, name = "default"),
plot_ly(mpg, x = ~cty, y = ~hwy) %>%
add_markers(alpha = 0.2, name = "alpha")
)
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
p <- plot_ly(mpg, x = ~cty, y = ~hwy, alpha = 0.5)
subplot(
add_markers(p, color = ~cyl, showlegend = FALSE) %>%
colorbar(title = "Viridis"),
add_markers(p, color = ~factor(cyl))
)