R语言数据可视化包:
knitr::opts_chunk$set(echo = TRUE, eval = TRUE, tidy = TRUE, highlight = TRUE, warning = FALSE, error = FALSE, message = FALSE, include = TRUE,fig.width = 9.4,fig.height = 4)
require(devtools)
# install_github('rCharts', 'ramnathv') install_github('yihui/recharts')
# install.packages('plotly') install_github('Lchiffon/REmap')
# install.packages('igraph') install.packages('networkD3')
library(plotly)
用法:
str(iris)
## 'data.frame': 150 obs. of 5 variables:
## $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
## $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
## $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
## $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
## $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
p0 <- plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length, showlegend = FALSE)
p1 <- plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length, showlegend = FALSE,
marker = list(size = 10, color = "lightblue", line = list(color = "black",
width = 2))) %>% layout(title = "Styled Scatter", yaxis = list(zeroline = FALSE),
xaxis = list(zeroline = FALSE))
subplot(p0, p1, nrows = 1, margin = 0.05)
library(plotly)
trace_0 <- rnorm(100, mean = 5)
trace_1 <- rnorm(100, mean = 0)
trace_2 <- rnorm(100, mean = -5)
x <- c(1:100)
data <- data.frame(x, trace_0, trace_1, trace_2)
p <- plot_ly(data, x = ~x) %>% add_trace(y = ~trace_0, type = "scatter", name = "node = 'lines'",
mode = "lines") %>% add_trace(y = ~trace_1, type = "scatter", name = "node = 'lines+markers'",
mode = "lines+markers") %>% add_trace(y = ~trace_2, type = "scatter", name = "node = 'markers'",
mode = "markers", marker = list(size = 10, color = "white", line = list(color = "black",
width = 2)))
p
plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length, color = ~Species)
plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length, color = ~Species,
colors = "Set1")
pal <- c("red", "blue", "green")
plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length, color = ~Species,
colors = pal)
为了确保特定的数据值被映射到特定颜色,提供颜色代码的字符向量,并相应地匹配名称属性。
pal <- c("red", "blue", "green")
pal <- setNames(pal, c("virginica", "setosa", "versicolor"))
p <- plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length, color = ~Species,
colors = pal)
plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length, type = "scatter",
mode = "markers", symbol = ~Species, symbols = c("circle", "x", "o"), color = I("black"),
marker = list(size = 10))
library(ggplot2)
d <- diamonds[base::sample(nrow(diamonds), 1000), ]
plot_ly(d, x = ~carat, y = ~price, color = ~carat, colors = "Set1", size = ~carat)