library(plotly)
## 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
library(ggplot2)
# Gráfica de precio de diamantes usando plotly
p1 <- 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(
  p1,            # Gráfica de plotly
  price_hist(),  # Histograma con método "FD"
  price_hist("Sturges"),  # Histograma con método "Sturges"
  price_hist("Scott"),    # Histograma con método "Scott"
  nrows = 4, shareX = TRUE
)
# Función para crear un plot_ly con anotaciones para cada claridad
one_plot <- function(d) {
  plot_ly(d, x = ~price) %>%
    add_annotations(
      ~unique(clarity), x = 0.5, y = 1, 
      xref = "paper", yref = "paper", showarrow = FALSE
    )
}

# Dividir el conjunto de datos de diamantes por claridad y aplicar la función one_plot
plots_list <- diamonds %>%
  split(.$clarity) %>%
  lapply(one_plot)

# Crear subplot con las gráficas generadas
subplot(
  plots_list,  # Lista de gráficas generadas
  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
library(ggplot2)
library(plotly)
# Crear gráfico de barras con ggplot2
ggplot(diamonds, aes(x = cut, fill = clarity)) +
  geom_bar() +
  ggtitle("Frecuencia de claridad por tipo de corte") +
  xlab("Corte") +
  ylab("Frecuencia") +
  theme_minimal()

# Crear gráfico interactivo con plotly
plot_ly(diamonds, x = ~cut, color = ~clarity) %>%
  add_histogram()
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(ggplot2)
library(plotly)
# Número de diamantes por corte y claridad (n)
cc <- count(diamonds, cut, clarity)
# Número de diamantes por corte (nn)
cc2 <- left_join(cc, count(cc, cut, wt = n, name = 'nn'))
## Joining with `by = join_by(cut)`
# Calcular proporción
cc2 <- cc2 %>%
  mutate(prop = n / nn)

# Gráfico interactivo con plotly
plot_ly(cc2, x = ~cut, y = ~prop, color = ~clarity) %>%
  add_bars() %>%
  layout(barmode = "stack")
library(dplyr)
library(ggplot2)
library(ggmosaic)  # Agregar esta línea para cargar ggmosaic

# Número de diamantes por corte y claridad (n)
cc <- count(diamonds, cut, clarity)

# Crear gráfico de mosaico con ggplot2
p <- ggplot(data = cc) +
  geom_mosaic(aes(weight = n, x = product(cut), fill = clarity))

# Convertir gráfico a plotly
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 <https://github.com/haleyjeppson/ggmosaic>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.