R plotly包函数的使用 plot_ly(),add_trace(),layout()

Plotly 是个交互式可视化的第三方库,官网提供了Python,R,Matlab,JavaScript,Excel的接口,因此我们可以很方便地在这些软件中调用Plotly,从而实现交互式的可视化绘图。
以下是在R 中 plotly包提供的各项函数。

plotly各项函数
1.plot_ly()
官网地址

plot_ly(data = data.frame(), ..., type = NULL, color, colors = NULL,
  alpha = 1, symbol, symbols = NULL, size, sizes = c(10, 100), linetype,
  linetypes = NULL, split, width = NULL, height = NULL, source = "A")

data:数据框
type:图形类型,有’scatter’,’bar’,’box’,’heatmap’,’histogram’,’histogram 2d’,’area’,’pie’,’contour’,’histogram 2d’,‘contour’, ’scatter3d’,’surface’,’mesh3d’,scattergeo’,’choropleth’
color:颜色变量,
colors:具体的颜色;colors可以是RColorBrewer包中的调色板颜色,也可以是十六进制的 “#RRGGBB” 格式
group:分布变量
symbol:符号变量
symbols:具体的符号类型,比如 ‘dot’, ‘cross’,‘diamond’, ‘square’, ‘triangle-down’, ‘triangle-left’, ‘triangle-right’,‘triangle-up’
size:尺寸变量
alpha:透明度

举例

library(plotly)
num <- c(1,2,3,4)
o_status = c("success","refund","success","refund")
dacheng = c(34,45,56,67)
d <- data.frame(num,o_status,dacheng)
plot_ly(d,x = ~ num,y = ~ dacheng,color = o_status,type = 'bar',alpha = 0.5) 

在这里插入图片描述

2.add_trace()
添加图层
官网地址

add_trace(p, ..., data = NULL, inherit = TRUE)
# 添加markers
add_markers(p, x = NULL, y = NULL, z = NULL, ..., data = NULL,
  inherit = TRUE)
# 添加标签
add_text(p, x = NULL, y = NULL, z = NULL, text = NULL, ...,
  data = NULL, inherit = TRUE)
add_paths(p, x = NULL, y = NULL, z = NULL, ..., data = NULL,
  inherit = TRUE)
# 添加拟合
add_lines(p, x = NULL, y = NULL, z = NULL, ..., data = NULL,
  inherit = TRUE)
add_area(p, r = NULL, t = NULL, ..., data = NULL, inherit = TRUE)
add_pie(p, values = NULL, labels = NULL, ..., data = NULL,
  inherit = TRUE)
add_bars(p, x = NULL, y = NULL, ..., data = NULL, inherit = TRUE)
add_histogram(p, x = NULL, y = NULL, ..., data = NULL, inherit = TRUE)
add_histogram2d(p, x = NULL, y = NULL, z = NULL, ..., data = NULL,
  inherit = TRUE)
add_heatmap(p, x = NULL, y = NULL, z = NULL, ..., data = NULL,
  inherit = TRUE)
add_contour(p, z = NULL, ..., data = NULL, inherit = TRUE)
add_boxplot(p, x = NULL, y = NULL, ..., data = NULL, inherit = TRUE)
add_surface(p, z = NULL, ..., data = NULL, inherit = TRUE)
add_mesh(p, x = NULL, y = NULL, z = NULL, ..., data = NULL,
  inherit = TRUE)
add_scattergeo(p, ...)

举例:

library(plotly)
num <- c(1,2,3,4,5,6,7)
o_status = c("success","refund","success","refund","refund","success","refund")
dacheng = c(34,45,56,100,67,98,43)
d <- data.frame(num,o_status,dacheng)
plot_ly(d,x = ~ num,y = ~ dacheng,color = o_status,alpha = 0.5) %>%
  add_text(text = 'test') %>%
  add_lines() %>%
  add_markers() %>%
  add_bars()

在这里插入图片描述
3.layout()
调整可视化效果
官网地址

  • title

text,font[family,size,color],xref,yref,x,y,xanchor,yanchor,pad

ps:xanchor:相对于x位置的水平对齐方式,有如下取值:“auto” | “left” | “center” | “right”
pad:设置标题的填充

  • legend

bgcolor,bordercolor,borderwidth,font,orientation,traceorder,xanchor,itemwidth,itemsizing,itemclick,x,y

ps:x和y表示图例在坐标内的位置
bordercolor:设置图例外边框颜色
borderwidth:设置图例外边框的线条宽度

  • margin

l,r,t,b,pad,autoexpand

  • autosize TRUE or FALSE
  • width 设置图表画布的宽度
  • height 设置图表画布的高度
  • font
  • uniformtext

mode [FALSE | “hide” | “show”],minsize

  • separators
  • paper_bgcolor 设置绘制图形的纸张颜色。默认值:"#fff"
  • plot_bgcolor
  • autotypenumbers
  • colorscale

sequential,sequentialminus,diverging

  • colorway
  • xaxis、yaxis 设置x、y 坐标轴参数

包括如下设置项
range 列表,设置坐标轴的取值范围
title:设置坐标轴的标题,包括标题内容、字体、颜色、大小等
tickformat:设置刻度线标签格式,如:’,’ 可以显示千分位标示;'YYYY-MM-DD’可以将日期格式化为类似 2019-01-01
tickprefix,ticksuffix:设置刻度标签前,后缀字符串
linecolor:坐标轴线的颜色
rangeslider:设置滑动条,有设置项(bgcolor,bordercolor,range,borderwidth)
rangeselector:设置范围选择按钮,有设置项(visible,buttons[visible,count,step,stepmode,label],x、y,xanchor,yanchor,font,bgcolor,bordercolor,borderwidth)

  • hovermode

enumerated , one of ( “x” | “y” | “closest” | FALSE | “x unified” | “y unified” )

举例

library(plotly)
num <- c(1,2,3,4,5,6,7)
o_status = c("success","refund","success","refund","refund","success","refund")
dacheng = c(34,45,56,100,67,98,43)
d <- data.frame(num,o_status,dacheng)
plot_ly(d,x = ~ num,y = ~ dacheng,color = o_status,alpha = 0.5) %>%
  add_text(text = 'test') %>%
  add_lines() %>%
  add_markers() %>%
  add_bars() %>%
  layout(
    title = 'test_plotly',
    legend = list(
      borderwidth = 2,
      orientation = 'h'
    ),
    margin = list(
      l = 70,
      r = 100
    )
    )

在这里插入图片描述

评论 1 您还未登录,请先 登录 后发表或查看评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:数字20 设计师:CSDN官方博客 返回首页

打赏作者

Mina笑美

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值