r - 使用 R 绘图的 Restyle Buttons 将颜色映射到变量上
问题描述
我正在尝试使用在更改颜色时保持 y 轴和 x 轴动态的Restyle
按钮将颜色映射到两个变量上。当我color=~fruit
在主图中添加时,它给出了我正在寻找的结果,但是在更改变量时我失去了轴的动态。我基本上想改变与水果相关的线条的颜色。下面是数据和我用来玩它的代码。感谢您的任何帮助或提示!
图书馆
library(dplyr); library(plotly);
数据
dfake <- tibble(days = seq(1,100, by=1),
bask = seq(1,500, by=5),
fruit = c(rep("grape", 50),
rep("apple", 50)));
绘图代码
plot <- dfake %>%
plot_ly(x = ~days, y = ~bask, text = ~fruit,
type = 'scatter',
mode = 'lines',
hoverinfo = 'text',
transforms = list(
list(type = 'filter',
target = ~fruit,
operation = '=',
value = unique(dfake$fruit)[1]))) %>%
layout(updatemenus = list(
list(type = 'dropdown',
active = 1,
buttons = list(
list(method = "restyle",
args = list("transforms[0].value",
unique(dfake$fruit)[1]),
label = unique(dfake$fruit)[1]),
list(method = "restyle",
args = list("transforms[0].value",
unique(dfake$fruit)[2]),
label = unique(dfake$fruit)[2])))));
plot;
解决方案
Yes, wasn't sure of the data was in the best possible format. So, I was fiddling with this in the following manner:
- Make a dataframe, where each
Y-axis variable
goes into individual column (refer to the tidyverse philosopy). - Appending the lines layer-by-layer into plotly.
- Using
updatemenus
to get the interactive buttons & desired visibility.
#Converting into a dataframe, mutating new columns for each fruit and getting their name:
df_dfake <- as.data.frame(dfake)
df_dfake <- df_dfake %>% mutate(fruit1_bask = case_when(fruit == "grape" ~ bask),
fruit2_bask = case_when(fruit == "apple" ~ bask))
fruit1 <- unique(dfake$fruit)[1]; fruit2 <- unique(dfake$fruit)[2];
#Plotly, adding layer by layer:
fig <- df_dfake %>% plot_ly(type = 'scatter',
mode = 'lines',
hoverinfo = 'text');
fig1 <- fig %>% add_lines(x = ~days , y = ~fruit1_bask, text = ~fruit,
line=list(color="#33CFA5"));
fig2 <- fig1 %>% add_lines(x = ~days, y = ~fruit2_bask, text = ~fruit,
line=list(color="#F06A6A"));
fig2;
fig2
Now, updatemenus
component, to make the interactive buttons
updatemenus <- list(
list(
active = -1,
type= 'buttons',
buttons = list(
list(
label = unique(dfake$fruit)[1],
method = "update",
args = list(list(visible = c(FALSE, TRUE)),# this defines visibility on click
list(title = "fruit1",
annotations = list(c(), df_dfake$fruit1_bask)))),
list(
label = unique(dfake$fruit)[2],
method = "update",
args = list(list(visible = c(T, F)),# this defines visibility on click
list(title = "fruit2",
annotations = list(c(), df_dfake$fruit2_bask))))
)
)
)
fig3 <- fig2 %>% layout(title = "Apples & Oranges", showlegend=FALSE,
xaxis=list(title="Days"),
yaxis=list(title="Basket"),
updatemenus=updatemenus); fig
Which results in the following graphs with interactive buttons:
推荐阅读
- ios - 在 UIActivityViewController 中仅将电子邮件+(任何特定应用程序)显示为可共享选项
- excel - 在 VBA 宏中使用 WORKDAY
- java - 带有可绘制对象的 ListView 滚动性能缓慢
- matlab - 在 Octave / Matlab 的图中为字符串添加制表符(空格)
- java - Java Apache POI - 获取单元格匹配正则表达式右侧的单元格值?
- mysql - 如何在 phpmyadmin 中声明游标?
- javascript - .then() 返回一个承诺而不是解析的数据值
- python - API 不接受来自 Python 的 JSON 数据
- python - 使用 PIL 在小部件文本上添加背景图像
- html - 按钮处于活动状态的引导导航栏 - 我无法清除它