# Load the plotly R package
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
# Load the diamonds dataset from the ggplot2 package
data(diamonds, package = "ggplot2")
# Now you can use the diamonds dataset for plotting with plotly
diamonds
## # A tibble: 53,940 × 10
## carat cut color clarity depth table price x y z
## <dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
## 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
## 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
## 4 0.29 Premium I VS2 62.4 58 334 4.2 4.23 2.63
## 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75
## 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48
## 7 0.24 Very Good I VVS1 62.3 57 336 3.95 3.98 2.47
## 8 0.26 Very Good H SI1 61.9 55 337 4.07 4.11 2.53
## 9 0.22 Fair E VS2 65.1 61 337 3.87 3.78 2.49
## 10 0.23 Very Good H VS1 59.4 61 338 4 4.05 2.39
## # ℹ 53,930 more rows
library(plotly)
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
p <- plot_ly(diamonds, y = ~price, color = I("black"), alpha = 0.1, boxpoints = "suspectedoutliers")
p1 <- p %>% add_boxplot(x="Overall")
p2 <- p %>% add_boxplot(x=~cut)
subplot(p1, p2, shareY = TRUE, widths = c(0.2, 0.8), margin = 0) %>%
hide_legend()
p <- plot_ly(diamonds, x= ~log(carat), y= ~log(price))
subplot(
add_histogram2d(p) %>%
colorbar(title = "default") %>%
layout(xaxis=list(title="default")))
subplot(
add_histogram2d(p, zsmooth = "best") %>%
colorbar(title = "zsmooth") %>%
layout(xaxis = list(title = "zsmooth")),
add_histogram2d(p, nbinsx = 60, nbinsy = 60) %>%
colorbar(title = "nbins") %>%
layout(xaxis = list(title = "nbins")),
shareY = TRUE, titleX = TRUE
)
library(plotly)
library(MASS)
##
## Attaching package: 'MASS'
## The following object is masked from 'package:dplyr':
##
## select
## The following object is masked from 'package:plotly':
##
## select
kde_count <- function(x, y, ...) {
kde <- MASS::kde2d(x, y, ...)
df <- with(kde, setNames(expand.grid(x, y), c("x", "y")))
df$count <- with(kde, c(z) * length(x) * diff(x)[1] * diff(y)[1])
return(data.frame(df))
}
kd <- with(diamonds, kde_count(log(carat), log(price), n = 30))
plot_ly(kd, x = ~x, y = ~y, z = ~count) %>%
add_heatmap() %>%
colorbar(title = "Number of diamonds")
corr <- cor(dplyr::select_if(diamonds, is.numeric))
plot_ly(colors = "RdBu") %>%
add_heatmap(x = rownames(corr), y = colnames(corr), z = corr) %>%
colorbar(limits = c(-1, 1))
plot_ly(mpg, x = ~cty, y = ~hwy, z = ~cyl) %>%
add_markers(color = ~cyl)
plot_ly(mpg, x = ~cty, y = ~hwy, z = ~cyl) %>%
add_paths(color = ~displ)
plot_ly(mpg, x = ~cty, y = ~hwy, z = ~cyl) %>%
add_lines(color = ~displ)
mpg %>%
plot_ly(x = ~cty, y = ~hwy, z = ~cyl) %>%
group_by(cyl) %>%
add_lines(color = ~displ)
plot_ly(mpg, x = ~cty, y = ~hwy, z = ~cyl) %>%
add_lines(color = ~displ) %>%
layout(
scene = list(
xaxis = list(title = "MPG city"),
yaxis = list(title = "MPG highway"),
zaxis = list(title = "Number of cylinders")
)
)
x <- seq_len(nrow(volcano)) + 100
y <- seq_len(ncol(volcano)) + 500
plot_ly() %>%
add_surface(x = x, y = y, z = ~volcano)