Riya Sutaria
30 September 2020
In this presentation I’ll show some plots formed with plotly. For this i will be using the iris ans the EuStockMarkets data set.
#Loading the required data set and libraries.
suppressPackageStartupMessages(library(plotly))
suppressPackageStartupMessages(library(tidyr))
suppressPackageStartupMessages(library(dplyr))
data("iris")
data("EuStockMarkets")
data("diamonds")plot_ly(data = iris, x = ~Sepal.Length, y = ~Sepal.Width,
mode = "markers", type = "scatter")plot_ly(data = iris, x = ~Sepal.Length, y = ~Sepal.Width,
color = ~as.factor(Species), mode = "markers",
marker = list(size = ~Petal.Length*3), type = "scatter")plot_ly(data = iris, x = ~Sepal.Length, y = ~Sepal.Width, z=~Petal.Width, color =
~as.factor(Species),mode = "markers", marker = list(size=~Petal.Length*3),
type = "scatter3d")#click and move with your cursor to view in different angles.#Converting the data into a data frame as plotly only takes data frame as data input.
EuStockMarket <- as.data.frame(EuStockMarkets)
plot_ly(data = EuStockMarket, x = ~time(EuStockMarkets),
y = ~DAX, mode = "line")## No trace type specified:
## Based on info supplied, a 'scatter' trace seems appropriate.
## Read more about this trace type -> https://plot.ly/r/reference/#scatter
#we'll mix up all the prices into one data frame to plot a multiline plot.
EuStockMarket <- EuStockMarket %>% gather(index, price) %>% mutate(time = rep(time(EuStockMarkets),4))
plot_ly(data = EuStockMarket, x = ~time, y = ~price,
color = ~index, mode = "line")## No trace type specified:
## Based on info supplied, a 'scatter' trace seems appropriate.
## Read more about this trace type -> https://plot.ly/r/reference/#scatter
plot_ly(data = iris, x = ~Sepal.Width, type = "histogram", color = "black", colors = "000001", alpha = 0.5)plot_ly(data = iris, y = ~Petal.Length, color = ~Species, type = "box")mat <- matrix(rnorm(100*100), nrow = 100, ncol = 100)
plot_ly(z = ~mat, type = "heatmap")#click and move with your cursor to view in different angles.
mat <- matrix(rnorm(100*100), nrow = 100, ncol = 100)
plot_ly(z = ~mat, type = "surface")#click and move with your cursor to view in different angles.
mat <- matrix(sort(rnorm(100*100)), nrow = 100, ncol = 100)
plot_ly(z = ~mat, type = "surface")g <- ggplot(data = diamonds, aes(x = carat , y = price)) + geom_point(aes(text = paste("Clarity :", clarity)), size = 4) + geom_smooth(aes(color = cut, fill = cut)) + facet_wrap(~cut)## Warning: Ignoring unknown aesthetics: text
g## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
g <- ggplot(data = diamonds, aes(x = carat , y = price)) + geom_point(aes(text = paste("Clarity :", clarity)), size = 4) + geom_smooth(aes(color = cut, fill = cut)) + facet_wrap(~cut)## Warning: Ignoring unknown aesthetics: text
gg <- ggplotly(g)## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
gg