交互式可视化plotly之基本图绘制

2020-04-01 16:22:49 浏览数 (1)

Plotly是个交互式可视化的第三方库,可以实现R语言的交互可视化,用法与ggplot差不多,默认的颜色比ggplot好看很多,本文简单介绍一下Plotly的应用。

首先安装并运行包

代码语言:javascript复制
install.packages("plotly")
# 载入ggplot2包
library(plotly)

主要参数: plot_ly(data = data.frame(), type = NULL, name, color, colors = NULL, alpha = NULL) ploly主要通过type来控制是画boxplot,barplot,scatter plot还是其他; name:Values mapped to the trace's name attribute.即选中处追加的名字

Boxplot

代码语言:javascript复制
library(plotly)
fig <- plot_ly(iris, x = ~Petal.Length, color = ~Species, type = "box")
fig

image.png

Scatter Plot

代码语言:javascript复制
fig <- plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length, color = ~Species)
fig

image.png

barplot

代码语言:javascript复制
fig <-
  plot_ly(
  x = c("a", "b", "c"),
  y = c(20, 14, 23),
  name = "barplot",
  type = "bar",
  color = c("#FFA488", "#8CD790", "#00AAAA")
  )
  
  fig

image.png

lineplot

代码语言:javascript复制
x <- c(1:100)
random_y <- rnorm(100, mean = 0)
data <- data.frame(x, random_y)

fig <- plot_ly(data, x = ~x, y = ~random_y, type = 'scatter', mode = 'lines')

fig

image.png

欢迎关注~

0 人点赞