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
library(plotly)
## Warning: package 'plotly' was built under R version 4.2.3
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
Sys.setenv(MAPBOX_TOKEN = 11122223333444)
library(listviewer)
## Warning: package 'listviewer' was built under R version 4.2.3
library(mapview)
## Warning: package 'mapview' was built under R version 4.2.3
library(mapedit)
## Warning: package 'mapedit' was built under R version 4.2.3
library(mapdeck)
## Warning: package 'mapdeck' was built under R version 4.2.3
## 
## Attaching package: 'mapdeck'
## The following objects are masked from 'package:plotly':
## 
##     add_heatmap, add_mesh, add_sf, add_text
library(tmap)
## Warning: package 'tmap' was built under R version 4.2.3
library(leaflet)
## Warning: package 'leaflet' was built under R version 4.2.3
library(ggmap)
## Warning: package 'ggmap' was built under R version 4.2.3
## ℹ Google's Terms of Service: ]8;;https://mapsplatform.google.com<https://mapsplatform.google.com>]8;;
## ℹ Please cite ggmap if you use it! Use `citation("ggmap")` for details.
## 
## Attaching package: 'ggmap'
## The following object is masked from 'package:plotly':
## 
##     wind
library(rnaturalearth)
## Warning: package 'rnaturalearth' was built under R version 4.2.3
library("albersusa")
library(cartogram)
## Warning: package 'cartogram' was built under R version 4.2.3

Barras y Histogramas

pl <- plot_ly (diamonds, x = ~price) %>% 
  add_histogram (name = "plotly.js")

price_hist <- function (method = "FD") {

h <- hist(diamonds$price, breaks = method, plot = FALSE)

plot_ly(x = h$mids, y = h$counts) %>% add_bars (name = method)

}

subplot(

pl, price_hist(), price_hist("Sturges"), price_hist("Scott"),

nrows = 4, shareX = TRUE
)
library (dplyr)

p1 <- plot_ly(diamonds, x = ~cut) %>%

add_histogram()

p2 <- diamonds %>%
count (cut) %>%
plot_ly (x = ~cut, y = ~n) %>%

 add_bars()

subplot(p1, p2) %>% hide_legend()
### Distribuciones numéricas múltiples.
one_plot <- function(d) {

plot_ly (d, x = ~price) %>%

add_annotations (

~unique (clarity), x = 0.5, y = 1,

xref = "paper", yref = "paper", showarrow = FALSE
)
}

diamonds %>%

split(.$clarity) %>%

lapply (one_plot) %>%

subplot (nrows = 2, shareX =TRUE, titleX = FALSE) %>%

hide_legend ()
## 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
## 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
## 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
## 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
## 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
## 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
## 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
## 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 = ~clarity) %>%

add_histogram ()
# number of diamonds by cut and clarity 👎
cc <- count (diamonds, cut, clarity)

# number of diamonds by cut (nn)

cc2 <-left_join (cc, count (cc, cut, wt = n, name = 'nn'))
## Joining with `by = join_by(cut)`
cc2 %>%

mutate(prop = n / nn) %>%

plot_ly (x = ~cut, y = ~prop, color = ~clarity) %>%

add_bars() %>%

layout (barmode = "stack")
library(ggmosaic)
## Warning: package 'ggmosaic' was built under R version 4.2.3
p <- ggplot(data = cc) +
geom_mosaic (aes (weight = n, x = product (cut), fill= clarity))
ggplotly (p)
## Warning: `unite_()` was deprecated in tidyr 1.2.0.
## ℹ Please use `unite()` instead.
## ℹ The deprecated feature was likely used in the ggmosaic package.
##   Please report the issue at <]8;;https://github.com/haleyjeppson/ggmosaichttps://github.com/haleyjeppson/ggmosaic]8;;>.