R语言的plotly怎么使用
这篇文章主要介绍“R语言的plotly怎么使用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“R语言的plotly怎么使用”文章能帮助大家解决问题。
成都创新互联始终坚持【策划先行,效果至上】的经营理念,通过多达十多年累计超上千家客户的网站建设总结了一套系统有效的全网营销推广解决方案,现已广泛运用于各行各业的客户,其中包括:成都塑料袋等企业,备受客户表扬。
plotly包:是一个基于浏览器的交互式图表库,建立在开源的JavaScript图表库plotly.js上,plotly包利用函数plot_ly函数绘制交互图。本文简单介绍几种常见图表的绘制方式,点图、线图及箱线图。
安装包准备
install.packages("plotly") ##安装方式
library(plotly) ##载入
一、点图
1)利用ColorBrewer Palette Names定义颜色,形状 大小
p <- plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length,
color = ~Species #颜色分类
,symbol = ~Species , symbols = c('circle','x','o')#符号分类及对应的表示符号
, colors = "Set1" #颜色选用Set1颜色集的颜色
,mode = 'markers',marker = list(size = 10)) #mode 点图;marker的大小
2)自定义颜色以及与分类相对应的颜色
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)
3)添加"悬浮"注释信息###
d <- diamonds[sample(nrow(diamonds), 1000), ]
p <- plot_ly(
d, x = ~carat, y = ~price,
# Hover text: 悬浮信息
text = ~paste("Price: ", price, '$
Cut:', cut),
color = ~carat, size = ~carat) ##渐变颜色
二、线图
1)自定义数据集
month <- c('January', 'February', 'March', 'April', 'May', 'June', 'July',
'August', 'September', 'October', 'November', 'December')
high_2007 <- c(36.5, 26.6, 43.6, 52.3, 71.5, 81.4, 80.5, 82.2, 76.0, 67.3, 46.1, 35.0)
low_2007 <- c(23.6, 14.0, 27.0, 36.8, 47.6, 57.7, 58.9, 61.2, 53.3, 48.5, 31.0, 23.6)
high_2014 <- c(28.8, 28.5, 37.0, 56.8, 69.7, 79.7, 78.5, 77.8, 74.1, 62.6, 45.3, 39.9)
low_2014 <- c(12.7, 14.3, 18.6, 35.5, 49.9, 58.0, 60.0, 58.6, 51.7, 45.2, 32.2, 29.1)
data <- data.frame(month, high_2007, low_2007, high_2014, low_2014)
#将月份按照如下方式排序(默认是按照字母顺序排列,图形有问题)
data$month <- factor(data$month, levels = data[["month"]])
##add_trace 可添加新的图形
p <- plot_ly(data, x = ~month, y = ~high_2014, name = 'High 2014', type = 'scatter', mode = 'lines',line = list(color = 'purple', width = 4)) %>%
add_trace(y = ~low_2014, name = 'Low 2014', line = list(color = 'rgb(22, 96, 167)', width = 4)) %>%
add_trace(y = ~high_2007, name = 'High 2007', line = list(color = 'purple', width = 4, dash = 'dash')) %>%
add_trace(y = ~low_2007, name = 'Low 2007', line = list(color = 'rgb(22, 96, 167)', width = 4, dash = 'dash')) %>%
##layout 设置标题相关信息
layout(title = "Average High and Low Temperatures in New York",
xaxis = list(title = "Months"),
yaxis = list (title = "Temperature (degrees F)"))
2)综合点图和线图
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, y = ~trace_0, name = 'trace 0', type = 'scatter', mode = 'lines') %>%
add_trace(y = ~trace_1, name = 'trace 1', mode = 'lines+markers') %>%
add_trace(y = ~trace_2, name = 'trace 2', mode = 'markers')
三、箱线图
1)分类箱线图
p <- plot_ly(ggplot2::diamonds, y = ~price, color = ~cut, type = "box")
###多组箱线图 layout
p <- plot_ly(ggplot2::diamonds, x = ~cut, y = ~price, color = ~clarity, type = "box") %>%
layout(boxmode = "group")
关于“R语言的plotly怎么使用”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注创新互联行业资讯频道,小编每天都会为大家更新不同的知识点。
文章标题:R语言的plotly怎么使用
地址分享:http://azwzsj.com/article/gosjci.html