Using Plotly event data with a key on a ggplotly object
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 25/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Đình trệ
- Công nghệ
- r
- Lĩnh vực
- data-visualization
Hướng nghiên cứu
Tái hiện reprex R Shiny được cung cấp, tập trung vào lệnh gọi ggplotly và đường dẫn event_data của plotly_click. Được xem là hoàn tất khi một khóa duy nhất được định nghĩa trong ggplot aes() xuất hiện trong dữ liệu sự kiện và lọc bảng chính xác; phần chỉnh sửa của issue mô tả một cách tiếp cận hoạt động.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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()
- Ngôn ngữ chính
- R
- Star
- 2.7k
- Fork
- 641
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của plotly/plotly.R
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 68/100
-
Data gets dropped in add_trace and consequently points have the wrong color if NA is present Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 72/100
-
save_image Error Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 52/100
Tất cả issue của plotly/plotly.R
Issue tương tự
-
bug triage_needed
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
pharmaverse/rtables#1123 · 1 bình luận ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
-
Incorrect coaches in games.csv Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 85/100
-
Trying to reassign a reactiveVal to the name of an existing reactiveVal doesn't overwrite the latter Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100