溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

R語言的plotly怎么使用

發(fā)布時間:2022-03-25 15:30:53 來源:億速云 閱讀:710 作者:iii 欄目:大數(shù)據(jù)

這篇文章主要介紹“R語言的plotly怎么使用”的相關(guān)知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“R語言的plotly怎么使用”文章能幫助大家解決問題。

plotly包:是一個基于瀏覽器的交互式圖表庫,建立在開源的JavaScript圖表庫plotly.js上,plotly包利用函數(shù)plot_ly函數(shù)繪制交互圖。本文簡單介紹幾種常見圖表的繪制方式,點圖、線圖及箱線圖。

安裝包準(zhǔn)備

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')#符號分類及對應(yīng)的表示符號
, colors = "Set1" #顏色選用Set1顏色集的顏色
,mode = 'markers',marker = list(size = 10)) #mode 點圖;marker的大小

R語言的plotly怎么使用

2)自定義顏色以及與分類相對應(yīng)的顏色
pal <- c("red", "blue", "green") #定義顏色
pal <- setNames(pal, c("virginica", "setosa", "versicolor")) #定義對應(yīng)的分類

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) ##漸變顏色

R語言的plotly怎么使用

二、線圖 

1)自定義數(shù)據(jù)集

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)

#將月份按照如下方式排序(默認(rèn)是按照字母順序排列,圖形有問題)
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 設(shè)置標(biāo)題相關(guān)信息
  layout(title = "Average High and Low Temperatures in New York",
         xaxis = list(title = "Months"),
         yaxis = list (title = "Temperature (degrees F)"))

R語言的plotly怎么使用

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")

R語言的plotly怎么使用

###多組箱線圖  layout
p <- plot_ly(ggplot2::diamonds, x = ~cut, y = ~price, color = ~clarity, type = "box") %>%
  layout(boxmode = "group")
 

R語言的plotly怎么使用

關(guān)于“R語言的plotly怎么使用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI