Plotly 过滤器不适用于转换
Plotly filter not working with transforms
我制作了一个经过过滤的堆叠条形图,但它显示了指定过滤器下不存在的条形的空白区域。
这是我的代码:
event <- c("eve1","eve2","eve3","eve1","eve3","eve4","eve3","eve1","eve1","eve2","eve3","eve4","eve2","eve1")
year <-c("2017","2017","2017","2018","2018","2019","2019","2019","2020","2020","Total","Total","Total","Total")
cat1 <- c(20,14,25,64,0,5,34,2,14,6,78,0,0,23)
cat2 <- c(0,4,0,4,3,9,0,2,4,12,8,42,31,2)
eve <- data.frame(event,year,cat1,cat2)
fig <- plot_ly(eve, x = ~event, y = ~cat1, type = 'bar', name = 'cat1',
transforms = list(list(type = 'filter',target = ~year, operation = '=',value = eve$year))) %>%
add_trace(y = ~cat2, name = 'cat2')%>%
layout(yaxis = list(title = 'count'), barmode = 'stack',
updatemenus = list(list(type = 'dropdown',active = 0,buttons =
list(list(method = "restyle",
args = list("transforms[0].value", unique(eve$year)[1]),
label = unique(eve$year)[1]),
list(method = "restyle",
args = list("transforms[0].value", unique(eve$year)[2]),
label = unique(eve$year)[2]),
list(method = "restyle",
args = list("transforms[0].value", unique(eve$year)[3]),
label = unique(eve$year)[3]),
list(method = "restyle",
args = list("transforms[0].value", unique(eve$year)[4]),
label = unique(eve$year)[4]),
list(method = "restyle",
args = list("transforms[0].value", unique(eve$year)[5]),
label = unique(eve$year)[5])
))))
fig
为什么数据中没有出现“eve2”却出现在 2019 年?
几个月后找到解决方案,将其发布以防其他人遇到类似问题。
布局下需要指定categoryorder和categoryarray。这会阻止 Plotly 对可能干扰过滤器的转换元素的数据帧进行自动排序。
这是更正后的代码:
event <- c("eve1","eve2","eve3","eve1","eve3","eve4","eve3","eve1","eve1","eve2","eve3","eve4","eve2","eve1")
year <-c("2017","2017","2017","2018","2018","2019","2019","2019","2020","2020","Total","Total","Total","Total")
cat1 <- c(20,14,25,64,0,5,34,2,14,6,78,0,0,23)
cat2 <- c(0,4,0,4,3,9,0,2,4,12,8,42,31,2)
eve <- data.frame(event,year,cat1,cat2)
fig <- plot_ly(eve, x = ~event, y = ~cat1, type = 'bar', name = 'cat1',
transforms = list(list(type = 'filter',target = ~year, operation = '=',value = eve$year))) %>%
add_trace(y = ~cat2, name = 'cat2')%>%
layout(yaxis = list(title = 'count'),xaxis=list(categoryorder = "array",
categoryarray = eve$year), barmode = 'stack',
updatemenus = list(list(type = 'dropdown',active = 0,buttons =
list(list(method = "restyle",
args = list("transforms[0].value", unique(eve$year)[1]),
label = unique(eve$year)[1]),
list(method = "restyle",
args = list("transforms[0].value", unique(eve$year)[2]),
label = unique(eve$year)[2]),
list(method = "restyle",
args = list("transforms[0].value", unique(eve$year)[3]),
label = unique(eve$year)[3]),
list(method = "restyle",
args = list("transforms[0].value", unique(eve$year)[4]),
label = unique(eve$year)[4]),
list(method = "restyle",
args = list("transforms[0].value", unique(eve$year)[5]),
label = unique(eve$year)[5])
))))
fig
我制作了一个经过过滤的堆叠条形图,但它显示了指定过滤器下不存在的条形的空白区域。
这是我的代码:
event <- c("eve1","eve2","eve3","eve1","eve3","eve4","eve3","eve1","eve1","eve2","eve3","eve4","eve2","eve1")
year <-c("2017","2017","2017","2018","2018","2019","2019","2019","2020","2020","Total","Total","Total","Total")
cat1 <- c(20,14,25,64,0,5,34,2,14,6,78,0,0,23)
cat2 <- c(0,4,0,4,3,9,0,2,4,12,8,42,31,2)
eve <- data.frame(event,year,cat1,cat2)
fig <- plot_ly(eve, x = ~event, y = ~cat1, type = 'bar', name = 'cat1',
transforms = list(list(type = 'filter',target = ~year, operation = '=',value = eve$year))) %>%
add_trace(y = ~cat2, name = 'cat2')%>%
layout(yaxis = list(title = 'count'), barmode = 'stack',
updatemenus = list(list(type = 'dropdown',active = 0,buttons =
list(list(method = "restyle",
args = list("transforms[0].value", unique(eve$year)[1]),
label = unique(eve$year)[1]),
list(method = "restyle",
args = list("transforms[0].value", unique(eve$year)[2]),
label = unique(eve$year)[2]),
list(method = "restyle",
args = list("transforms[0].value", unique(eve$year)[3]),
label = unique(eve$year)[3]),
list(method = "restyle",
args = list("transforms[0].value", unique(eve$year)[4]),
label = unique(eve$year)[4]),
list(method = "restyle",
args = list("transforms[0].value", unique(eve$year)[5]),
label = unique(eve$year)[5])
))))
fig
为什么数据中没有出现“eve2”却出现在 2019 年?
几个月后找到解决方案,将其发布以防其他人遇到类似问题。
布局下需要指定categoryorder和categoryarray。这会阻止 Plotly 对可能干扰过滤器的转换元素的数据帧进行自动排序。
这是更正后的代码:
event <- c("eve1","eve2","eve3","eve1","eve3","eve4","eve3","eve1","eve1","eve2","eve3","eve4","eve2","eve1")
year <-c("2017","2017","2017","2018","2018","2019","2019","2019","2020","2020","Total","Total","Total","Total")
cat1 <- c(20,14,25,64,0,5,34,2,14,6,78,0,0,23)
cat2 <- c(0,4,0,4,3,9,0,2,4,12,8,42,31,2)
eve <- data.frame(event,year,cat1,cat2)
fig <- plot_ly(eve, x = ~event, y = ~cat1, type = 'bar', name = 'cat1',
transforms = list(list(type = 'filter',target = ~year, operation = '=',value = eve$year))) %>%
add_trace(y = ~cat2, name = 'cat2')%>%
layout(yaxis = list(title = 'count'),xaxis=list(categoryorder = "array",
categoryarray = eve$year), barmode = 'stack',
updatemenus = list(list(type = 'dropdown',active = 0,buttons =
list(list(method = "restyle",
args = list("transforms[0].value", unique(eve$year)[1]),
label = unique(eve$year)[1]),
list(method = "restyle",
args = list("transforms[0].value", unique(eve$year)[2]),
label = unique(eve$year)[2]),
list(method = "restyle",
args = list("transforms[0].value", unique(eve$year)[3]),
label = unique(eve$year)[3]),
list(method = "restyle",
args = list("transforms[0].value", unique(eve$year)[4]),
label = unique(eve$year)[4]),
list(method = "restyle",
args = list("transforms[0].value", unique(eve$year)[5]),
label = unique(eve$year)[5])
))))
fig