您好,登錄后才能下訂單哦!
這篇文章主要介紹“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, '$<br>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怎么使用”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。