0
点赞
收藏
分享

微信扫一扫

plotly for R

简介

  • 交互式绘图工具, 提供python, R, js接口, 以R接口为例
  • api类似, 需要注意在参数中涉及子项的在R中使用列表, 在python中使用字典
  • 支持管道符操作 %>%, 类似ggplot2的 +
  • 详细教程参见 (https://plotly.com/r/)

基本命令

  • 基本绘图命令:plot_ly(data, x, y, z, type, mode...)

图形命令 (add_trace/add_xxx)

点类型 (散点图, 折线图, 气泡图...)

散点图

  • 指定参数add_trace(..., type="scatter", mode="")
  • 等同于add_markers/add_lines
  • line: 传入列表, 指定线的特征
  • connectgaps: 应用于折线图的参数, 确定所提供的数据数组中的间隙(NA)是否连接。
  • 同属scatter类型下的不同模式 (mode)
library(plotly)
plot_ly(economics, x = ~date, y = ~uempmed) %>% 
  add_trace(type="scatter", mode="markers", marker = list(color = "black", line = list(color = "red", width = 1)))

## head(economics)
## # A tibble: 6 x 6
##   date         pce    pop psavert uempmed unemploy
##   <date>     <dbl>  <dbl>   <dbl>   <dbl>    <dbl>
## 1 1967-07-01  507. 198712    12.6     4.5     2944
## 2 1967-08-01  510. 198911    12.6     4.7     2945
## 3 1967-09-01  516. 199113    11.9     4.6     2958
## 4 1967-10-01  512. 199311    12.9     4.9     3143
## 5 1967-11-01  517. 199498    12.8     4.7     3066
## 6 1967-12-01  525. 199657    11.8     4.8     3018


## 等同于以下命令
plot_ly(economics, x = ~date, y = ~uempmed) %>% 
  add_markers(marker = list(color = "black", line = list(color = "red", width = 1)))


在散点图上又有许多扩展, 将点连成线就变成了折线图

plot_ly(economics, x = ~date, y = ~uempmed) %>% 
  add_trace(type = "scatter", mode = "lines", 
            line = list(color = "green", width = 2))

## 等同于以下命令
plot_ly(economics, x = ~date, y = ~uempmed) %>% 
  add_trace(type = "scatter", mode = "lines", 
            line = list(color = "green", width = 2))

也可以在折线图的基础上将点标记出来

  • 包含的trace: marker, line
plot_ly(economics, x = ~date, y = ~uempmed) %>% 
  add_trace(type = "scatter", mode = "markers+lines", 
            marker = list(color = "black", line = list(color = "red", width = 1)),
            line = list(color = "green", width = 2))

## 等同于以下命令
plot_ly(economics, x = ~date, y = ~uempmed) %>% 
  add_trace(type = "scatter", mode = "lines", 
            line = list(color = "green", width = 2)) %>% 
  add_trace(type = "scatter", mode = "markers",
            marker = list(color = "black", line = list(color = "red", width = 1)))

气泡图则是将点的大小和颜色与其他特征联系起来 (Bubble)

library(plotly)

data <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/school_earnings.csv")

data$State <- as.factor(c('Massachusetts', 'California', 'Massachusetts', 'Pennsylvania', 'New Jersey', 'Illinois', 'Washington DC',
                          'Massachusetts', 'Connecticut', 'New York', 'North Carolina', 'New Hampshire', 'New York', 'Indiana',
                          'New York', 'Michigan', 'Rhode Island', 'California', 'Georgia', 'California', 'California'))

## head(data)
##      School Women Men Gap         State
## 1       MIT    94 152  58 Massachusetts
## 2  Stanford    96 151  55    California
## 3   Harvard   112 165  53 Massachusetts
## 4    U.Penn    92 141  49  Pennsylvania
## 5 Princeton    90 137  47    New Jersey
## 6   Chicago    78 118  40      Illinois

fig <- plot_ly(data, x = ~Women, y = ~Men, text = ~School, type = 'scatter', 
               mode = 'markers', size = ~Gap, color = ~State, colors = 'Paired',
               marker = list(opacity = 0.5, sizemode = 'diameter', line = list(color="white", width=1)))
fig <- fig %>% layout(title = 'Gender Gap in Earnings per University',
                      xaxis = list(showgrid = FALSE),
                      yaxis = list(showgrid = FALSE),
                      showlegend = FALSE)

fig


哑铃图, 将起始点与终止点用segment连接起来 (Dumbbell)

  • 包含的trace: marker, line
s <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/school_earnings.csv")
# order factor levels by men's income (plot_ly() will pick up on this ordering)
s$School <- factor(s$School, levels = s$School[order(s$Men)])

## head(s)
##      School Women Men Gap
## 1       MIT    94 152  58
## 2  Stanford    96 151  55
## 3   Harvard   112 165  53
## 4    U.Penn    92 141  49
## 5 Princeton    90 137  47
## 6   Chicago    78 118  40

library(plotly)
fig <- plot_ly(s)
## add_segment添加线段, 指定起始位置x, y和终止位置xend, yend
fig <- fig %>% add_segments(x = ~Women, xend = ~Men, y = ~School, yend = ~School, showlegend = F,
                            line = list(color = "rgb(230,230,230)"))
fig <- fig %>% add_markers(x = ~Women, y = ~School, name = "Women", color = I("pink"))
fig <- fig %>% add_markers(x = ~Men, y = ~School, name = "Men", color = I("blue"))
fig <- fig %>% layout(
  title = "Gender earnings disparity",
  xaxis = list(title = "Annual Salary (in thousands)"),
  margin = list(l = 65)
)

fig

对于多个变量, 创建二维散点图矩阵 (Splom)

  • marker: 设置splom中点的特征
  • text: 文本信息
library(plotly)

df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/iris-data.csv')

## head(df)
##   sepal.length sepal.width petal.length petal.width       class
## 1          5.1         3.5          1.4         0.2 Iris-setosa
## 2          4.9         3.0          1.4         0.2 Iris-setosa
## 3          4.7         3.2          1.3         0.2 Iris-setosa
## 4          4.6         3.1          1.5         0.2 Iris-setosa
## 5          5.0         3.6          1.4         0.2 Iris-setosa
## 6          5.4         3.9          1.7         0.4 Iris-setosa

## 注意色阶是左闭右开的区间
pl_colorscale=list(c(0.0, '#19d3f3'),
                   c(0.333, '#e763fa'),
                   # c(0.333, '#e763fa'),
                   c(0.666, '#e763fa'),
                   # c(0.666, '#636efa'),
                   c(1, '#636efa'))

axis = list(showline=FALSE,
            zeroline=FALSE,
            gridcolor='#ffff',
            ticklen=4)

fig <- df %>%
  plot_ly() 
fig <- fig %>%
  add_trace(
    type = 'splom',
    dimensions = list(
      list(label='sepal length', values=~sepal.length),
      list(label='sepal width', values=~sepal.width),
      list(label='petal length', values=~petal.length),
      list(label='petal width', values=~petal.width)
    ),
    text=~class,
    marker = list(
      color = as.integer(as.factor(df$class)) / 3,
      colorscale = pl_colorscale,
      size = 7,
      line = list(
        width = 1,
        color = 'rgb(230,230,230)'
      )
    )
  ) 
fig <- fig %>%
  layout(
    title= 'Iris Data set',
    hovermode='closest',
    dragmode= 'select',
    plot_bgcolor='rgba(240,240,240, 0.95)',
    xaxis=list(domain=NULL, showline=F, zeroline=F, gridcolor='#ffff', ticklen=4),
    yaxis=list(domain=NULL, showline=F, zeroline=F, gridcolor='#ffff', ticklen=4),
    xaxis2=axis,
    xaxis3=axis,
    xaxis4=axis,
    yaxis2=axis,
    yaxis3=axis,
    yaxis4=axis
  )

fig

柱类型(柱状图, 堆积图, 直方图, 瀑布图...)

柱状图 (Bar Charts)

  • 指定参数add_trace(..., type="bar")
  • 等同于add_bars
  • layout中对于柱状图的设置
library(plotly)

## 构建数据
x= c(1, 2, 3, 5.5, 10)
y= c(10, 8, 6, 4, 2)
width = c(0.8, 0.8, 0.8, 3.5, 4)
data <- data.frame(x, y, width)

## 绘图
fig <- plot_ly(data)
fig <- fig %>% add_trace(
  type="bar",
  x= ~x,
  y= ~y,
  width = ~width,
  marker = list(color = 'rgb(158,202,225)', 
                line = list(color = 'rgb(8,48,107)', width = 1.5)))

fig


直方图 (Histgram)

  • 指定参数add_trace(..., type="histogram")
  • 等同于 add_histogram
fig <- plot_ly(alpha = 0.6)
fig <- fig %>% add_histogram(x = ~rnorm(500))
fig <- fig %>% add_histogram(x = ~rnorm(500) + 1)
fig <- fig %>% layout(barmode = "overlay")

fig


瀑布图 (Waterfall)

  • 指定参数add_trace(..., type="waterfall")
library(plotly)

x= list("Sales", "Consulting", "Net revenue", "Purchases", "Other expenses", "Profit before tax")
measure= c("relative", "relative", "total", "relative", "relative", "total")
text= c("+60", "+80", "", "-40", "-20", "Total")
y= c(60, 80, 0, -40, -20, 0)
data = data.frame(x=factor(x,levels=x),measure,text,y)

fig <- plot_ly(
  data, name = "20", type = "waterfall", measure = ~measure,
  x = ~x, textposition = "outside", y= ~y, text =~text,
  connector = list(line = list(color= "rgb(63, 63, 63)"))) 
fig <- fig %>%
  layout(title = "Profit and loss statement 2018",
        xaxis = list(title = ""),
        yaxis = list(title = ""),
        autosize = TRUE,
        showlegend = TRUE)

fig

数据整体分布 (箱线图, 小提琴图...)

箱线图 (Box Plot)

  • 包含的trace: marker(点)
  • 使用add_trace(..., type="box")绘制箱线图
  • 等同于add_boxplot
  • line: 传入列表, 指定箱线图的边框信息
  • boxmean: 展示箱线图均值所在的位置: TRUE | "sd" (mean和sd都标记出来) | FALSE
  • layout中对于箱线图的参数
library(plotly)
fig <- plot_ly(y = list(1,2,3,4,5), type = "box", quartilemethod="linear", name="Linear Quartile Mode") %>% 
  add_trace(y = list(1,2,3,4,5), quartilemethod="inclusive", name="Inclusive Quartile Mode") %>% 
  add_trace(y = list(1,2,3,4,5), quartilemethod="exclusive", name="Exclusive Quartile Mode") %>% 
  layout(title = "Modifying The Algorithm For Computing Quartiles")

fig

fig <- plot_ly(y = ~rnorm(50), type = "box", boxpoints = "all", jitter = 0.3, pointpos = 1.8)

fig


小提琴图 (Violin Plot)

  • 包含的trace: box(小提琴中的箱线图), marker(点), line(边框)
  • 指定参数add_trace(..., type="voilin")绘制小提琴图
  • marker: 传入列表参数, 设定小提琴图中点的特征
  • line: 传入列表参数, 指定小提琴的边框特征
  • side: 指定小提琴展示在哪一侧, 可选参数有: "negative"(左侧) | "positive"(右侧) | "both"(两侧, 默认)
  • points: 展示点, 可选参数有: "all"(全部点) | "outliers"(异常点, 默认) | FALSE(不展示点, 只展示小提琴)
  • pointpos: 散点图与小提琴图的偏移量, -2 ~ 2的区间
  • jitter: 点的抖动范围, 0 ~ 1的范围
  • filecolor: 小提琴图的填充颜色
  • meanline: 传入列表小提琴内部显示与样本平均值相对应的线
  • layout中对于小提琴图的设定 (官网没有这些参数, 但是对图形有影响)
library(plotly)

df <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/violin_data.csv")

fig <- df %>%
  plot_ly(type = 'violin') 
fig <- fig %>%
  add_trace(
    x = ~day[df$smoker == 'Yes'],
    y = ~total_bill[df$smoker == 'Yes'],
    legendgroup = 'Yes',
    scalegroup = 'Yes',
    name = 'Yes',
    side = 'negative',
    box = list(
      visible = T
    ),
    meanline = list(
      visible = T
    ),
    color = I("blue")
  ) 
fig <- fig %>%
  add_trace(
    x = ~day[df$smoker == 'No'],
    y = ~total_bill[df$smoker == 'No'],
    legendgroup = 'No',
    scalegroup = 'No',
    name = 'No',
    side = 'positive',
    box = list(
      visible = T
    ),
    meanline = list(
      visible = T
    ),
    color = I("green")
  ) 

fig <- fig %>%
  layout(
    xaxis = list(
      title = ""  
    ),
    yaxis = list(
      title = "",
      zeroline = F
    ),
    violingap = 0,
    violingroupgap = 0,
    violinmode = 'overlay'
  )

fig

不知道为何violingap, violingroupgap, violinmode会报warning提示没有该参数, 但是实际改变这些参数确实是对图形有影响的

桑基图 (Sankey Diagram)

  • 包含的trace: node (节点), link (连接)
  • 指定参数add_trace(..., type="sankey")
  • 节点node和连接link
  • node: 传入列表, 指定节点信息
  • link: 传入列表, 设置连接的特征
library(plotly)

fig <- plot_ly(
  type = "sankey",
  orientation = "h",
  
  node = list(
    label = c("A1", "A2", "B1", "B2", "C1", "C2"),
    color = c("blue", "blue", "blue", "blue", "blue", "blue"),
    pad = 15,
    thickness = 20,
    line = list(
      color = "black",
      width = 0.5
    )
  ),
  
  link = list(
    source = c(0,1,0,2,3,3),  ## 每个值对应一个link的起始位置, 值代表node的index, 从0开始
    target = c(2,3,3,4,4,5),  ## 每个值对应一个link的终止位置, 值代表node的index, 从0开始
    value =  c(8,4,2,8,4,2)   ## 每个值对应一个link的流量
  )
)
fig <- fig %>% layout(
  title = "Basic Sankey Diagram",
  font = list(
    size = 10
  )
)

fig

library(plotly)
library(rjson)

json_file <- "https://raw.githubusercontent.com/plotly/plotly.js/master/test/image/mocks/sankey_energy.json"
json_data <- fromJSON(paste(readLines(json_file), collapse=""))

fig <- plot_ly(
    type = "sankey",
    domain = list(
      x =  c(0,1),
      y =  c(0,1)
    ),
    orientation = "h",
    valueformat = ".0f",
    valuesuffix = "TWh",

    node = list(
      label = json_data$data[[1]]$node$label,
      color = json_data$data[[1]]$node$color,
      pad = 15,
      thickness = 15,
      line = list(
        color = "black",
        width = 0.5
      )
    ),

    link = list(
      source = json_data$data[[1]]$link$source,
      target = json_data$data[[1]]$link$target,
      value =  json_data$data[[1]]$link$value,
      label =  json_data$data[[1]]$link$label
    )
  ) 
fig <- fig %>% layout(
    title = "Energy forecast for 2050<br>Source: Department of Energy & Climate Change, Tom Counsell via <a href='https://bost.ocks.org/mike/sankey/'>Mike Bostock</a>",
    font = list(
      size = 10
    ),
    xaxis = list(showgrid = F, zeroline = F),
    yaxis = list(showgrid = F, zeroline = F)
)

fig

误差线 (Error Bar)

  • 指定add_trace(..., error_y / error_x)绘制误差线
  • 注意需要先计算特征的方差/标准差合并到原数据中, 指定该变量绘制误差线
  • 误差线可以应用于barplot/Scatterplot /lineplot
  • 传入列表指定误差线特征
library(plotly)
library(plyr)

data_mean <- ddply(ToothGrowth, c("supp", "dose"), summarise, length = mean(len))
data_sd <- ddply(ToothGrowth, c("supp", "dose"), summarise, length = sd(len))
data <- data.frame(data_mean, data_sd$length)
data <- rename(data, c("data_sd.length" = "sd"))
data$dose <- as.factor(data$dose)

plot_ly() %>% 
  add_trace(data = data[which(data$supp == 'OJ'),], x = ~dose, y = ~length, type = 'bar', name = 'OJ',
            error_y = ~list(array = sd, color = '#000000')) %>% 
  add_trace(data = data[which(data$supp == 'VC'),], x = ~dose, y = ~length, type = "bar", name = 'VC',
            error_y = ~list(array = sd, color = '#000000'))

图内文字和批注 (text and annotations)

  • add_text指定文字内容以及字体特征
  • 根据所绘制图形的不同文字的位置也有所不同

text

library(plotly)

df <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv", 
               stringsAsFactors = FALSE)
