r - 如何让 plotly::ggplotly 不将 boxplot x 轴标签转换为数值
问题描述
我想了解如何plotly::ggplotly
保留x 轴标签而不将它们转换为数值?data.frame
我将 my中用于标签的列转换x-axis
为字符,以确保它们不是因素。会发生以下情况:
- 下面的第一个图 (
ggplot
) 显示了正确显示的 x 轴标签。 - 使用
ggplot
上面的对象,plotly::ggplotly
被调用,结果是 x 轴标签显示不正确(作为数值)。 - 第三个图显示了
plotly
直接使用(即不使用plotly::ggplotly
)绘制的相同数据;这将显示正确的结果。
我想知道如何plotly::ggplotly
正确显示 x 轴标签?
我已经对此进行了一些搜索,但没有找到任何东西。
以下是一些关键版本(软件包、操作系统等):
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 轴标签
解决方案
推荐阅读
- android - add inputt textfield when selected item dropdown in flutter
- sql - 运行平均但在增加连续停止时重置
- java - 如何解决 AdjustCapsLockLEDForKeyTransitionHandling?
- python - 我需要返回在 python tkinter 中单击的两个菜单选项的选项
- angular - 如何使用 Renderer2 将组件或元素移动到 Angular Material Datepicker Calendar 弹出窗口中?
- reactjs - 将博客添加到网站时如何使用富文本或降价
- .net - Perfview 并从固定句柄获取更详细的内存视图
- arrays - 如何在 Ruby on Rails 中更新数组的第一个元素?
- java - 我的程序有一个Java。网。套接字异常:连接重置
- javascript - 带数据的 jQuery .load 仅在第一次工作