1

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
  • See also plotly.R issues #989 & #2246 Commented Jul 26, 2023 at 15:07

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.