For some reason one of my plots won't appear on my app except if I put the window on full screen on the biggest of my two monitors. Here is how it is looking like:
On my other plots, the plots are just cropped:
For some reason, once the plot is opened with the full window on my big monitor, then when I make the window small again, the plot displays correctly.
How do I make my "Color Distribution" plot appear correctly from the start (cropped) even when the window is small ?
Following is my code, the concerned plot is the 'Top 5 Main Colors' one:
library(fontawesome)
library(shiny)
library(shinydashboard)
library(ggplot2)
library(dplyr)
library(wordcloud2)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title = "Animal Shelter"),
dashboardSidebar(
sidebarMenu(
menuItem("Outcome & Neutering", tabName = "outcome_neutering",
icon = icon("paw")),
menuItem("Animal Color", tabName = "animal_color",
icon = icon("brush")),
radioButtons(inputId = "AnimalType",
label = "Animal Species",
choices = c("Dog", "Cat", "All"),
selected = "Dog"),
uiOutput("breedselect"),
uiOutput("image")
)
),
dashboardBody(
tabItems(
tabItem(tabName = "outcome_neutering",
fluidRow(
box(
title = "Outcome Type",
plotOutput(outputId = "barplot", width = "600px")
),
box(
title = "Neutered Status",
plotOutput(outputId = "piechart", width = "600px")
)
)
),
tabItem(tabName = "animal_color",
fluidRow(
box(
title = "Color Distribution",
plotOutput(outputId = "colorplot", width = "600px",
height = "500px")
),
box(
title = "Breed Color Wordcloud",
wordcloud2Output(outputId = "wordcloud", width = "600px",
height = "500px")
)
)
)
)
)
)
server <- function(input, output) {
load("trainnew.RData")
output$breedselect <- renderUI({
if(input$AnimalType == "All") {
selectInput(inputId = "SimplifiedBreed",
label = "Simplified Breed",
choices = "All")
} else {
selectInput(inputId = "SimplifiedBreed",
label = "Simplified Breed",
choices = c("All",
unique(trainnew$SimplifiedBreed[trainnew$AnimalType == input$AnimalType])))
}
})
output$image <- renderUI({
if (input$AnimalType == "Dog") {
tags$img(src="https://purepng.com/public/uploads/large/purepng.com-doganimalsdogboy-981524673092o91pm.png", width = "100%")
} else if (input$AnimalType == "Cat") {
tags$img(src="https://www.transparentpng.com/thumb/cat/I06qSD-cat-transparent-image.png", width = "100%")
} else {
tags$img(src="https://i.pinimg.com/originals/90/0f/53/900f537452fd83ec2f947e01fbd8ae67.png", width = "100%")
}
})
data <- reactive({
if(input$AnimalType == "All") {
trainnew %>% filter(AnimalType %in% c("Dog", "Cat"))
} else {
trainnew %>%
filter(AnimalType == input$AnimalType,
SimplifiedBreed == input$SimplifiedBreed |
input$SimplifiedBreed == "All")
}
})
#the outcomes plot
output$barplot <- renderPlot({
ggplot(data(), aes(x = OutcomeType, fill = OutcomeType)) +
geom_bar() +
theme_minimal() +
xlab("Outcome Type") +
scale_fill_brewer(palette = "Blues", direction = -1) +
ylab("Animal Count") +
ggtitle("Outcome distribution")
})
#the adopted pie chart
output$piechart <- renderPlot({
neutered_data <- data() %>%
filter(OutcomeType == "Adoption") %>%
group_by(NeuteredYN) %>%
summarize(count = n())
ggplot(neutered_data, aes(x="", y=count, fill=NeuteredYN)) +
geom_bar(width = 1, stat = "identity") +
coord_polar("y", start=0) +
scale_fill_manual(values = c( "#307fc5","#CE5B65"),
breaks = c("yes", "no")) +
ggtitle("Proportion of neutered/non-neutered adopted animals") +
labs(fill = "Neutered?") +
geom_text(aes(label = scales::percent(count/sum(count)),
color = "white"),
position = position_stack(vjust = 0.5), fontface = "bold") +
scale_color_manual(values = "white", guide = FALSE)
})
output$colorplot <- renderPlot({
top_colors <- data() %>%
count(MainColor) %>%
arrange(desc(n)) %>%
slice(1:5)
ggplot(top_colors, aes(x = reorder(MainColor, -n), y = n,
fill = MainColor)) +
geom_col(color = "#307fc5") +
scale_fill_brewer(palette = "Blues") +
theme_minimal() +
xlab("Main Color") +
ylab("Animal Count") +
ggtitle("Top 5 Main Colors")
})
output$wordcloud <- renderWordcloud2({
if(input$AnimalType == "All") {
df_filter <- filter(data(), AnimalType %in% c("Dog", "Cat"))
} else {
df_filter <- filter(data(), AnimalType == input$AnimalType)
}
# count the frequencies of each color
color_freq <- df_filter %>%
count(Color) %>%
arrange(desc(n))
# create a vector of colors
blue_tones <- c("#0071c5", "#2196F3", "#5dade2", "#85c1e9", "#aed6f1")
# pass the resulting data frame and vector of colors to wordcloud2
wordcloud2(color_freq, size = 1.5, color = blue_tones)
})
}
shinyApp(ui, server)
I've tried putting my app into fluid page, didn't change anything, tried tweaking the width and height of the plot output in the ui :(