Hi
I want to make a simple interactive bubble chart with shiny and ggplot 2. I have an example data frame 'dF'. I want to plot 'dF$date' on the x axis and 'dF$score' on the y axis.
I want the user to be able to select the 'size' of the bubble and the 'group' of the bubble.
The code and sessionInfo() is below and I am attaching the app.r file too
I face errors when adjusting the 'sliderInput' and/or when selecting the 'selectInput'
For example:
When I have one selectInput value, ggplot seems to make a plot
When I have two selectInput values, ggplot makes a plot but there are points missing. In addition only some combinations of two inputs work.
When I select three or more selectInput values no plot is created with the following error
Aesthetics must be either length 1 or the same as the data (1): size, colour, x, y
I think there may be a problem with my variable types and my aes() calls.
Any help on how to make this interactive bubble chart work correctly would be greatly appreciated.
Thanks In advance
Sanjeev
##############################################################################################################
library(shiny)
library(ggplot2)
library(dplyr)
dF<-data.frame(
id=c("26807712", "26794930", "26518434","26847041","26504153", "26824982", "26773072", "26757162", "26810136", "26781275"),
date=c("2016-01-26","2016-01-22","2016-01-15","2016-02-05","2015-12-19","2016-01-29","2016-01-16", "2016-01-12","2016-01-26", "2016-01-19"),
score=c(1, 6.5, 3, 4, 2.5, 4, 5, 9, 3,4),
rank=c(10, 15, 5, 6, 8, 14, 19, 2, 5, 7),
class=c("OBS","RCT","GEN","META","RCT","DIA","OBS" ,"META","GEN","DIA"))
dF$date<-as.Date(dF$date)
print(str(dF))
ui<-fluidPage(
titlePanel(title="Interactive Bubble Chart"),
sidebarLayout(
sidebarPanel(
### Input for Impact
sliderInput("impact","Choose an Impact", min=min(dF$rank), max=max(dF$rank), value=c(5,10)),
## Input for Method
selectInput("method", "Choose a Methodology",choices=as.character(dF$class), multiple=T, selected = "OBS")
),
mainPanel(
plotOutput("plot"),
textOutput("text1")
)
)
)
server<-function(input, output){
subsetted <- reactive({
dF %>%
filter(rank >= input$impact[1] & rank <= input$impact[2]) %>%
filter(class == input$method)
})
output$plot<-renderPlot({
if (is.null(subsetted()))
return(NULL)
ggplot(subsetted(), aes(date,score)) +
geom_point(aes(size=input$impact, colour=input$method)) +
ylim(-1,10)
})
output$text1<-renderText({
paste("You have selected this", input$method)
})
}
shinyApp(ui=ui, server=server)
################################################################################################################
sessionInfo()
R version 3.2.3 (2015-12-10)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.3 (El Capitan)
locale:
[1] en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] googleVis_0.5.10 dplyr_0.4.3 ggplot2_2.0.0 shiny_0.13.0
loaded via a namespace (and not attached):
[1] Rcpp_0.12.3 assertthat_0.1 digest_0.6.9 mime_0.4 grid_3.2.3 R6_2.1.2
[7] plyr_1.8.3 jsonlite_0.9.19 DBI_0.3.1 xtable_1.8-2 gtable_0.1.2 magrittr_1.5
[13] scales_0.3.0 lazyeval_0.1.10 labeling_0.3 RJSONIO_1.3-0 tools_3.2.3 munsell_0.4.2
[19] parallel_3.2.3 httpuv_1.3.3 rsconnect_0.4.1.11 colorspace_1.2-6 htmltools_0.3