I am triying to make a heatmap with ggplot and Shiny in R, but the plot is not shown as I expected. I'm using data which have a diffrent structure than Shiny-RStudio exemples and maybe that's why I didn't have a good result.
My data table contains 152013 lines and 7 variabls (zone, season, week, fishingfleet, length, category, vul1.indicator). I want to make a heatmap of the variable "vul1.indicator" with x=week and y=zone and the shiny app user can choose the category and season. My problem is when I change the choices for "season" and "category" in the sidebar only the legend change but not the plot. It seems like there is no link between the plot section and choices. I don't know where I made up mistake. Can you help me please?
This is an example of my table (I have 10 choices for "category" and 3 choices for "season":
> Vul1MoyZoneAgr
zone season week fishingfleet length category vul1.indicator
1 Z1 2012-2013 1 CAD T1 CAD-T1 1
2 Z1 2012-2013 2 CAD T1 CAD-T1 1
3 Z1 2012-2013 3 CAD T1 CAD-T1 1
4 Z1 2012-2013 4 CAD T1 CAD-T1 1
5 Z1 2012-2013 5 CAD T1 CAD-T1 1
6 Z1 2012-2013 6 CAD T1 CAD-T1 1
7 Z1 2012-2013 7 CAD T1 CAD-T1 1
8 Z1 2012-2013 46 CAD T1 CAD-T1 1
9 Z1 2012-2013 47 CAD T1 CAD-T1 1
10 Z1 2012-2013 48 CAD T1 CAD-T1 1
11 Z1 2012-2013 49 CAD T1 CAD-T1 1
12 Z1 2012-2013 50 CAD T1 CAD-T1 1
13 Z1 2012-2013 51 CAD T1 CAD-T1 1
14 Z1 2012-2013 52 CAD T1 CAD-T1 1
And this is my code:
library(ggplot2)
Vul1MoyZoneAgr <- read.table("C:/Users/user/Documents/Vul1MoyZoneAgr.txt", sep="\t",quote="", dec=",", header = TRUE)
Vul1MoyZoneAgr$zone <- factor(Vul1MoyZoneAgr$zone, levels = c("Z1", "Z2", "Z3", "Z4", "Z5", "Z6", "Z7", "Z8", "Z9", "Z10", "Z11", "Z12", "Z13", "Z14", "Z15", "ZI", "ZJ"))
Vul1MoyZoneAgr$week <- factor(Vul1MoyZoneAgr$week, levels = c("40","41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"))
# Define UI ----
ui <- fluidPage(
titlePanel("Vulnerability indicator"),
sidebarLayout(
sidebarPanel(
selectInput("category", "category:",
choices = c(unique(Vul1MoyZoneAgr$category))),
selectInput("season", "season:",
choices = c(unique(Vul1MoyZoneAgr$season))),
),
mainPanel(
# Output: Formatted text for caption ----
h3(textOutput("caption")),
# Output ----
plotOutput("vul1.indicatorPlot")
)
)
)
# Define server logic ----
server <- function(input, output) {
# Compute the formula text ----
# This is in a reactive expression since it is shared by the
# output$caption and output$vul1.indicatorPlot functions
formulaText <- reactive({
paste("vulnerability indicator ~", input$category, "~", input$season)
})
# Return the formula text for printing as a caption ----
output$caption <- renderText({
formulaText()
})
# Reactive expression for the data subsetted to what the user selected
# Generate a plot of the requested season and category ----
output$vul1.indicatorPlot <- renderPlot({
ggplot(as.formula(formulaText()),
data = Vul1MoyZoneAgr, mapping = aes(x=week, y=zone, fill=vul1.indicator)) + theme_bw() +
geom_tile(aes(fill = vul1.indicator), colour = "white") + scale_fill_gradient(low = "#F2E8EC", high = "#990000", limits=c(0, 7), breaks=seq(0,7,by=1)) +
labs(fill = "Vul. Ind.") +
theme(strip.background = element_rect(colour="black", fill="white"), axis.text.x = element_text(face= "bold", size = 10), axis.text.y = element_text(face= "bold",size = 10), axis.title.x = element_text(face= "bold",size = 12), axis.title.y = element_text(face= "bold",size = 12), strip.text.y = element_text(face= "bold",size = 10),strip.text.x = element_text(face= "bold",size = 12), legend.text = element_text(face= "bold",size = 12), legend.title = element_text(face= "bold",size = 12), legend.key.size = unit(1.5, "cm"), legend.key.width = unit(0.5,"cm"), panel.grid.major=element_line(colour = "white"))
})
}
shinyApp(ui, server)
Thank you!