Scatter and Bubble Plots
library(plotly)
<- plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length)
fig fig
library(ggplot2)
library(plotly)
library(gapminder)
<- gapminder %>%
p filter(year==1977) %>%
ggplot( aes(gdpPercap, lifeExp, size = pop, color=continent)) +
geom_point() +
theme_bw()
ggplotly(p)
Heatmaps
# Libraries
#devtools::install_github("talgalili/d3heatmap")
library(tidyverse)
library(hrbrthemes)
library(viridis)
library(plotly)
library(d3heatmap)
# Load data
<- read.table("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/multivariate.csv", header=T, sep=";")
data colnames(data) <- gsub("\\.", " ", colnames(data))
# Select a few country
<- data %>%
data filter(Country %in% c("France", "Sweden", "Italy", "Spain", "England", "Portugal", "Greece", "Peru", "Chile", "Brazil", "Argentina", "Bolivia", "Venezuela", "Australia", "New Zealand", "Fiji", "China", "India", "Thailand", "Afghanistan", "Bangladesh", "United States of America", "Canada", "Burundi", "Angola", "Kenya", "Togo")) %>%
arrange(Country) %>%
mutate(Country = factor(Country, Country))
# Matrix format
<- data
mat rownames(mat) <- mat[,1]
<- mat %>% dplyr::select(-Country, -Group, -Continent)
mat <- as.matrix(mat)
mat
# Heatmap
#d3heatmap(mat, scale="column", dendrogram = "none", width="800px", height="80Opx", colors = "Blues")
library(heatmaply)
<- heatmaply(mat,
p dendrogram = "none",
xlab = "", ylab = "",
main = "",
scale = "column",
margins = c(60,100,40,20),
grid_color = "white",
grid_width = 0.00001,
titleX = FALSE,
hide_colorbar = TRUE,
branches_lwd = 0.1,
label_names = c("Country", "Feature:", "Value"),
fontsize_row = 5, fontsize_col = 5,
labCol = colnames(mat),
labRow = rownames(mat),
heatmap_layers = theme(axis.line=element_blank())
) p
Kita dapat pula menampilkan dendogram dari amatan dan peubah pada data, dengan menghapus argumen dendogram="none"
.
<- heatmaply(mat,
p #dendrogram = "row",
xlab = "", ylab = "",
main = "",
scale = "column",
margins = c(60,100,40,20),
grid_color = "white",
grid_width = 0.00001,
titleX = FALSE,
hide_colorbar = TRUE,
branches_lwd = 0.1,
label_names = c("Country", "Feature:", "Value"),
fontsize_row = 5, fontsize_col = 5,
labCol = colnames(mat),
labRow = rownames(mat),
heatmap_layers = theme(axis.line=element_blank())
) p
Streamgraphs
#devtools::install_github("hrbrmstr/streamgraph")
library(streamgraph)
# Create data:
<- data.frame(
data year=rep(seq(1990,2016) , each=10),
name=rep(letters[1:10] , 27),
value=sample( seq(0,1,0.0001) , 270)
)
# Stream graph with a legend
<- streamgraph(data, key="name", value="value", date="year", height="300px", width="1000px") %>%
pp sg_legend(show=TRUE, label="names: ")
#save the widget
#library(htmlwidgets)
#saveWidget(pp, file=paste0( getwd(), "/streamgraphDropdown.html"))
Interactive Area Chart
<- density(AirPassengers)
density <- plot_ly(x = ~density$x, y = ~density$y ,
fig type = 'scatter', mode = 'lines', fill = "tozeroy")
fig
#library(plotly)
library(igraph)
library(networkD3)
# create a dataset:
<- tibble(
data from=c("A", "A", "B", "D", "C", "D", "E", "B", "C", "D", "K", "A", "M"),
to=c("B", "E", "F", "A", "C", "A", "B", "Z", "A", "C", "A", "B", "K")
)
# Plot
<- simpleNetwork(data, height="100px", width="100px")
p
#save the widget
#saveWidget(p, file=paste0( getwd(), "/network.html"))
Time Series Plot
library(dygraphs)
<- cbind(mdeaths, fdeaths)
lungDeaths dygraph(lungDeaths)
dygraph(lungDeaths) %>% dyRangeSelector()
dygraph(lungDeaths) %>%
dySeries("mdeaths", label = "Male") %>%
dySeries("fdeaths", label = "Female") %>%
dyOptions(stackedGraph = TRUE) %>%
dyRangeSelector(height = 20)
<- HoltWinters(ldeaths)
hw <- predict(hw, n.ahead = 72, prediction.interval = TRUE)
predicted
dygraph(predicted, main = "Predicted Lung Deaths (UK)") %>%
dyAxis("x", drawGrid = FALSE) %>%
dySeries(c("lwr", "fit", "upr"), label = "Deaths") %>%
dyOptions(colors = RColorBrewer::brewer.pal(3, "Set1"))
References
Barter, R. (2017, April 20). Interactive visualization in R. Blog - Rebecca Barter. https://www.rebeccabarter.com/blog/2017-04-20-interactive/
Datasciencebeginners. (2020, May 16). 7 useful interactive charts in R. R-bloggers. https://www.r-bloggers.com/2020/05/7-useful-interactive-charts-in-r/
Dygraphs for R. (n.d.). rstudio.github.io. https://rstudio.github.io/dygraphs/
Holtz, Y. (2018). Interactive charts. The R Graph Gallery – Help and inspiration for R charts. https://www.r-graph-gallery.com/interactive-charts.html