如何用负数绘制 R Plotly 条形图
How to plot R Plotly bar chart with negative numbers
我正在准备一个 R Plotly 水平条形图。图表值包含负数和正数,我希望绘制类似于下面的 ggplot 示例(“ggplot 1”)。然而,一些 colours/values 在 Plotly 图表('plotly 2')中是不可见的,无论如何不是通过它们指定的颜色。例如,指标 1 的红色未显示,但可以通过将光标悬停在图表中的条上看到它。
如果值都是正数(‘plotly 1’),我可以获得所有颜色来显示,但是这与我拥有的真实数据不准确。我需要做些什么才能使“plotly 2”看起来像“ggplot 1”吗?
我不是在寻找 ggplotly 包装器解决方案,我也不打算为每个指标引入新的硬编码跟踪,因为生产数据中的指标数量可能会有所不同。提前致谢。
library(tidyverse)
library(plotly)
group_color_df <- tibble(
group = 'group_1'
, metric = paste0('metric_', 1:9)
, value_wt_negs = c(-0.9, 0.5, -0.5, 0.6, -0.1, 0.1, 0.7, 0.1, 0.1)
, value = c(0.9, 0.5, 0.5, 0.6, 0.1, 0.1, 0.7, 0.1, 0.1)
, met_color = c('#ff0000','#ddff00','#1eff00','#a800fc','#00eaff','#0400ff','#ff6f00','#ff00ff','#f5f3f0')
, real_col = c('red', 'yellow', 'green', 'purple', 'lightblue', 'blue', 'orange', 'pink', 'grey'
)
)
group_color_df %>%
ggplot(aes(x = group, y = value_wt_negs, fill = metric)) +
geom_col(width = 0.4) +
coord_flip() +
theme_minimal() +
scale_fill_manual(values = group_color_df$met_color) +
ggtitle('ggplot 1')
group_color_df %>%
plot_ly(
x = ~value
, y = ~group
, type = 'bar'
, orientation = 'h'
, color = ~metric
, colors = ~met_color
) %>%
layout(
bargap = 0.8
, barmode = 'stack'
, title = 'plotly 1'
)
group_color_df %>%
plot_ly(
x = ~value_wt_negs
, y = ~group
, type = 'bar'
, orientation = 'h'
, color = ~metric
, colors = ~met_color
) %>%
layout(
bargap = 0.8
, barmode = 'stack'
, title = 'plotly 2'
)
您应该在布局中使用 barmode = 'relative'
。您可以使用以下代码:
library(tidyverse)
library(plotly)
group_color_df %>%
plot_ly(
x = ~value_wt_negs
, y = ~group
, type = 'bar'
, orientation = 'h'
, color = ~metric
, colors = ~met_color
) %>%
layout(
bargap = 0.8
, barmode = 'relative'
, title = 'plotly 2'
)
输出:
如您所见,图中显示了所有颜色。
我正在准备一个 R Plotly 水平条形图。图表值包含负数和正数,我希望绘制类似于下面的 ggplot 示例(“ggplot 1”)。然而,一些 colours/values 在 Plotly 图表('plotly 2')中是不可见的,无论如何不是通过它们指定的颜色。例如,指标 1 的红色未显示,但可以通过将光标悬停在图表中的条上看到它。
如果值都是正数(‘plotly 1’),我可以获得所有颜色来显示,但是这与我拥有的真实数据不准确。我需要做些什么才能使“plotly 2”看起来像“ggplot 1”吗?
我不是在寻找 ggplotly 包装器解决方案,我也不打算为每个指标引入新的硬编码跟踪,因为生产数据中的指标数量可能会有所不同。提前致谢。
library(tidyverse)
library(plotly)
group_color_df <- tibble(
group = 'group_1'
, metric = paste0('metric_', 1:9)
, value_wt_negs = c(-0.9, 0.5, -0.5, 0.6, -0.1, 0.1, 0.7, 0.1, 0.1)
, value = c(0.9, 0.5, 0.5, 0.6, 0.1, 0.1, 0.7, 0.1, 0.1)
, met_color = c('#ff0000','#ddff00','#1eff00','#a800fc','#00eaff','#0400ff','#ff6f00','#ff00ff','#f5f3f0')
, real_col = c('red', 'yellow', 'green', 'purple', 'lightblue', 'blue', 'orange', 'pink', 'grey'
)
)
group_color_df %>%
ggplot(aes(x = group, y = value_wt_negs, fill = metric)) +
geom_col(width = 0.4) +
coord_flip() +
theme_minimal() +
scale_fill_manual(values = group_color_df$met_color) +
ggtitle('ggplot 1')
group_color_df %>%
plot_ly(
x = ~value
, y = ~group
, type = 'bar'
, orientation = 'h'
, color = ~metric
, colors = ~met_color
) %>%
layout(
bargap = 0.8
, barmode = 'stack'
, title = 'plotly 1'
)
group_color_df %>%
plot_ly(
x = ~value_wt_negs
, y = ~group
, type = 'bar'
, orientation = 'h'
, color = ~metric
, colors = ~met_color
) %>%
layout(
bargap = 0.8
, barmode = 'stack'
, title = 'plotly 2'
)
您应该在布局中使用 barmode = 'relative'
。您可以使用以下代码:
library(tidyverse)
library(plotly)
group_color_df %>%
plot_ly(
x = ~value_wt_negs
, y = ~group
, type = 'bar'
, orientation = 'h'
, color = ~metric
, colors = ~met_color
) %>%
layout(
bargap = 0.8
, barmode = 'relative'
, title = 'plotly 2'
)
输出:
如您所见,图中显示了所有颜色。