df <- df[which(df$year==2007 & df$continent=='Europe' & df$pop > 2.e6),]

fig <- plot_ly(df, type='bar', x = ~country, y = ~pop, text = ~lifeExp, name="",
               hovertemplate = paste('%{x}', '<br>lifeExp: %{text:.2s}<br>'),
               texttemplate = '%{y:.2s}', textposition = 'outside')

fig <- fig %>% layout(uniformtext=list(minsize=8, mode='hide'))
fig


annotations

  • 包含箭头, 文本框的批注信息
  • add_annotations指定批注信息
  • 也可以由layout(..., annotations)指定, 需要传入列表参数
  • width/height: 批注的宽高
  • opacity: 批注的透明度, 0 ~ 1的区间 (Default: 1)
  • align: 设置批注的水平对齐方式, 可选参数: "left" | "center" | "right" (Default: "center")
  • valign: 设置批注的数值对齐方式, 可选参数: "top" | "middle" | "bottom" (Default: "middle")
  • bgcolor: 设置批注的背景颜色 (Default: "rgba(0,0,0,0)")
  • bordercolor: 设置批注的边框颜色 (Default: "rgba(0,0,0,0)")
  • borderpad: 设置边框和文字内容之间的填充 (Default: 1)
  • borderwidth: 边框的宽度
  • showarrow: 布尔值, 是否显示箭头
  • arrowwidth: 箭头的宽度
  • arrowcolor: 箭头的颜色
  • arrowside: 指定箭头指向的方向, 可选参数: "end" | "start" | "end+start" | "none" (Default: "end")
  • arrowhead/startarrowhead: 指定结束/开始箭头的形状, 0 ~ 8的区间 (Default: 1)
  • arrowsize/startarrowsize: 指定结束/开始位置箭头的大小 (Default: 1)
  • standoff/startstandoff: 设置距离结束/开始位置的距离, 以像素为单位 (Default: 0)
  • ax/ay: 设置批注与图形的偏移量, 以像素为单位 (ax: 左右偏移; ay: 上下偏移)
  • xref/yref: 设置批注相对参考坐标轴, 可选参数 "x" | "x2"... | "y" | "y2"...
