stagger X-axis labels using plotly in R

Viewed 28

Using ggplot I am able to stagger the x-axis labels using the scale_x_discrete function

library(tidyverse)
tibble(A = factor(LETTERS[1:5]), B = c(abs(rnorm(5)))) %>% 
  ggplot(aes(x=A, y=B))+
  geom_bar(stat="identity")+
  scale_x_discrete(guide = guide_axis(n.dodge = 2))

However, I wish to do the same programming in plotly within the R environment (ie. not using the ggplotly wrapper function). Does anyone have an idea what function I should use?

1 Answers

To generate this type of labeling, you can use ticktext. However, you have to include tickvals for it to work. Other than the default colors, I generated a Plotly version that is extremely similar to the ggplot graph in your question.

set.seed(23)
df1 <- tibble(A = factor(LETTERS[1:5]), B = c(abs(rnorm(5))))

plot_ly(type = "bar", data = df1, x = ~A, y = ~B) %>% 
  layout(xaxis = list(tickmode = "array",
                      tickvals = df1$A,
                      ticktext = c("A", "\nB", "C", "\nD", "E"),
                      ticklen = 5, ticks = "outside"))

The ggplot is on the left; the plotly is on the right.

enter image description here enter image description here

Related