如何在 R 中使用 plotly 在條形圖中顯示長 X 軸標籤?


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

示例

載入 plotly 包

> library(plotly)

考慮以下資料幀

即時演示

> x<-c("United States of America","United Kingdom","Republic of China")
> y<-c(501,510,505)
> df<-data.frame(x,y)
> df

輸出

x y
1 United States of America 501
2 United Kingdom 510
3 Republic of China 505

為 x 建立條形圖

示例

> plot_ly(x=x,y=y,type="bar")%>%layout(xaxis=list(tickangle=45))

輸出

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.

這裡顯示了警告,但它與我們的圖表無關,因此忽略它沒有問題。

輸出

更新於:2020-11-23

369 次瀏覽

開啟你的 職業生涯

完成課程認證

開始
廣告