R 中 plotly 的多个过滤器和下拉菜单
Multiple filters and drop down menus for plotly in R
我有一个瀑布图,我想要两个下拉筛选框(一个工作正常,但第二个我遇到了问题)。最初似乎给出了正确的图表,但当我单击下拉框时,图表要么消失,要么显示给定选择的错误值。
我的数据:
my_data <- structure(list(Cohort = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L), .Label = c("2003", "2006", "2009"), class = "factor"), ind = structure(c(1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L,
4L, 4L, 4L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L,
4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 3L, 4L, 4L, 4L, 4L, 4L), .Label = c("Aspiration", "Gender",
"Low_SES", "Parent_edu"), class = "factor"), Category = structure(c(1L,
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L,
3L, 4L, 5L, 1L, 2L, 3L, 5L, 1L, 2L, 3L, 5L, 1L, 2L, 3L, 5L, 1L,
2L, 3L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L,
4L, 5L, 1L, 2L, 3L, 4L, 5L), .Label = c("Aspiration", "Closest_uni_distance_g2",
"Educ_occupation_quartile", "Family_structure", "Gender"), class = "factor"),
roll = c(0.0750803710978645, 0.076436441980399, 0.0925424424098545,
0.0950458976596428, 0.0917674134165381, -0.345423918218276,
-0.347660661701295, -0.356519150007063, -0.360758049199678,
-0.372940308381271, -0.0453779875920708, -0.0465416728725297,
-0.047252123130504, -0.0463144654922128, -0.127690827133109,
-0.0607795367471432, -0.0598934089046648, -0.0814516006011201,
-0.0836894858818226, -0.0840952435220562, 0.0874835324452846,
0.0912364919757644, 0.105777226141928, 0.106963827631275,
0.356379384253744, 0.36207727951625, 0.370840500815585, 0.384840489694092,
-0.0475543917831162, -0.0478582465019799, -0.0487284614746369,
-0.13308503060702, 0.0441449522093825, 0.0505563180773581,
0.0658006236043333, 0.0683279904326434, 0.07261902634577,
0.0778692418494206, 0.0872004492519247, 0.0850098679360723,
0.0838837730292262, 0.32446212681395, 0.328231007457026,
0.335365279883835, 0.332040073705212, 0.351959150036042,
0.0536891922354224, 0.0544669559094648, 0.0551853855166302,
0.0555352735715207, 0.133232816312245, 0.0519405537106114,
0.0591010371260817, 0.0794756801714813, 0.0793173156131281,
0.0777547869099181), base = c(0, 0.0750803710978645, 0.076436441980399,
0.0925424424098545, 0.0950458976596428, 0, -0.345423918218276,
-0.347660661701295, -0.356519150007063, -0.360758049199678,
0, -0.0453779875920708, -0.0465416728725297, -0.047252123130504,
-0.0463144654922128, 0, -0.0607795367471432, -0.0598934089046648,
-0.0814516006011201, -0.0836894858818226, 0, 0.0874835324452846,
0.0912364919757644, 0.105777226141928, 0, 0.356379384253744,
0.36207727951625, 0.370840500815585, 0, -0.0475543917831162,
-0.0478582465019799, -0.0487284614746369, 0, 0.0441449522093825,
0.0505563180773581, 0.0658006236043333, 0, 0.07261902634577,
0.0778692418494206, 0.0872004492519247, 0.0850098679360723,
0, 0.32446212681395, 0.328231007457026, 0.335365279883835,
0.332040073705212, 0, 0.0536891922354224, 0.0544669559094648,
0.0551853855166302, 0.0555352735715207, 0, 0.0519405537106114,
0.0591010371260817, 0.0794756801714813, 0.0793173156131281
), roll_minus_base = c(0.0750803710978645, 0.00135607088253455,
0.0161060004294555, 0.00250345524978828, -0.00327848424310467,
-0.345423918218276, -0.0022367434830185, -0.00885848830576863,
-0.00423889919261472, -0.0121822591815926, -0.0453779875920708,
-0.00116368528045896, -0.000710450257974227, 0.000937657638291178,
-0.0813763616408965, -0.0607795367471432, 0.000886127842478407,
-0.0215581916964553, -0.00223788528070247, -0.000405757640233637,
0.0874835324452846, 0.00375295953047973, 0.0145407341661632,
0.00118660148934696, 0.356379384253744, 0.00569789526250536,
0.00876322129933566, 0.0139999888785068, -0.0475543917831162,
-0.000303854718863766, -0.000870214972656977, -0.0843565691323827,
0.0441449522093825, 0.00641136586797556, 0.0152443055269752,
0.00252736682831006, 0.07261902634577, 0.00525021550365058,
0.00933120740250415, -0.00219058131585249, -0.00112609490684605,
0.32446212681395, 0.00376888064307568, 0.00713427242680886,
-0.00332520617862214, 0.0199190763308294, 0.0536891922354224,
0.000777763674042455, 0.000718429607165359, 0.000349888054890515,
0.0776975427407239, 0.0519405537106114, 0.00716048341547032,
0.0203746430453996, -0.000158364558353163, -0.00156252870321001
), Colour = c("rgba(120, 162, 47, 0.7)", "rgba(120, 162, 47, 0.7)",
"rgba(120, 162, 47, 0.7)", "rgba(120, 162, 47, 0.7)", "rgba(255, 0, 0, 0.7)",
"rgba(255, 0, 0, 0.7)", "rgba(255, 0, 0, 0.7)", "rgba(255, 0, 0, 0.7)",
"rgba(255, 0, 0, 0.7)", "rgba(255, 0, 0, 0.7)", "rgba(255, 0, 0, 0.7)",
"rgba(255, 0, 0, 0.7)", "rgba(255, 0, 0, 0.7)", "rgba(120, 162, 47, 0.7)",
"rgba(255, 0, 0, 0.7)", "rgba(255, 0, 0, 0.7)", "rgba(120, 162, 47, 0.7)",
"rgba(255, 0, 0, 0.7)", "rgba(255, 0, 0, 0.7)", "rgba(255, 0, 0, 0.7)",
"rgba(120, 162, 47, 0.7)", "rgba(120, 162, 47, 0.7)", "rgba(120, 162, 47, 0.7)",
"rgba(120, 162, 47, 0.7)", "rgba(120, 162, 47, 0.7)", "rgba(120, 162, 47, 0.7)",
"rgba(120, 162, 47, 0.7)", "rgba(120, 162, 47, 0.7)", "rgba(255, 0, 0, 0.7)",
"rgba(255, 0, 0, 0.7)", "rgba(255, 0, 0, 0.7)", "rgba(255, 0, 0, 0.7)",
"rgba(120, 162, 47, 0.7)", "rgba(120, 162, 47, 0.7)", "rgba(120, 162, 47, 0.7)",
"rgba(120, 162, 47, 0.7)", "rgba(120, 162, 47, 0.7)", "rgba(120, 162, 47, 0.7)",
"rgba(120, 162, 47, 0.7)", "rgba(255, 0, 0, 0.7)", "rgba(255, 0, 0, 0.7)",
"rgba(120, 162, 47, 0.7)", "rgba(120, 162, 47, 0.7)", "rgba(120, 162, 47, 0.7)",
"rgba(255, 0, 0, 0.7)", "rgba(120, 162, 47, 0.7)", "rgba(120, 162, 47, 0.7)",
"rgba(120, 162, 47, 0.7)", "rgba(120, 162, 47, 0.7)", "rgba(120, 162, 47, 0.7)",
"rgba(120, 162, 47, 0.7)", "rgba(120, 162, 47, 0.7)", "rgba(120, 162, 47, 0.7)",
"rgba(120, 162, 47, 0.7)", "rgba(255, 0, 0, 0.7)", "rgba(255, 0, 0, 0.7)"
)), row.names = c(NA, -56L), class = "data.frame")
我创建图表的代码:
p <- plot_ly(my_data, x = ~Category, y = ~base, type = 'bar',marker=list(color = 'rgba(1,1,1, 0.0)'),
transforms = list(list(
type = 'filter',
target = ~Cohort,
operation = '=',
value = unique(my_data$Cohort)[1]),
list(
type = 'filter',
target = ~ind,
operation = '=',
value = unique(my_data$ind)[1]))) %>%
add_trace(y = ~roll_minus_base, marker=list(color = ~Colour,
line = list(color = ~Colour,width = 2))) %>%
layout(title = 'Decomposition',
xaxis = list(title = ""),
yaxis = list(title = "",
range = c(floor(min(my_data$roll)*10)/10, ceiling(max(my_data$roll)*10)/10)),
barmode = 'stack',
paper_bgcolor = 'rgba(245, 246, 249, 1)',
plot_bgcolor = 'rgba(245, 246, 249, 1)',
showlegend = FALSE,
updatemenus = list(
list(
type = 'dropdown',
active = 0,
buttons = apply(as.data.frame(unique(my_data$ind)), 1,
function(x) list(method = 'restyle',args = list('transforms[2].value',x),label = x))),
list(
type = 'dropdown',
active = 0,
buttons = apply(as.data.frame(unique(my_data$Cohort)), 1,
function(x) list(method = 'restyle',args = list('transforms[1].value',x),label = x)))
))
p
没有大问题,下拉框只是引用了不正确的转换。
- $ind 下拉菜单应引用 'transforms[1].value'
- $cohort 下拉列表应引用 'transforms[0].value'
我对 plotly 很陌生,但看起来转换有一个 0:n 列表索引,而不是像你在 R 中可能习惯的 1:n 列表索引。
我有一个瀑布图,我想要两个下拉筛选框(一个工作正常,但第二个我遇到了问题)。最初似乎给出了正确的图表,但当我单击下拉框时,图表要么消失,要么显示给定选择的错误值。
我的数据:
my_data <- structure(list(Cohort = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L), .Label = c("2003", "2006", "2009"), class = "factor"), ind = structure(c(1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L,
4L, 4L, 4L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L,
4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 3L, 4L, 4L, 4L, 4L, 4L), .Label = c("Aspiration", "Gender",
"Low_SES", "Parent_edu"), class = "factor"), Category = structure(c(1L,
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L,
3L, 4L, 5L, 1L, 2L, 3L, 5L, 1L, 2L, 3L, 5L, 1L, 2L, 3L, 5L, 1L,
2L, 3L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L,
4L, 5L, 1L, 2L, 3L, 4L, 5L), .Label = c("Aspiration", "Closest_uni_distance_g2",
"Educ_occupation_quartile", "Family_structure", "Gender"), class = "factor"),
roll = c(0.0750803710978645, 0.076436441980399, 0.0925424424098545,
0.0950458976596428, 0.0917674134165381, -0.345423918218276,
-0.347660661701295, -0.356519150007063, -0.360758049199678,
-0.372940308381271, -0.0453779875920708, -0.0465416728725297,
-0.047252123130504, -0.0463144654922128, -0.127690827133109,
-0.0607795367471432, -0.0598934089046648, -0.0814516006011201,
-0.0836894858818226, -0.0840952435220562, 0.0874835324452846,
0.0912364919757644, 0.105777226141928, 0.106963827631275,
0.356379384253744, 0.36207727951625, 0.370840500815585, 0.384840489694092,
-0.0475543917831162, -0.0478582465019799, -0.0487284614746369,
-0.13308503060702, 0.0441449522093825, 0.0505563180773581,
0.0658006236043333, 0.0683279904326434, 0.07261902634577,
0.0778692418494206, 0.0872004492519247, 0.0850098679360723,
0.0838837730292262, 0.32446212681395, 0.328231007457026,
0.335365279883835, 0.332040073705212, 0.351959150036042,
0.0536891922354224, 0.0544669559094648, 0.0551853855166302,
0.0555352735715207, 0.133232816312245, 0.0519405537106114,
0.0591010371260817, 0.0794756801714813, 0.0793173156131281,
0.0777547869099181), base = c(0, 0.0750803710978645, 0.076436441980399,
0.0925424424098545, 0.0950458976596428, 0, -0.345423918218276,
-0.347660661701295, -0.356519150007063, -0.360758049199678,
0, -0.0453779875920708, -0.0465416728725297, -0.047252123130504,
-0.0463144654922128, 0, -0.0607795367471432, -0.0598934089046648,
-0.0814516006011201, -0.0836894858818226, 0, 0.0874835324452846,
0.0912364919757644, 0.105777226141928, 0, 0.356379384253744,
0.36207727951625, 0.370840500815585, 0, -0.0475543917831162,
-0.0478582465019799, -0.0487284614746369, 0, 0.0441449522093825,
0.0505563180773581, 0.0658006236043333, 0, 0.07261902634577,
0.0778692418494206, 0.0872004492519247, 0.0850098679360723,
0, 0.32446212681395, 0.328231007457026, 0.335365279883835,
0.332040073705212, 0, 0.0536891922354224, 0.0544669559094648,
0.0551853855166302, 0.0555352735715207, 0, 0.0519405537106114,
0.0591010371260817, 0.0794756801714813, 0.0793173156131281
), roll_minus_base = c(0.0750803710978645, 0.00135607088253455,
0.0161060004294555, 0.00250345524978828, -0.00327848424310467,
-0.345423918218276, -0.0022367434830185, -0.00885848830576863,
-0.00423889919261472, -0.0121822591815926, -0.0453779875920708,
-0.00116368528045896, -0.000710450257974227, 0.000937657638291178,
-0.0813763616408965, -0.0607795367471432, 0.000886127842478407,
-0.0215581916964553, -0.00223788528070247, -0.000405757640233637,
0.0874835324452846, 0.00375295953047973, 0.0145407341661632,
0.00118660148934696, 0.356379384253744, 0.00569789526250536,
0.00876322129933566, 0.0139999888785068, -0.0475543917831162,
-0.000303854718863766, -0.000870214972656977, -0.0843565691323827,
0.0441449522093825, 0.00641136586797556, 0.0152443055269752,
0.00252736682831006, 0.07261902634577, 0.00525021550365058,
0.00933120740250415, -0.00219058131585249, -0.00112609490684605,
0.32446212681395, 0.00376888064307568, 0.00713427242680886,
-0.00332520617862214, 0.0199190763308294, 0.0536891922354224,
0.000777763674042455, 0.000718429607165359, 0.000349888054890515,
0.0776975427407239, 0.0519405537106114, 0.00716048341547032,
0.0203746430453996, -0.000158364558353163, -0.00156252870321001
), Colour = c("rgba(120, 162, 47, 0.7)", "rgba(120, 162, 47, 0.7)",
"rgba(120, 162, 47, 0.7)", "rgba(120, 162, 47, 0.7)", "rgba(255, 0, 0, 0.7)",
"rgba(255, 0, 0, 0.7)", "rgba(255, 0, 0, 0.7)", "rgba(255, 0, 0, 0.7)",
"rgba(255, 0, 0, 0.7)", "rgba(255, 0, 0, 0.7)", "rgba(255, 0, 0, 0.7)",
"rgba(255, 0, 0, 0.7)", "rgba(255, 0, 0, 0.7)", "rgba(120, 162, 47, 0.7)",
"rgba(255, 0, 0, 0.7)", "rgba(255, 0, 0, 0.7)", "rgba(120, 162, 47, 0.7)",
"rgba(255, 0, 0, 0.7)", "rgba(255, 0, 0, 0.7)", "rgba(255, 0, 0, 0.7)",
"rgba(120, 162, 47, 0.7)", "rgba(120, 162, 47, 0.7)", "rgba(120, 162, 47, 0.7)",
"rgba(120, 162, 47, 0.7)", "rgba(120, 162, 47, 0.7)", "rgba(120, 162, 47, 0.7)",
"rgba(120, 162, 47, 0.7)", "rgba(120, 162, 47, 0.7)", "rgba(255, 0, 0, 0.7)",
"rgba(255, 0, 0, 0.7)", "rgba(255, 0, 0, 0.7)", "rgba(255, 0, 0, 0.7)",
"rgba(120, 162, 47, 0.7)", "rgba(120, 162, 47, 0.7)", "rgba(120, 162, 47, 0.7)",
"rgba(120, 162, 47, 0.7)", "rgba(120, 162, 47, 0.7)", "rgba(120, 162, 47, 0.7)",
"rgba(120, 162, 47, 0.7)", "rgba(255, 0, 0, 0.7)", "rgba(255, 0, 0, 0.7)",
"rgba(120, 162, 47, 0.7)", "rgba(120, 162, 47, 0.7)", "rgba(120, 162, 47, 0.7)",
"rgba(255, 0, 0, 0.7)", "rgba(120, 162, 47, 0.7)", "rgba(120, 162, 47, 0.7)",
"rgba(120, 162, 47, 0.7)", "rgba(120, 162, 47, 0.7)", "rgba(120, 162, 47, 0.7)",
"rgba(120, 162, 47, 0.7)", "rgba(120, 162, 47, 0.7)", "rgba(120, 162, 47, 0.7)",
"rgba(120, 162, 47, 0.7)", "rgba(255, 0, 0, 0.7)", "rgba(255, 0, 0, 0.7)"
)), row.names = c(NA, -56L), class = "data.frame")
我创建图表的代码:
p <- plot_ly(my_data, x = ~Category, y = ~base, type = 'bar',marker=list(color = 'rgba(1,1,1, 0.0)'),
transforms = list(list(
type = 'filter',
target = ~Cohort,
operation = '=',
value = unique(my_data$Cohort)[1]),
list(
type = 'filter',
target = ~ind,
operation = '=',
value = unique(my_data$ind)[1]))) %>%
add_trace(y = ~roll_minus_base, marker=list(color = ~Colour,
line = list(color = ~Colour,width = 2))) %>%
layout(title = 'Decomposition',
xaxis = list(title = ""),
yaxis = list(title = "",
range = c(floor(min(my_data$roll)*10)/10, ceiling(max(my_data$roll)*10)/10)),
barmode = 'stack',
paper_bgcolor = 'rgba(245, 246, 249, 1)',
plot_bgcolor = 'rgba(245, 246, 249, 1)',
showlegend = FALSE,
updatemenus = list(
list(
type = 'dropdown',
active = 0,
buttons = apply(as.data.frame(unique(my_data$ind)), 1,
function(x) list(method = 'restyle',args = list('transforms[2].value',x),label = x))),
list(
type = 'dropdown',
active = 0,
buttons = apply(as.data.frame(unique(my_data$Cohort)), 1,
function(x) list(method = 'restyle',args = list('transforms[1].value',x),label = x)))
))
p
没有大问题,下拉框只是引用了不正确的转换。
- $ind 下拉菜单应引用 'transforms[1].value'
- $cohort 下拉列表应引用 'transforms[0].value'
我对 plotly 很陌生,但看起来转换有一个 0:n 列表索引,而不是像你在 R 中可能习惯的 1:n 列表索引。