Subset a dataframe based on a plotly on-click event
up vote
1
down vote
favorite
I have the shiny app below which displays a stacked bar chart with five bars. This bar chart is dependent on the FinalDF dataframe. What I would like to do is when I click on -lets say- the 1st bar "TC" to generate a table below with the info only related to "TC" (FinalDF$Name=="TC"
). The same logic is needed for all the bars.
library(plotly)
ui <- fluidPage(
plotlyOutput("plot"),
dataTableOutput("click")
)
server <- function(input, output) {
Name<-c("DCH","DCH","DCH","DGI","DGI","DGI","LDP","LDP","LDP","RH","RH","RH","TC","TC","TC")
Class<-c("Class1","Class2","Overlap","Class1","Class2","Overlap","Class1","Class2","Overlap","Class1","Class2","Overlap","Class1","Class2","Overlap")
count<-c(2077,1642,460,1971,5708,566,2316,810,221,2124,3601,413,2160,1097,377)
FinalDF<-data.frame(Name, Class,count)
output$plot <- renderPlotly({
# Create the stacked bar plot using ggplot()
stackedBarPlot<- ggplot(data = FinalDF,key=key) +
geom_col(mapping = aes(x = Name, y = count, fill = Class,reference="DB"), width = rep(0.9,5),
color = "black", position = position_fill(reverse = T)) +
geom_text(size = 4, position = position_fill(reverse = T, vjust = 0.50), color = "black",
mapping = aes(x = Name, y = count, fill = Class, label = round(count),Reference="DB")) +
coord_flip() +
scale_fill_manual(values = c('lemonchiffon', 'palegreen3', 'deepskyblue2'),breaks=c("Class1", "Overlap", "Class2"), labels = c(paste("Unique to","DB"), "Overlap", "Unique to Comparison Dataset "),
guide = guide_legend(label.position = 'left', label.hjust = 0, label.vjust = 0.5))
ggplotly(stackedBarPlot)
})
output$click <- renderDataTable({
d <- event_data("plotly_click")
if (is.null(d)) "Click events appear here (double-click to clear)" else FinalDF
})
}
shinyApp(ui, server)
r shiny plotly
add a comment |
up vote
1
down vote
favorite
I have the shiny app below which displays a stacked bar chart with five bars. This bar chart is dependent on the FinalDF dataframe. What I would like to do is when I click on -lets say- the 1st bar "TC" to generate a table below with the info only related to "TC" (FinalDF$Name=="TC"
). The same logic is needed for all the bars.
library(plotly)
ui <- fluidPage(
plotlyOutput("plot"),
dataTableOutput("click")
)
server <- function(input, output) {
Name<-c("DCH","DCH","DCH","DGI","DGI","DGI","LDP","LDP","LDP","RH","RH","RH","TC","TC","TC")
Class<-c("Class1","Class2","Overlap","Class1","Class2","Overlap","Class1","Class2","Overlap","Class1","Class2","Overlap","Class1","Class2","Overlap")
count<-c(2077,1642,460,1971,5708,566,2316,810,221,2124,3601,413,2160,1097,377)
FinalDF<-data.frame(Name, Class,count)
output$plot <- renderPlotly({
# Create the stacked bar plot using ggplot()
stackedBarPlot<- ggplot(data = FinalDF,key=key) +
geom_col(mapping = aes(x = Name, y = count, fill = Class,reference="DB"), width = rep(0.9,5),
color = "black", position = position_fill(reverse = T)) +
geom_text(size = 4, position = position_fill(reverse = T, vjust = 0.50), color = "black",
mapping = aes(x = Name, y = count, fill = Class, label = round(count),Reference="DB")) +
coord_flip() +
scale_fill_manual(values = c('lemonchiffon', 'palegreen3', 'deepskyblue2'),breaks=c("Class1", "Overlap", "Class2"), labels = c(paste("Unique to","DB"), "Overlap", "Unique to Comparison Dataset "),
guide = guide_legend(label.position = 'left', label.hjust = 0, label.vjust = 0.5))
ggplotly(stackedBarPlot)
})
output$click <- renderDataTable({
d <- event_data("plotly_click")
if (is.null(d)) "Click events appear here (double-click to clear)" else FinalDF
})
}
shinyApp(ui, server)
r shiny plotly
1
Seems you have a big project and lots of questions. Good luck to you mate. (unnecessary comment, just trying to send good vibes tho).
– Masoud
Nov 8 at 18:49
Big and with deadline thanks mate!
– firmo23
Nov 8 at 18:50
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have the shiny app below which displays a stacked bar chart with five bars. This bar chart is dependent on the FinalDF dataframe. What I would like to do is when I click on -lets say- the 1st bar "TC" to generate a table below with the info only related to "TC" (FinalDF$Name=="TC"
). The same logic is needed for all the bars.
library(plotly)
ui <- fluidPage(
plotlyOutput("plot"),
dataTableOutput("click")
)
server <- function(input, output) {
Name<-c("DCH","DCH","DCH","DGI","DGI","DGI","LDP","LDP","LDP","RH","RH","RH","TC","TC","TC")
Class<-c("Class1","Class2","Overlap","Class1","Class2","Overlap","Class1","Class2","Overlap","Class1","Class2","Overlap","Class1","Class2","Overlap")
count<-c(2077,1642,460,1971,5708,566,2316,810,221,2124,3601,413,2160,1097,377)
FinalDF<-data.frame(Name, Class,count)
output$plot <- renderPlotly({
# Create the stacked bar plot using ggplot()
stackedBarPlot<- ggplot(data = FinalDF,key=key) +
geom_col(mapping = aes(x = Name, y = count, fill = Class,reference="DB"), width = rep(0.9,5),
color = "black", position = position_fill(reverse = T)) +
geom_text(size = 4, position = position_fill(reverse = T, vjust = 0.50), color = "black",
mapping = aes(x = Name, y = count, fill = Class, label = round(count),Reference="DB")) +
coord_flip() +
scale_fill_manual(values = c('lemonchiffon', 'palegreen3', 'deepskyblue2'),breaks=c("Class1", "Overlap", "Class2"), labels = c(paste("Unique to","DB"), "Overlap", "Unique to Comparison Dataset "),
guide = guide_legend(label.position = 'left', label.hjust = 0, label.vjust = 0.5))
ggplotly(stackedBarPlot)
})
output$click <- renderDataTable({
d <- event_data("plotly_click")
if (is.null(d)) "Click events appear here (double-click to clear)" else FinalDF
})
}
shinyApp(ui, server)
r shiny plotly
I have the shiny app below which displays a stacked bar chart with five bars. This bar chart is dependent on the FinalDF dataframe. What I would like to do is when I click on -lets say- the 1st bar "TC" to generate a table below with the info only related to "TC" (FinalDF$Name=="TC"
). The same logic is needed for all the bars.
library(plotly)
ui <- fluidPage(
plotlyOutput("plot"),
dataTableOutput("click")
)
server <- function(input, output) {
Name<-c("DCH","DCH","DCH","DGI","DGI","DGI","LDP","LDP","LDP","RH","RH","RH","TC","TC","TC")
Class<-c("Class1","Class2","Overlap","Class1","Class2","Overlap","Class1","Class2","Overlap","Class1","Class2","Overlap","Class1","Class2","Overlap")
count<-c(2077,1642,460,1971,5708,566,2316,810,221,2124,3601,413,2160,1097,377)
FinalDF<-data.frame(Name, Class,count)
output$plot <- renderPlotly({
# Create the stacked bar plot using ggplot()
stackedBarPlot<- ggplot(data = FinalDF,key=key) +
geom_col(mapping = aes(x = Name, y = count, fill = Class,reference="DB"), width = rep(0.9,5),
color = "black", position = position_fill(reverse = T)) +
geom_text(size = 4, position = position_fill(reverse = T, vjust = 0.50), color = "black",
mapping = aes(x = Name, y = count, fill = Class, label = round(count),Reference="DB")) +
coord_flip() +
scale_fill_manual(values = c('lemonchiffon', 'palegreen3', 'deepskyblue2'),breaks=c("Class1", "Overlap", "Class2"), labels = c(paste("Unique to","DB"), "Overlap", "Unique to Comparison Dataset "),
guide = guide_legend(label.position = 'left', label.hjust = 0, label.vjust = 0.5))
ggplotly(stackedBarPlot)
})
output$click <- renderDataTable({
d <- event_data("plotly_click")
if (is.null(d)) "Click events appear here (double-click to clear)" else FinalDF
})
}
shinyApp(ui, server)
r shiny plotly
r shiny plotly
asked Nov 8 at 18:47
firmo23
628113
628113
1
Seems you have a big project and lots of questions. Good luck to you mate. (unnecessary comment, just trying to send good vibes tho).
– Masoud
Nov 8 at 18:49
Big and with deadline thanks mate!
– firmo23
Nov 8 at 18:50
add a comment |
1
Seems you have a big project and lots of questions. Good luck to you mate. (unnecessary comment, just trying to send good vibes tho).
– Masoud
Nov 8 at 18:49
Big and with deadline thanks mate!
– firmo23
Nov 8 at 18:50
1
1
Seems you have a big project and lots of questions. Good luck to you mate. (unnecessary comment, just trying to send good vibes tho).
– Masoud
Nov 8 at 18:49
Seems you have a big project and lots of questions. Good luck to you mate. (unnecessary comment, just trying to send good vibes tho).
– Masoud
Nov 8 at 18:49
Big and with deadline thanks mate!
– firmo23
Nov 8 at 18:50
Big and with deadline thanks mate!
– firmo23
Nov 8 at 18:50
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53214258%2fsubset-a-dataframe-based-on-a-plotly-on-click-event%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Seems you have a big project and lots of questions. Good luck to you mate. (unnecessary comment, just trying to send good vibes tho).
– Masoud
Nov 8 at 18:49
Big and with deadline thanks mate!
– firmo23
Nov 8 at 18:50