Using Plotly event data with a key on a ggplotly object
まだ誰も着手していません。
- 主要言語
- R
- スター
- 2.7k
- フォーク
- 641
- PR マージ指標
- 30日以内にマージされた PR はありません
説明
I am using Plotly in an R Shiny dashboard with event data so a user can click on a visualization and the linked table will filter to show just that data observation. In the reprex below, I have a plot made in ggplot that I converted to a Plotly object using ggplotly. This works fine, but when identifying observations using pointNumber in the event data, it misidentifies the observation that was clicked on because the plot and the table have different numbers of observations.
Because of this, I'm trying to identify observations using a "key" argument, but no matter how I define the key, it does not appear as a column in the event data. When this plot is made using Plotly to begin with, eliminating ggplot entirely, it works fine and the key column does appear. However, the visualization I need to use is a phylogenetic tree that has proven impossible to visualize in Plotly, which is why I started it off in ggplot. I have searched extensively, but I can't find a way to make event data work with a key using a ggplot -> plotly object. Is there a way to make this work?
### Load data
data(mtcars)
mtcars$index <- c(1:32)
data <- mtcars
# Define UI
ui <- fluidPage(
titlePanel("Dashboard"),
tags$style('.container-fluid {
background-color: #e3f2fd;
}'),
fluidRow(
column(6,
h3("Scatterplot"),
plotlyOutput("phylo_tree")),
# Metadata
column(12, h3("Metadata")),
column(12, DT::dataTableOutput("data_table"))
)
)
# Define server logic
server <- function(input, output) {
output$phylo_tree <- renderPlotly({
# Create a scatterplot using ggplot2
scatterplot <- ggplot(mtcars, aes(x = mpg, y = hp, label = rownames(mtcars))) +
geom_point() +
geom_text(nudge_y = 5, nudge_x = 1, check_overlap = TRUE) +
labs(title = "Scatterplot of mtcars Dataset",
x = "Miles Per Gallon (mpg)",
y = "Horsepower (hp)") +
theme_minimal()
scatterplot <- plotly::ggplotly(scatterplot, tooltip = "text",
source = "source1", key = ~index)
# Collect data when a user clicks on a point
event_register(scatterplot, event = "plotly_selected")
scatterplot
})
output$data_table <- DT::renderDataTable({
# Use click event data
click_data <- event_data("plotly_click", source = "source1")
observe({
print(event_data("plotly_click", source = "source1"))
})
# Check if any points are clicked
if (!is.null(click_data)) {
# Filter data based on the clicked point
data <- data[data$index %in% click_data$key,]
}
datatable(data,
rownames = FALSE,
filter = "top",
extensions = "Buttons", # Add download buttons
options = list(scrollX = TRUE, dom = "Bfrtip",
buttons = c("csv", "excel", "pdf"),
lengthMenu = list(c(10,25,50,-1),
c(10,25,50,"All"))),
selection = list(mode = "multiple", target = "row")) %>%
formatStyle(names(data),lineHeight='80%')
})
}
Edit: I solved my own problem. The key needs to be specified in the aes() argument of the ggplot code. The following ggplot code should work with the above datatable code. Note that the id variable specified here should be some unique key identifier.
# Create a scatterplot using ggplot2
scatterplot <- ggplot(mtcars, aes(x = mpg, y = hp, label = rownames(mtcars), key = id)) +
geom_point() +
geom_text(nudge_y = 5, nudge_x = 1, check_overlap = TRUE) +
labs(title = "Scatterplot of mtcars Dataset",
x = "Miles Per Gallon (mpg)",
y = "Horsepower (hp)") +
theme_minimal()
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
提供された R Shiny reprex を、ggplotly の呼び出しと plotly_click の event_data パスに重点を置いて再現する。ggplot aes() で定義された一意のキーがイベントデータに存在し、テーブルを正しくフィルタリングできれば完了とする。issue の編集には、動作するアプローチを記載する。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- r
- 領域
- data-visualization
- issue の種類
- バグ
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 活発さ
- 停滞
- 明瞭さ
- 明確に書かれている
- 初心者へのやさしさ
- 25/100