How to display long X-axis labels in a bar chart using plotly in R?


Plotly in R is a package specifically designed to create highly-interactive and publication-quality charts. The chart can be created by using plot_ly function of the package and there are three main arguments of plot_ly defined as x, y, and type, where x refers to the X-axis, y refers to the Y-axis and type refers to the chart type but the axes values are stored in a data frame or itself a shared.

Example

Loading plotly package:

> library(plotly)

Consider the below data frame:

Live Demo

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

Output

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

Creating the bar plot for x:

Example

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

Output

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.

Here it is showing a warning but it is not related to our chart hence there is no problem in ignoring it.

Output

Updated on: 23-Nov-2020

303 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements