いろんな選択肢があります
HTML + CSS + JSで実現しよう!
# CRANから
install.packages("leaflet")
# GitHubから
devtools::install_github("rstudio/leaflet")
# githubinstallを使うなら
githubinstall::githubinstall("leaflet")
以下のような感じになります:
library(leaflet)
df2 <- data.frame(id=1:100, lng=135+rnorm(100, sd = 0.1), lat=35+rnorm(100, sd = 0.1))
leaflet(df2) %>% addTiles() %>%
addMarkers(~lng, ~lat, label=~paste0(id,"番"),
clusterOptions = markerClusterOptions())
# CRANから
install.packages("dygraphs")
# GitHubから
devtools::install_github("rstudio/dygraphs")
# githubinstallを使うなら
githubinstall::githubinstall("dygraphs")
以下のような感じになります
library(dygraphs)
lungDeaths <- cbind(mdeaths, fdeaths)
dygraph(lungDeaths, height = 450) %>% dyRangeSelector()
# CRANから
install.packages("DiagrammeR")
# GitHubから
devtools::install_github("rich-iannone/DiagrammeR")
# githubinstallを使うなら
githubinstall::githubinstall("DiagrammeR")
例えばこんな感じです:
library(DiagrammeR)
create_graph() %>% add_n_nodes(n = 2) %>% add_edge(from = 1, to = 2) %>%
render_graph()
visNetwork
を利用することも可能:
create_random_graph(n = 30, m = 50) %>% render_graph(output = "visNetwork")
# CRANから
install.packages("networkD3")
# GitHubから
devtools::install_github("christophergandrud/networkD3")
# githubinstallを使うなら
githubinstall::githubinstall("networkD3")
公式サイトから:
library(networkD3)
forceNetwork(Links = MisLinks, Nodes = MisNodes, Source = "source",
Target = "target", Value = "value", NodeID = "name",
Group = "group", opacity = 0.8)
# CRANから
install.packages("DT")
# GitHubから
devtools::install_github("rstudio/DT")
# githubinstallを使うなら
githubinstall::githubinstall("DT")
こんな感じです:
library(DT)
iris2 = iris[c(1:10, 51:60, 101:110), ]
datatable(iris2, filter = 'top', options = list(
pageLength = 5, autoWidth = TRUE
))
# CRANから
install.packages("plotly")
# GitHubから
devtools::install_github("ropensci/plotly")
# githubinstallを使うなら
githubinstall::githubinstall("plotly")
library(plotly)
library(magrittr)
plot_ly(iris, x = ~Sepal.Length, y = ~Sepal.Width, color = ~Species)
plot_ly(iris, x = ~Species, y = ~Sepal.Length, type = "box")
type
(グラフの種類)が重要
type = scatter
から分岐p_empty <- plot_ly()
p_empty
plot_ly(data = iris)
plot_ly(data = iris,
x = ~Sepal.Length,
y = ~Sepal.Width)
~
が必要plot_ly(data = iris,
x = ~Species,
y = ~Sepal.Length,
type = "box")
type
でグラフのタイプを指定
plot_ly(data = iris,
x = ~Sepal.Length,
y = ~Sepal.Width,
split = ~Species,
type = "scatter")
plot_ly(data = iris,
x = ~Sepal.Length,
y = ~Sepal.Width,
color = ~Petal.Length,
type = "scatter")
plot_ly(data = iris,
x = ~Sepal.Length,
y = ~Sepal.Width,
split = ~Species,
type = "scatter") %>%
layout(xaxis = list(
showgrid = F,
rangemode = "tozero",
nticks = 6,
title = "ほげ"
))
xaxis
引数に設定したい内容をlist型でyaxis
引数へplot_ly(data = iris,
x = ~Sepal.Length,
y = ~Sepal.Width,
split = ~Species,
type = "scatter") %>%
layout(legend = list(
orientation = "h",
yanchor = "bottom"
))
legend
引数にlist型で設定を渡すplot_ly(data = iris,
x = ~Sepal.Length,
y = ~Sepal.Width,
color = ~Species,
type = "scatter") %>%
layout(title = "たいとる",
paper_bgcolor = "#aaf",
plot_bgcolor = "#66d",
showlegend = FALSE)
df <- data.frame(
x = c(1:50),
y1 = rnorm(50, mean = 5),
y2 = rnorm(50, mean = 0)
)
plot_ly(df, x = ~x, y = ~y1,
name = "kosaki",
type = "scatter", mode = "lines+marker") %>%
add_trace(y = ~y2,
name = "chitoge",
mode = "markers")
add_trace
関数で新たに描写オブジェクトを追加
p1 <- plot_ly(df, x = ~x, y = ~y1,
type = "scatter",
mode = "markers")
p2 <- plot_ly(df, x = ~x, y = ~y2,
type = "scatter",
mode = "lines")
subplot(p1, p2, nrows = 2)
subplot
で束ねることが可能
nrows
引数で行数を指定layout
を繋げば、全体でのlayout設定が可能df_tidy <- df %>%
tidyr::gather(var, value, -x)
plot_ly(df_tidy,
x = ~x, y = ~value,
color = ~var,
yaxis = ~var) %>%
add_lines() %>%
subplot(nrows = 2)
plot_ly
のyaxis
でY軸のIDを準備
library(ggplot2)
gg <- ggplot(df_tidy, aes(x, value, group = var)) +
geom_point(aes(color = var))
ggplotly(gg)
ggploly
関数で一発変換
gg_facet <- ggplot(df_tidy,
aes(x, value, group = var)) +
geom_point(aes(color = var)) +
facet_wrap(~var, nrow = 2)
ggplotly(gg_facet)
.plotly
htmlwidgets::saveWidget
plotly::export
を使うAnimals <- c("giraffes", "orangutans", "monkeys")
SF_Zoo <- c(20, 14, 23)
LA_Zoo <- c(12, 18, 29)
data <- data.frame(Animals, SF_Zoo, LA_Zoo)
plot_ly(data, x = ~Animals, y = ~SF_Zoo, type = 'bar', name = 'SF Zoo') %>%
add_trace(y = ~LA_Zoo, name = 'LA Zoo') %>%
layout(yaxis = list(title = 'Count'), barmode = 'stack')
plot_ly(y = ~rnorm(50), type = "box") %>%
add_trace(y = ~rnorm(50, 1))
plot_ly(z = volcano, type = "heatmap")
plot_ly(z = ~volcano) %>% add_surface()