如何在 R 中使用 plotly 建立條形圖?


R 中的 Plotly 是一個專門設計用於建立高度互動且具有出版質量的地圖的程式包。可以用程式包的 plot_ly 函式建立地圖,並且 plot_ly 的三個主要引數定義為 x、y 和型別,其中 x 指 X 軸,y 指 Y 軸,型別指地圖型別,但是軸值儲存在資料幀中或本身是一個共享。

示例

載入 plotly 程式包

> library(plotly)

考慮以下資料幀

即時演示

> x<-c("A","B","C","D")
> count<-c(321,324,320,328)
> df<-data.frame(x,count)
> df

輸出

x count
1 A 321
2 B 324
3 C 320
4 D 328

為 x 建立條形圖

> plot_ly(x=x,y=count,type='bar')

輸出

Warning message:

`arrange_()` is deprecated as of dplyr 0.7.0. Please use `arrange()` instead. See vignette('programming') for more help This warning is displayed once every 8 hours. Call `lifecycle::last_warnings()` to see where this warning was generated.


此處顯示一個警告,但與我們的地圖無關,因此忽略它沒有問題。

輸出

如果我們要建立帶有主標題和軸標題的地圖,那麼我們需要將其儲存在一個物件中,然後使用佈局函式,如下所示

示例

> Bar<-plot_ly(x=x,y=count,type='bar')
> layout(Bar,title="Bar chart",xaxis=list(title="Groups"),yaxis=list(title="Count"))

輸出

更新於:19-11-2020

328 次瀏覽

啟動 職業

完成課程獲得認證

開始
廣告