如何让 plotly::ggplotly 不将箱线图 x 轴标签转换为数值

How to get plotly::ggplotly to not convert boxplot x-axis labels to numeric values

我想了解如何让 plotly::ggplotly 保留 x 轴 标签而不是将它们转换为数值?我将 data.frame 中用于 x-axis 标签的列转换为字符,以确保它们 不是 因素。发生的情况如下:

我想知道如何让 plotly::ggplotly 正确显示 x 轴标签?

我对此进行了一些搜索,但没有找到任何东西。

以下是一些关键版本(软件包、OS 等):

R version 3.6.0 (2019-04-26)
Platform: x86_64-pc-linux-gnu (64-bit)

[1] plotly_4.9.0
[1] ggplot2_3.3.0

更正 X 轴标签

library(dplyr)
### Convert "Species" column to character (i.e. ensure it is NOT a factor).
iris.v2 <- iris %>% dplyr::mutate(Species = as.character(Species))
str(iris.v2)

### Shows that Species is a "chr" type.
# 'data.frame': 150 obs. of  5 variables:
# $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
# $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
# $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
# $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
# $ Species     : chr  "setosa" "setosa" "setosa" "setosa" ...

### Correct x-axis labeling: Plot data using ggplot.
plt.iris <-
  iris.v2 %>%
  ggplot2::ggplot(aes(as.character(Species), Petal.Width)) +
  ggplot2::geom_boxplot()
plt.iris

X 轴标签不正确

### Incorrect x-axis labeling: plot above plot with ggplotly.
pltly.iris <-
  plotly::ggplotly(plt.iris)
pltly.iris

### Correct x-axis labeling: Plot data using plotly directly.
native.pltly.iris <-
  plotly::plot_ly(iris.v2, x = ~Species, y = ~Petal.Width, type = "box")
native.pltly.iris

更正 X 轴标签

问题出在plotly的版本上;从 plotly_4.9.0 更新到 plotly_4.9.2.1 解决了这个问题。 4.9.0 之后的任何 plotly 版本都可能解决这个问题,但我没有测试过,我只能确认 4.9.2.1 为我解决了这个问题。