[英]How to Arrange the x axis on a Plotly Bar Chart with R
请确定如何安排 Plotly 条形图上 x 轴的顺序。
我正在使用带有钻石数据集的玩具示例,并尝试通过增加平均深度来安排清晰度。 我对 ggplot 非常熟悉,但对 plotly 却很陌生。 我在网上看到了一些关于这个问题的帖子,但似乎没有一个是确定的。 渲染 plot 后,我认为净度类别的排序确实正确,将鼠标悬停以获取 label 值会表明这一点,但是这些值(61.3、61.3、61.4、61.6、61.7、61.6、61.7、61.6、61.7、61.91.8、组)不明显 map 到 y 轴,范围为 0 到 16k。 这让我很困惑。 我不想使用 ggplotly 包装器,我正在寻找 plotly 解决方案,谢谢。
library(tidyverse)
library(plotly)
set.seed(321)
my_diamonds <- ggplot2::diamonds %>%
slice(sample(nrow(.), 1000))
my_diamonds %>%
group_by(clarity) %>%
mutate(mean_depth = mean(depth)) %>%
ungroup() %>%
plot_ly(
data = .
, x = ~ clarity
, y = ~ mean_depth
) %>%
layout(
title = "Mean Depth for each Clarity Category"
, xaxis = list(categoryorder = "array", categoryarray = ~ reorder(clarity, mean_depth))
)
在绘图之前数据尚未完全处理。 一旦您 select 获得了适当的信息,您的代码就可以很好地绘制。 我从mean_depth
中减去了 61,因为这样更容易看到柱状顺序。 您可以删除减法。 尝试这个
set.seed(321)
my_diamonds <- ggplot2::diamonds %>%
slice(sample(nrow(.), 1000))
my_diamonds %>%
group_by(clarity) %>%
mutate(mean_depth = mean(depth)-61) %>%
distinct(clarity,mean_depth) %>% arrange(mean_depth) %>%
ungroup() %>%
plot_ly(
data = .
, x = ~ clarity
, y = ~ mean_depth
) %>%
layout(
title = "Mean Depth for each Clarity Category"
, xaxis = list(categoryorder = "array", categoryarray = ~ reorder(clarity, mean_depth))
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.