library(plotly)

m <- mtcars[which.max(mtcars$mpg), ]

a <- list(
  x = m$wt,
  y = m$mpg,           # 需要添加注释的位置
  text = rownames(m),  # 注释内容
  xref = "x",          # 参考坐标轴x
  yref = "y",          # 参考坐标轴y
  showarrow = TRUE,    # 展示箭头
  arrowhead = 7,       # 箭头类型 (7 -> 方块)
  ax = 20,             # x轴偏移量 (20px)
  ay = -40             # y轴偏移量 (40px)
)

fig <- plot_ly(mtcars, x = ~wt, y = ~mpg)
fig <- fig %>% add_markers()
fig <- fig %>% layout(annotations = a)

fig

热图 (Heatmap)

  • add_trace(p, type="heatmap"[, ...])
library(plotly)
## dim(volcano)
## [1] 87 61

fig <- plot_ly(z = volcano,    ## (data.frame/matrix/array)格式, 每一个值对应热图一个cell的颜色
               type = "heatmap")

fig

m <- matrix(rnorm(9), nrow = 3, ncol = 3)
fig <- plot_ly(
    x = c("a", "b", "c"), y = c("d", "e", "f"),  ## 指定x/y
    z = m, type = "heatmap"
)

fig

其他

  • 布局 (layout)
  • xref: 横向偏移时参考的坐标轴 (Default: "container")
  • yref: 纵向偏移时参考的坐标轴 (Default: "container")
  • x: 在标准化坐标系中,相对于"xref"的x位置从"0" (左)到"1" (右) (Default: 0.5)
  • y: 在标准化坐标系中,相对于"yref"的y位置从“0”(左)到“1”(右) (Default: "auto")
  • xanchor: 设置标题相对于其x位置的水平对齐方式, 可选参数: "left" (左对齐) | "right" (右对齐) | "center" (居中) | "auto" (将"xref"除以3,并根据"x"的值自动计算"xanchor"值) (Default: "auto")
  • yanchor: 设置标题相对于其y位置的水平对齐方式, 可选参数: "left" (左对齐) | "right" (右对齐) | "center" (居中) | "auto" (将"yref"除以3,并根据"y"的值自动计算"yanchor"值) (Default: "auto")
  • pad: 设置标题的填充。每个填充值仅在相应设置了相应的"xanchor"/"yanchor"值时才适用
  • orientation: 设置图例的方向, 可选参数: "v" (竖直) | "h" (水平)
  • traceorder: 确定图例项的显示顺序, 可选参数: "reversed" (与normal相反) | "grouped" (则项目将分组显示(当提供跟踪"legendgroup"时) | "reversed+grouped" (与"grouped"按相反的顺序显示) | "normal" (与输入数据相同的顺序从上到下显示)
  • tracegroupgap: 设置图例组之间的垂直间距 (以px为单位) (Default: 10)
  • itemsizing: 确定图例项符号是否具有相应的trace进行缩放, 可选参数: "trace" | "constant"
  • x/y: 图例的坐标 (-2 ~ 3之间)
  • xanchor: 图例的水平定位, 可选参数: "auto" | "left" | "center" | "right"
  • yanchor: 图例的竖直定位, 可选参数: "auto" | "top" | "middle" | "bottom"
  • title: 传入列表, 设置图例标题的特征
  • type: 设置坐标轴的类型, 可选参数: "-" | "linear" | "log" | "date" | "category" | "multicategory" (Default: "-")
  • autorange: 布尔值, 是否根据输入数据自动计算坐标轴的范围 (Default: TRUE)
  • rangemode: 适用于线性轴, 则根据输入数据的极值计算范围, 可选参数: "normal" | "tozero" (坐标轴扩展到0) | "nonnegative" (坐标轴始终非负)
  • range: 手动设置坐标轴的范围
  • fixedrange: 布尔值, 确定此轴是否可缩放, 如果为TRUE, 则禁用缩放
  • tickmode: 设定轴的刻度模式, 可选参数: "auto" (通过"nticks"设置刻度数) | "linear" (刻度的位置由起始位置"tick0"和刻度步长"dtick"决定) | "array" (通过"tickvals"设置ticks的位置,tick文本为"ticktext")
  • nticks: 指定特定轴的最大刻度数
  • tick0: 设置坐标轴上第一个刻度的位置
  • dtick: 设置刻度步长
  • tickvals: 传入数组, 设置该坐标轴上的刻度出现的值
  • ticktext: 每一个tickvals出现的刻度上的显示的文本
  • ticks: 是否绘制刻度, 可选参数: "outside" | "inside" | ""
  • mirror: 确定坐标轴/刻度是否镜像到另一侧, 可选参数: TRUE | "ticks" | FALSE | "all" | "allticks"
  • ticklen: 设置刻度长度 (px) (Default: 5)
  • tickwidth: 设置刻度宽度 (px) (Default: 1)
  • tickcolor: 设置刻度的颜色 (Default: "#444")
  • showticklabels: 布尔值, 是否绘制刻度标签
  • automargin: 确定长刻度标签是否自动扩展外边距
  • tickfont: 刻度的字体特征
  • tickangle: 刻度的角度
  • tickprefix: 刻度的前缀 (Default: "")
  • showtickprefix: 指定哪些刻度会展示签证, 可选参数: "all" | "first" | "last" | "none" (Default: "all")
  • ticksuffix: 刻度的后缀 (Default: "")
  • showticksuffix: 指定哪些刻度会展示签证, 可选参数: "all" | "first" | "last" | "none" (Default: "all")
  • showline: 布尔值, 是否绘制此坐标轴的边界线
  • linecolor: 坐标轴的颜色 (Default: "#444")
  • linewidth: 设置坐标轴的宽度 (Default: 1)
  • showgrid: 确定是否绘制网格线。如果为"TRUE", 则在每个刻度线处绘制网格线
  • gridcolor: 设置网格线的颜色 (Default: "#eee")
  • gridwidth: 设置网格线的宽度 (Default: 1)
  • zeroline: 布尔值, 确定是否沿该轴的0值绘制直线
  • zerolinecolor: 设置0线的颜色 (Default: "#444")
  • zerolinewidth: 设置0线的宽度 (Default: 1)
  • side: 确定坐标轴绘制的位置, 可选参数: "top" | "bottom" | "left" | "right"
  • domain: 设置此轴的域 (Default: [0, 1])
  • anchor: 如果设置为相反的字母轴id (例如"x2", "y"), 则此轴将绑定到相应的相反字母轴。如果设置为free,则该轴的位置由position决定。
  • position: 设置坐标轴在图形中的位置 (在标准化坐标中), 只有当anchor设置为free时才有效果
  • ggplot2图形转换plotly: ggplotly(p)
  • 导出静态图片
library(plotly)

if (!require("processx")) install.packages("processx")
fig <- plot_ly(z = ~volcano) %>% add_surface()
orca(fig, "surface-plot.png", scale = 0.5, verbose = T)

## exported surface-plot, in 1822.699999 ms
## \
## done with code 0 in 1.82 sec - all task(s) completed

举报

相关推荐

0 条评论