I'm new here as a user but I have searched like crazy for a problem I have encountered while trying to create a data visualization app with shiny in Rstudio.
The thing is, I want to read a .csv, understand it's columns, select wich column I want as x and as y axis, plot them with the type of graph I have chosen and be able to zoom in in a secondary plot whenever I want.
I'm almost there, the thing is that the zoom with brush that I have tried to do is no working properly. It doesn't understand the values of the axis correctly, instead it works as if both axis where only from 0 to 1, and then zoom in the correct way but with the wrong xlim and ylim.
Here is my ui.R:
library(shiny)
library(ggplot2)
base = read.csv("TESTE.csv", sep = ";")
tipos <- c("Dispersão", "Histograma", "Boxplot", "Área")
shinyUI(fluidPage(
titlePanel("MGM"),
sidebarLayout(
sidebarPanel(
selectInput("selectedColX", "Select colum for X axis", choices = colnames(base), selected = colnames(base)[7]),
selectInput("selectedColY", "Select colum for Y axis", choices = colnames(base), selected = colnames(base)[4]),
selectInput("selectedColor", "Select colum for colour axis", choices = colnames(base), selected = colnames(base)[6]),
selectInput("seletedGraph", "Select type of graph", choices = tipos, selected = tipos[1])
),
fluidRow(
column(width = 12, class = "well",
h4("Left plot controls right plot"),
fluidRow(
column(width = 10,
plotOutput("Disp", height = 300,
brush = brushOpts(
id = "Disp_brush",
clip = TRUE,
resetOnNew = TRUE
)
)
),
column(width = 10,
plotOutput("DispZoom", height = 300)
)
)
)
)
# mainPanel(
#
# plotOutput("Hist"),
# plotOutput("Box"),
# plotOutput("Ar")
# )
)
))
And then my Server.R:
library(shiny)
library(ggplot2)
base = read.csv("TESTE.csv", sep = ";")
tipos <- c("Dispersão", "Histograma", "Boxplot", "Área")
shinyServer(function(input, output) {
output$Disp <- renderPlot({
validate(need(input$seletedGraph=="Dispersão", message=FALSE))
y_axis <- input$selectedColY
x_axis <- input$selectedColX
color_axis <- input$selectedColor
gg <- ggplot(base, aes_string(x = x_axis, y = y_axis, color = color_axis))
gg <- gg + geom_point()
plot(gg)
})
ranges2 <- reactiveValues(x = NULL, y = NULL)
output$DispZoom <- renderPlot({
validate(need(input$seletedGraph=="Dispersão", message=FALSE))
y_axis <- input$selectedColY
x_axis <- input$selectedColX
color_axis <- input$selectedColor
gg <- ggplot(base, aes_string(x = x_axis, y = y_axis, color = color_axis)) + geom_point() + coord_cartesian(xlim = ranges2$x, ylim = ranges2$y)
plot(gg)
})
output$Hist <- renderPlot({
validate(need(input$seletedGraph=="Histograma", message=FALSE))
y_axis <- input$selectedColY
x_axis <- input$selectedColX
color_axis <- input$selectedColor
gg <- ggplot(base, aes_string(x = x_axis))
gg <- gg + geom_histogram()
gg
})
output$Box <- renderPlot({
validate(need(input$seletedGraph=="Boxplot", message=FALSE))
y_axis <- input$selectedColY
x_axis <- input$selectedColX
color_axis <- input$selectedColor
gg <- ggplot(base, aes_string(x = x_axis, y = y_axis, color = color_axis))
gg <- gg + geom_boxplot()
gg
})
output$Ar <- renderPlot({
validate(need(input$seletedGraph=="Área", message=FALSE))
y_axis <- input$selectedColY
x_axis <- input$selectedColX
color_axis <- input$selectedColor
gg <- ggplot(base, aes_string(x = x_axis, y = y_axis, color = color_axis))
gg <- gg + geom_area()
gg
})
observe({
brush <- input$Disp_brush
if (!is.null(brush)) {
ranges2$x <- c(brush$xmin, brush$xmax)
ranges2$y <- c(brush$ymin, brush$ymax)
} else {
ranges2$x <- NULL
ranges2$y <- NULL
}
})
})
Just ignore the other plots that are not the geom_point. As soon as I get this one working the others should work just fine, I guess...
Thank you so much, I'm having such a pain trying to figure this out!
Some texts are in portuguese, but I think everything is understandable enough.
Your brushed points are on scale from 0 to 1 in the brushOpts because you print or plot your variable instead of just returning it.
1. Short desmonstration
This short app show the difference between the brushed points scales according to how it has been returned.
library(shiny)
ui <- fluidPage(
fluidRow(
column(6,
# My plot rendering with print or plot
h4("Plot with print or plot variable"),
plotOutput("plot1", height = 300, brush = brushOpts(id = "plot1_brush", clip = TRUE, resetOnNew = TRUE)),
p(),
# Brushed points
"Brushed points informations, scale from 0 to 1",
verbatimTextOutput("brush1")
),
column(6,
# My plot rendering without print or plot
h4("Plot with a return variable"),
plotOutput("plot2", height = 300, brush = brushOpts(id = "plot2_brush", clip = TRUE, resetOnNew = TRUE)),
p(),
# Brushed points
"Brushed points informations, scale according to x and y variables",
verbatimTextOutput("brush2")
)
)
)
server <- function(input, output) {
data <- iris
# Plot1 I render with print or plot
output$plot1 <- renderPlot({
gg <- ggplot(data, aes(x = Sepal.Length, y = Petal.Length, color = Species)) + geom_point()
plot(gg)
})
# Brush points from plot1
output$brush1 <- renderPrint({
input$plot1_brush
})
# Plot2 I render just returning the variable
output$plot2 <- renderPlot({
gg <- ggplot(data, aes(x = Sepal.Length, y = Petal.Length, color = Species)) + geom_point()
return(gg)
})
# Brush points from plot2
output$brush2 <- renderPrint({
input$plot2_brush
})
}
shinyApp(ui = ui, server = server)
2. Reproductible example from your question
Herebelow I made a reproductible example using the iris dataset.
Also, I changed some characters because of accents.
ui.R
library(shiny)
library(ggplot2)
shinyUI(fluidPage(
titlePanel("MGM"),
sidebarLayout(
sidebarPanel(
uiOutput("plots_parameters")
),
mainPanel(
fluidRow(
column(12,
h4("Plot without zoom"),
plotOutput("Disp", height = 300, brush = brushOpts(id = "Disp_brush", clip = TRUE, resetOnNew = TRUE))
)
),
fluidRow(
column(12,
h4("Zoomed plot"),
plotOutput("DispZoom", height = 300)
)
)
)
)
))
server.R
library(shiny)
library(ggplot2)
base = iris
shinyServer(function(input, output) {
output$plots_parameters <- renderUI({
tipos <- c("Dispersao", "Histograma", "Boxplot", "Área")
choices <- colnames(base)
div(
selectInput("selectedColX", "Select colum for X axis", choices = choices, selected = "Sepal.Length"),
selectInput("selectedColY", "Select colum for Y axis", choices = choices, selected = "Petal.Length"),
selectInput("selectedColor", "Select colum for colour axis", choices = choices, selected = "Species"),
selectInput("seletedGraph", "Select type of graph", choices = tipos, selected = "Dispersao")
)
})
output$Disp <- renderPlot({
req(input$seletedGraph == "Dispersao")
y_axis <- input$selectedColY
x_axis <- input$selectedColX
color_axis <- input$selectedColor
gg <- ggplot(base, aes_string(x = x_axis, y = y_axis, color = color_axis))
gg <- gg + geom_point()
# Return variable without print or plot
gg
})
ranges2 <- reactiveValues(x = NULL, y = NULL)
output$DispZoom <- renderPlot({
req(input$seletedGraph == "Dispersao")
y_axis <- input$selectedColY
x_axis <- input$selectedColX
color_axis <- input$selectedColor
gg <- ggplot(base, aes_string(x = x_axis, y = y_axis, color = color_axis)) + geom_point() +
coord_cartesian(xlim = ranges2$x, ylim = ranges2$y)
# Return variable without print or plot
gg
})
observe({
brush <- input$Disp_brush
if (!is.null(brush)) {
ranges2$x <- c(brush$xmin, brush$xmax)
ranges2$y <- c(brush$ymin, brush$ymax)
} else {
ranges2$x <- NULL
ranges2$y <- NULL
}
})
})
Related
The ggplot just shows a vertical line of values that doesn't change when I try changing the x axis selection. The only thing on the x axis is the word "column" when I try to change the x axis, instead of the values of df$column according to what's selected.
df_variable <- df
df_colnames <- colnames(df)
xaxis_input <- selectInput(
inputId = "xaxis",
label = "Feature of Interest",
choices = df_colnames,
selected = df_colnames['default']
)
ui <- fluidPage(
titlePanel("DF"),
xaxis_input,
plotOutput(
outputId = "df_plot",
)
)
server <- function(input, output) {
output$df_plot <- renderPlot({
plot <- ggplot(data = df) +
geom_point(aes(x = input$xaxis, y = some_other_col))
return(plot)
})
}
input$xaxis is a string, so you cannot use it directly inside aes().
Try using aes_string() instead.
Note that some_other_col should also be a string.
server <- function(input, output) {
output$df_plot <- renderPlot({
plot <- ggplot(data = df) +
geom_point(aes_string(x = input$xaxis, y = "some_other_col"))
return(plot)
})
A full working example:
library(shiny)
library(ggplot2)
df <- iris
df_colnames <- colnames(df)
xaxis_input <- selectInput(
inputId = "xaxis",
label = "Feature of Interest",
choices = df_colnames
)
ui <- fluidPage(
titlePanel("DF"),
xaxis_input,
plotOutput(
outputId = "df_plot",
)
)
server <- function(input, output) {
output$df_plot <- renderPlot({
plot <- ggplot(data = df) +
geom_point(aes_string(x = input$xaxis, y = "Sepal.Width"))
return(plot)
})
}
# Run the application
shinyApp(ui = ui, server = server)
I'd like to include the reactive outputs of two data sets as different geom_lines in the same ggplotly figure. The code runs as expected when only one reactive data.frame is included as a geom_line. Why not two?
ui <- fluidPage(
sidebarLayout(
selectInput("Var1",
label = "Variable", #DATA CHOICE 1
selected = 10,
choices = c(10:100)),
selectInput("Var1",
label = "Variable2", #DATA CHOICE 2
selected = 10,
choices = c(10:100))
# Show a plot of the generated distribution
),
mainPanel(
plotlyOutput('plot') #Draw figure
)
)
server <- function(input, output) {
out <- reactive({
data.frame(x = rnorm(input$Var1), #Build data set 1
y = 1:input$Var1)
})
out2 <- reactive({
data.frame(x = rnorm(input$Var2), #Build data set 2
y = 1:input$Var2)
})
output$plot <- renderPlotly({
p <- ggplot() +
geom_line(data = out(), aes(x = x, y = y)) #Add both data sets in one ggplot
geom_line(data = out2(), aes(x = x, y = y), color = "red")
ggplotly(p)
})
}
# Run the application
shinyApp(ui = ui, server = server)
When you put the data into long format and give each group a group identifier it seems to work. Note that you should be able to change sliderInput back to selectInput - this was one of the entries I toggled during testing, but the choice of UI widget should not matter.
This works -- code can be simplified inside the reactive from here:
library(plotly)
ui <- fluidPage(
sidebarLayout(
sliderInput("Var1",
label = "Variable", #DATA CHOICE 1
min=10, max=100, value=10),
sliderInput("Var2",
label = "Variable2", #DATA CHOICE 2
min=10, max=100, value=10),
),
mainPanel(
plotlyOutput('plot') #Draw figure
)
)
server <- function(input, output) {
out <- reactive({
x1 <- rnorm(input$Var1)
y1 <- seq(1:input$Var1)
x2 <- rnorm(input$Var2)
y2 <- seq(1:input$Var2)
xx <- c(x1,x2)
yy <- c(y1,y2)
gg <- c( rep(1,length(y1)), rep(2,length(y2)) )
df <- data.frame(cbind(xx,yy,gg))
df
})
output$plot <- renderPlotly({
p <- ggplot() +
geom_line(data=out(), aes(x = xx, y = yy, group=gg, colour=gg))
ggplotly(p)
})
}
shinyApp(ui = ui, server = server)
I have plot and next to it an interactive plot that updates the coordinate limits when user brushes on the plot. I need the interactive plot to show (on the plot) the data point values when hovered on (e.g. "The data belongs to ID X". has anyone had such problem?
I included a reproducible chunk of my code and tried to make it fully relevant, but you can only focus on output$text.
Would appreciate any help.
Thanks
require(shiny)
require(ggplot2)
dat <-data.frame(seq(1,50, 1), seq(0,100,length.out = 50), sapply(seq(0,100,length.out = 50), function(x) x+3*rnorm(1,0,4)))
varlist <- c("ID", "IPRED", "DV")
names(dat) <- varlist
ui <- fluidPage(
selectInput("y", "Y Variable", choices = varlist, selected = "IPRED" ),
selectInput("x", "X Variable", choices = varlist, selected = "DV"),
checkboxInput("dvpred", "Show Unity Line", value = TRUE),
column(width=6,
plotOutput("p1",
dblclick = "plot1_dblclick",
brush = brushOpts(
id = "plot1_brush",
resetOnNew = TRUE))
),
column(width = 6,
plotOutput("p12", hover = hoverOpts("p12_hover", delay = 100, delayType = "debounce"))),
uiOutput("txt")
)
server <- function(input, output){
pxy <- function(dataset, xvar, yvar, xlim=NULL, ylim=NULL){
dat = subset(dataset, dat$DV<500)
vmax <- max(max(dat[[xvar]]),max(dat[[yvar]]))
vmin <- min(min(dat[[xvar]]),min(dat[[yvar]]))
p <- ggplot(data = dat) +
geom_point(aes_string(x=xvar, y=yvar), size=2,shape=21, fill="blue") +
labs(x=xvar, y=yvar) + ggtitle(paste0(yvar, " vs ", xvar)) +
geom_hline(yintercept=0) +
coord_cartesian(xlim = xlim, ylim = ylim, expand = TRUE)
if ( input$dvpred)(p <- p + xlim(vmin, vmax)+ ylim(vmin, vmax) + geom_abline(slope=1) )
return(p)
}
output$p1 <- renderPlot({
xlength <- length(unique(dat[[input$x]]))
if (xlength>12){
return(pxy(dat, input$x, input$y))}
else
return (bxplotxy(dat, input$x, input$y))
})
output$txt <-
renderPrint({
if(!is.null(input$p21_hover)){
hover=input$p21_hover
dat$dist<-sqrt((hover$x-dat$DV)^2+(hover$y-dat$IPRED)^2)
if (min(subset(dat, !is.na(dist))$dist)<4)
cat("This Data Point Belongs to the Patient ID: ", dat$ID[which.min(dat$dist)])
}
})
ranges <- reactiveValues(x = NULL, y = NULL)
output$p12 <- renderPlot({
xlength <- length(unique(dat[[input$x]]))
if (xlength>12){
return(pxy(dat, input$x, input$y, ranges$x, ranges$y))}
else
return (NULL)
})
observe({
brush <- input$plot1_brush
if (!is.null(brush)) {
ranges$x <- c(brush$xmin, brush$xmax)
ranges$y <- c(brush$ymin, brush$ymax)
} else {
ranges$x <- NULL
ranges$y <- NULL
}
})
}
shinyApp(ui, server)
I think you should be using verbatimTextOutput("txt") with output$txt <- renderPrint({ ... }), rather than uiOutput("txt"). But this would give you a text output outside of the plot, which doesn't sound like what you want..?
If you want the text to appear next to the cursor as you hover, there's a ggplot2 extension called ggiraph: http://davidgohel.github.io/ggiraph/index.html. But I'm not sure it's compatible with brushing.
So I agree with Keqiang Li's suggestion to use plotly. The brushed information is different from ggplot, so you might find this example helpful: https://plot.ly/r/shinyapp-linked-brush/
Good luck!
I'm trying to create an easy shiny dashboard. I'm using the next data frame:
df <- data.frame(Age = c(18,20,25,30,40),
Salary = c(18000, 20000, 25000, 30000, 40000),
Date = as.Date(c("2006-01-01", "2008-01-01", "2013-01-01", "2018-01-01", "2028-01-01")))
save(df, file = "data.Rdata")
And the code for doing the shiny app is the following:
library(shiny)
library(ggplot2)
load("C:/.../data.RData")
ui <- fluidPage(
sidebarLayout(
# Inputs
sidebarPanel(
# Select variable for y-axis
selectInput(inputId = "y",
label = "Y-axis:",
choices = names(df),
selected = "Salary"),
# Select variable for x-axis
selectInput(inputId = "x",
label = "X-axis:",
choices = names(df),
selected = "Date")
),
# Outputs
mainPanel(
plotOutput(outputId = "scatterplot")
)
)
)
server <- function(input, output) {
output$scatterplot <- renderPlot({
ggplot(data = df, aes(x = input$x, y = input$y)) +
geom_line()
})
}
shinyApp(ui = ui, server = server)
This is what I get on my plot:
And this is what I'm expecting:
I'm not sure what I'm missing on my code.
Try with:
output$scatterplot <- renderPlot({
ggplot(data = df, aes(x = df[, input$x], y = df[, input$y])) +
geom_line()
})
or simply by using:
output$scatterplot <- renderPlot({
ggplot(data = df, aes_string(x = input$x, y = input$y)) +
geom_line()
})
I have a shiny app, example shown below that should be reproducible where I am trying to show a ggplot2 scatterplot with points which can be excluded as shown in this example here. I am also using modules, which might be part of this issue here.
https://gallery.shinyapps.io/106-plot-interaction-exclude/
I keep getting this "Error in eval: object 'xaxis' not found" message. Any ideas? I put the module code up front then the rest of the code for the app.R file.
library(ggplot2)
library(scales)
library(shiny)
library(shinydashboard)
###### MODULE CODE ###############
scatter_graphUI <- function(id, tab_panel_name, height = "500px") {
ns <- NS(id)
tabPanel(tab_panel_name,
plotOutput(ns("scatter_1"), height = height, click = "plot1_click", brush =
brushOpts(id = "plot1_brush")),
actionButton(ns("exclude_toggle"), "Toggle points"),
actionButton(ns("exclude_reset"), "Reset")
)
}
scatter_graph <- function(input, output, session, scatter_data, col_select) {
scatter_data_df <- reactive({
mtcars
})
vals <- reactiveValues()
data_df <- reactive({
scatter_df <- scatter_data_df()
main_df <- scatter_df[,col_select]
vals$keeprows = rep(TRUE,nrow(main_df))
main_df
})
output$scatter_1 <- renderPlot({
graph_df <- data_df()
# Plot the kept and excluded points as two separate data sets
keep <- graph_df[ vals$keeprows,]
exclude <- graph_df[!vals$keeprows,]
final_df <- keep
title = paste(colnames(final_df)[1], "vs", colnames(final_df)[2])
line_method = "quad"
axis_text = 12
title_text = 16
split_colors = TRUE
colors = c("red","black")
# create red points for negative x axis returns if split_colors is TRUE
if (split_colors == TRUE) {
final_df[,"color"] <- ifelse(final_df[,2,drop=F]<0,colors[1],colors[2])
} else {
final_df[,"color"] <- ifelse(final_df[,2,drop=F]<0,colors[2],colors[2])
}
# create a generic graphing final_df
colnames(final_df) <- c("xaxis","yaxis","color")
# setup the graph
gg <- ggplot(final_df, aes(x = xaxis, y = yaxis)) + geom_point(color = final_df[,"color"])
gg <- gg + geom_point(data = exclude, shape = 21, fill = NA, color = "black", alpha = 0.25) +
coord_cartesian(xlim = c(1.5, 5.5), ylim = c(5,35))
if (line_method == "loess") {
gg <- gg + stat_smooth(span = 0.9)
} else if (line_method == "quad") {
gg <- gg + stat_smooth(method = "lm", formula = y ~ poly(x, 2), size = 1)
} else if (line_method == "linear") {
gg <- gg + stat_smooth(method = "lm")
} else {
}
gg <- gg + theme_bw()
gg <- gg + labs(x = colnames(final_df)[2], y = colnames(final_df)[3], title = title)
gg
})
# Toggle points that are clicked
observeEvent(input$plot1_click, {
main_df <- data_df()
res <- nearPoints(main_df, input$plot1_click, allRows = TRUE)
vals$keeprows <- xor(vals$keeprows, res$selected_)
})
# Toggle points that are brushed, when button is clicked
observeEvent(input$exclude_toggle, {
main_df <- data_df()
res <- brushedPoints(main_df, input$plot1_brush, allRows = TRUE)
vals$keeprows <- xor(vals$keeprows, res$selected_)
})
# Reset all points
observeEvent(input$exclude_reset, {
main_df <- data_df()
vals$keeprows <- rep(TRUE, nrow(main_df))
})
}
########################################
##### REST OF APP CODE ######
header <- dashboardHeader(
title = 'Test Dashboard'
)
body <- dashboardBody(
tabItems(
tabItem(tabName = "scatter_eval",
tabBox(
title = "Scatter",
selected = "Selected",
height = "600px", side = "right",
scatter_graphUI("selected_scatter", "Selected")
)
)
)
)
sidebar <- dashboardSidebar(
sidebarMenu(
menuItem("Scatter Evaluation", icon = icon("th"), tabName = "scatter_eval")
)
)
ui <- dashboardPage(skin = "blue",
header,
sidebar,
body
)
server <- function(input, output, session) {
callModule(scatter_graph, id ="selected_scatter", scatter_data = reactive(selected_scatter_data()),
col_select = c(1,2))
}
shinyApp(ui = ui, server = server)
########
The issue is the two lines:
gg <- ggplot(final_df, aes(x = xaxis, y = yaxis)) + geom_point(color = final_df[,"color"])
gg <- gg + geom_point(data = exclude, shape = 21, fill = NA, color = "black", alpha = 0.25) +
coord_cartesian(xlim = c(1.5, 5.5), ylim = c(5,35))
Because you have not set a new aes for the exclude object, it inherits the aes from your ggplot call. It therefore needs to find a column named xaxis and yaxis in the exclude dataset. Since you only renamed final_df, it throws this error.
A graph is displayed when you change:
colnames(final_df) <- c("xaxis","yaxis","color")
to:
colnames(final_df) <- c("xaxis","yaxis","color")
colnames(exclude) <- c("xaxis","yaxis")