使用此代码:
data.frame(Group = LETTERS[1:6],
Value = c(10,20,30,40,50,60),
Shade = c("A","A","B","B","C","C"),
UCI = c(1,2,3,4,5,6),
LCI = c(1,2,3,4,5,6)) |>
plot_ly(x =~Group,
y=~Value,
color = ~Shade,
type = 'bar',
error_y=~list(type="data",
symmetric = FALSE,
array=UCI,
arrayminus = LCI))
但是,如果我使用其他一些字符串来定义颜色,则误差线与数据不对应
data.frame(Group = LETTERS[1:6],
Value = c(10,20,30,40,50,60),
Shade = c("similar","similar","higher","higher","lower","lower"),
UCI = c(1,2,3,4,5,6),
LCI = c(1,2,3,4,5,6)) |>
plot_ly(x =~Group,
y=~Value,
color = ~Shade,
type = 'bar',
error_y=~list(type="data",
symmetric = FALSE,
array=UCI,
arrayminus = LCI))
请注意,工具提示表明 C 列上的误差线现在为 +/- 1,而不是 3。
有人可以解释一下这里发生了什么以及如何解决它吗?
我想你可能需要一个有序因子,因为组是按字母顺序排序的:
data.frame(Group = LETTERS[1:6],
Value = c(10,20,30,40,50,60),
Shade = factor(c("similar","similar","higher","higher","lower","lower"),
ordered=T, levels = c("similar","higher","lower")),
UCI = c(1,2,3,4,5,6),
LCI = c(1,2,3,4,5,6)) |>
plot_ly(x =~Group,
y=~Value,
color = ~Shade,
type = 'bar',
error_y=~list(type="data",
symmetric = FALSE,
array=UCI,
arrayminus = LCI))
或通过添加前缀来解决此排序问题:
data.frame(Group = LETTERS[1:6],
Value = c(10,20,30,40,50,60),
Shade = c("A. similar","A. similar","B. higher","B. higher","C. lower","C. lower"),
UCI = c(1,2,3,4,5,6),
LCI = c(1,2,3,4,5,6)) |>
plot_ly(x =~Group,
y=~Value,
color = ~Shade,
type = 'bar',
error_y=~list(type="data",
symmetric = FALSE,
array=UCI,
arrayminus = LCI))