I'm building a shiny app where I can manipulate a histogram of the diamonds dataset by changing the input to be displayed which works fine. I now want to be able to change the colours of the plot to reflect the cut, clarity and color of the diamonds data.
Code to build app
#Data preparation
library(shiny)
library(tidyverse)
library(ggplot2)
diamonds_data <- as_tibble(diamonds) %>%
rename_all(stringr::str_to_title)
#App design
ui <- fluidPage(
#App title
titlePanel("Histogram version"),
#Sidebar layout for main and sidebar panel
sidebarLayout(
# Sidebar panel for inputs
sidebarPanel(
#Selecting histogram colour
selectInput(inputId="color1",label="Choose Color",choices = c("Color"="Color","Carat"="Carat","Clarity"="Clarity"),
selected = "Cut",multiple = F),
#Selecting Histogram input
selectInput(inputId="channel1",label="Distribution of data",choices = c("Carat"="Carat",
"Depth"="Depth",
"Table"="Table",
"Price"="Price",
"X"="X",
"Y"="Y",
"Z"="Z"),
selected = "Carat",multiple = F),
#Selecting number of histogram bind
sliderInput(inputId = "NOofBins",
label = "Number of bins:",
min = 1,
max = 50,
value = 30),
),
#Main panel for outputs
mainPanel(
#Histogram
plotOutput(outputId = "distPlot")
)
)
)
server <- function(input, output){
output$distPlot <- renderPlot({
if(input$color1=="Clarity"){
Color = "Clarity"
}else if(input$color1=="Cut"){
Color = "Cut"
}else if(input$color1=="Color"){
Color = "Color"
}
my_plot <- diamonds_data %>% ggplot()
if(input$channel1 == "Carat"){
my_plot <- my_plot + geom_histogram(aes(x=Carat),bins = input$NOofBins,fill=Color)
}else if(input$channel1 == "Depth"){
my_plot <- my_plot + geom_histogram(aes(x=Depth),bins = input$NOofBins,fill=Color)
}else if(input$channel1 == "Table"){
my_plot <- my_plot + geom_histogram(aes(x=Table),bins = input$NOofBins,fill=Color)
}else if(input$channel1 == "Price"){
my_plot <- my_plot + geom_histogram(aes(x=Price),bins = input$NOofBins,fill=Color)
}else if(input$channel1 == "X"){
my_plot <- my_plot + geom_histogram(aes(x=X),bins = input$NOofBins,fill=Color)
}else if(input$channel1 == "Y"){
my_plot <- my_plot + geom_histogram(aes(x=Y),bins = input$NOofBins,fill=Color)
}else if(input$channel1 == "Z"){
my_plot <- my_plot + geom_histogram(aes(x=Z),bins = input$NOofBins,fill=Color)
}
my_plot <- my_plot + theme_bw()+
theme(axis.title = element_text(size=26,color="Grey",face="bold"),
axis.text = element_text(size=12,color="Grey",face="bold"))+
labs(x="Diamonds Element",y="Count",title=paste("Distribution of diamonds data",input$channel1,sep = " "))
my_plot
})
}
shinyApp(ui = ui, server = server)
As mentioned I want to be able to alter the input of the histogram where I can change the colours based on the cut, clarity or color as can be seen with the code below:
ggplot(data = diamonds_data, aes(x = Price)) +
geom_histogram(aes(fill = Cut))
When I use my app script I get the warning >Unknown colour name: Color
You can use the .data
argument of ggplot
which accepts strings as inputs and so significantly simplify your code:
my_plot <- diamonds_data %>%
ggplot() +
geom_histogram(aes(x = .data[[input$channel1]], fill = .data[[input$color1]]), bins = input$NOofBins) +
theme_bw()+
theme(axis.title = element_text(size=26,color="Grey",face="bold"),
axis.text = element_text(size=12,color="Grey",face="bold"))+
labs(x="Diamonds Element",y="Count",title=paste("Distribution of diamonds data",input$channel1,sep = " "))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.