R plotly 条形图 positive/negative 值不同颜色第二轴
R plotly bar chart positive/negative values different color second axis
所以从 我试图在第二个 y 轴上制作一个 plotly 条形图,负值表示为红色,正值表示为绿色....但不知何故颜色仍然是错误的.. ..为什么?
t <- tibble("one" = c(1,2,3,4,5,6,7,8,9),
"two" = c(-5,6,9,-8,7,-1,5,2,1),
"three" = c(2,5,6,9,8,7,8,4,5))
Plot <-
plot_ly(t, x = ~one, y = ~three, type = 'scatter', mode = 'lines',
name = "three", line = list(color = "#ffc107")
)
Plot <- Plot %>%
add_trace(x = ~one, y = ~two, type = 'bar', name = "two",
color = ~two < 0, colors = c("#28a745", "#dc3545"),
yaxis = 'y2'
) %>%
layout(title = "test",
xaxis = list(titel = "one"),
yaxis = list(side = 'left', title = 'two',
showgrid = F, zeroline = F,
showline = T),
yaxis2 = list(side = 'right', overlaying = "y",
title = 'three',
showgrid = F, zeroline = F,
showline = T))
如果您不阻止,plot_ly()
调用中 colors
的定义(在您的示例中为 NULL
)将在 add_trace()
中回收。
请检查以下内容:
library(plotly)
library(dplyr)
t <- tibble("one" = c(1,2,3,4,5,6,7,8,9),
"two" = c(-5,6,9,-8,7,-1,5,2,1),
"three" = c(2,5,6,9,8,7,8,4,5))
# 'bar' objects don't have these attributes: 'mode', 'line'
Plot <- plot_ly(t, x = ~one, y = ~two, type = 'bar', name = ~ifelse(two < 0, yes = "two < 0", no = "two > 0"),
color = ~two < 0, colors = c("#28a745", "#dc3545"),
yaxis = 'y'
) %>% add_trace(t, x = ~one, y = ~three, type = 'scatter', mode = 'lines',
name = "three", line = list(color = "#ffc107"), color = NULL, yaxis = "y2"
) %>% layout(title = "test",
xaxis = list(titel = "one"),
yaxis = list(side = 'right',
title = 'three',
showgrid = F, zeroline = F,
showline = T),
yaxis2 = list(side = 'left', title = 'two',
showgrid = F, zeroline = F,
showline = T, overlaying = "y"))
Plot
按以下要求编辑:
library(plotly)
library(dplyr)
t <- tibble("one" = c(1,2,3,4,5,6,7,8,9),
"two" = c(-5,6,9,-8,7,-1,5,2,1),
"three" = c(2,5,6,9,8,7,8,4,5))
# 'bar' objects don't have these attributes: 'mode', 'line'
Plot <- plot_ly(t, x = ~one, y = ~three, type = 'scatter', mode = 'lines',
name = "three", line = list(color = "#ffc107"), color = NULL, yaxis = "y") %>%
add_trace(t, x = ~one, y = ~two, type = 'bar', mode = NULL, line = NULL, name = ~ifelse(two < 0, yes = "two < 0", no = "two > 0"),
color = ~two < 0, colors = c("#28a745", "#dc3545"),
yaxis = 'y2') %>%
layout(title = "test",
xaxis = list(titel = "one"),
yaxis = list(side = 'right',
title = 'three',
showgrid = F, zeroline = F,
showline = T),
yaxis2 = list(side = 'left', title = 'two',
showgrid = F, zeroline = F,
showline = T, overlaying = "y"),
hovermode = "x unified")
Plot
所以从
t <- tibble("one" = c(1,2,3,4,5,6,7,8,9),
"two" = c(-5,6,9,-8,7,-1,5,2,1),
"three" = c(2,5,6,9,8,7,8,4,5))
Plot <-
plot_ly(t, x = ~one, y = ~three, type = 'scatter', mode = 'lines',
name = "three", line = list(color = "#ffc107")
)
Plot <- Plot %>%
add_trace(x = ~one, y = ~two, type = 'bar', name = "two",
color = ~two < 0, colors = c("#28a745", "#dc3545"),
yaxis = 'y2'
) %>%
layout(title = "test",
xaxis = list(titel = "one"),
yaxis = list(side = 'left', title = 'two',
showgrid = F, zeroline = F,
showline = T),
yaxis2 = list(side = 'right', overlaying = "y",
title = 'three',
showgrid = F, zeroline = F,
showline = T))
如果您不阻止,plot_ly()
调用中 colors
的定义(在您的示例中为 NULL
)将在 add_trace()
中回收。
请检查以下内容:
library(plotly)
library(dplyr)
t <- tibble("one" = c(1,2,3,4,5,6,7,8,9),
"two" = c(-5,6,9,-8,7,-1,5,2,1),
"three" = c(2,5,6,9,8,7,8,4,5))
# 'bar' objects don't have these attributes: 'mode', 'line'
Plot <- plot_ly(t, x = ~one, y = ~two, type = 'bar', name = ~ifelse(two < 0, yes = "two < 0", no = "two > 0"),
color = ~two < 0, colors = c("#28a745", "#dc3545"),
yaxis = 'y'
) %>% add_trace(t, x = ~one, y = ~three, type = 'scatter', mode = 'lines',
name = "three", line = list(color = "#ffc107"), color = NULL, yaxis = "y2"
) %>% layout(title = "test",
xaxis = list(titel = "one"),
yaxis = list(side = 'right',
title = 'three',
showgrid = F, zeroline = F,
showline = T),
yaxis2 = list(side = 'left', title = 'two',
showgrid = F, zeroline = F,
showline = T, overlaying = "y"))
Plot
按以下要求编辑:
library(plotly)
library(dplyr)
t <- tibble("one" = c(1,2,3,4,5,6,7,8,9),
"two" = c(-5,6,9,-8,7,-1,5,2,1),
"three" = c(2,5,6,9,8,7,8,4,5))
# 'bar' objects don't have these attributes: 'mode', 'line'
Plot <- plot_ly(t, x = ~one, y = ~three, type = 'scatter', mode = 'lines',
name = "three", line = list(color = "#ffc107"), color = NULL, yaxis = "y") %>%
add_trace(t, x = ~one, y = ~two, type = 'bar', mode = NULL, line = NULL, name = ~ifelse(two < 0, yes = "two < 0", no = "two > 0"),
color = ~two < 0, colors = c("#28a745", "#dc3545"),
yaxis = 'y2') %>%
layout(title = "test",
xaxis = list(titel = "one"),
yaxis = list(side = 'right',
title = 'three',
showgrid = F, zeroline = F,
showline = T),
yaxis2 = list(side = 'left', title = 'two',
showgrid = F, zeroline = F,
showline = T, overlaying = "y"),
hovermode = "x unified")
Plot