简体   繁体   English

在 R 中使用 plotly 错开 X 轴标签

[英]stagger X-axis labels using plotly in R

Using ggplot I am able to stagger the x-axis labels using the scale_x_discrete function使用 ggplot 我可以使用 scale_x_discrete function 错开 x 轴标签

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).但是,我希望在 R 环境中的 plotly 中进行相同的编程(即不使用 ggplotly 包装函数)。 Does anyone have an idea what function I should use?有谁知道我应该使用什么 function ?

To generate this type of labeling, you can use ticktext .要生成这种类型的标签,您可以使用ticktext However, you have to include tickvals for it to work.但是,您必须包含tickvals才能使其工作。 Other than the default colors, I generated a Plotly version that is extremely similar to the ggplot graph in your question.除了默认的 colors 之外,我生成了一个 Plotly 版本,它与您问题中的ggplot图非常相似。

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; ggplot在左边; the plotly is on the right. plotly位于右侧。

在此处输入图像描述 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM