如何在 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 创建条形图

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

示例

> 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 次浏览

开启你的 职业生涯

完成课程认证

开始
广告