Reputation: 101
I'm attempting to create a shiny app where the user can choose which of three columns to plot over time, consisting of the percentages of three candidates. So far the actual plot works perfectly, but I would like to add colours such that Cand_1 gets a blue line, Cand_2 a green, and Cand_3 a red one. I've attempted to use Plot + scale_colour_manuall = "c("Cand_1" = "blue", "Cand_2" = "green", "Cand_3" = "red)
with and without "" around the variable names, and also if
within the aes()
such that:
Plot <- Plot + geom_line(aes(month, !! sym(input$cand)), group = 1, if(input$cand == "Cand_1){
colour = "blue"}
if(input$cand == "Cand_2"){colour = "green"}
if(input$cand == "Cand_2"){colour = "red})
But none of them works, either giving the error Attempted to create layer with no stat
, or simply ignoring the argumens.
The whole code looks like this:
library(shiny)
library(tidyverse)
setwd("")
Data <- read.csv("Data.csv", stringsAsFactors = F)
# Define UI
ui <- fluidPage(
# Application title
titlePanel("Candidates"),
# Sidebar with a select input
sidebarLayout(
sidebarPanel(
selectInput("Cand",
"Candidates",
choices = colnames(Data)[2:4], multiple = TRUE)
),
mainPanel(
plotOutput("LederPlott"),
textOutput("length")
)
)
)
# Define server logic required to draw plot
server <- function(input, output) {
output$CandPlott <- renderPlot({
Plot <- ggplot(Data)
if(length(input$Cand) == 1){
Plot <- Plot + geom_line(aes(month, !! sym(input$Cand)), group = 1)
}
if(length(input$Cand) == 2){
Plot <- Plot + geom_line(aes(month, !! syms(input$Cand)[[1]]), group = 1)+
geom_line(aes(month, !! syms(input$Cand)[[2]]), group = 1)
}
if(length(input$Cand) == 3){
Plot <- Plot + geom_line(aes(month, !! syms(input$Cand)[[1]]), group = 1)
Plot <- Plot + geom_line(aes(month, !! syms(input$Cand)[[2]]), group = 1)
Plot <- Plot + geom_line(aes(month, !! syms(input$Cand)[[3]]), group = 1)
}
Plot <- Plot + theme_classic() + ylab("%") + ggtitle("%God")
Plot
})
output$length <- renderText(input$Cand)
}
# Run the application
shinyApp(ui = ui, server = server)
And here is some sample data:
Month Cand_1 Cand_2 Cand_3
2019-02-01 60,7 90,1 86,2
2019-03-01 58,9 90,2 80,3
2019-04-01 47,3 88,3 84,6
2019-05-01 54,5 87,3 90
2019-06-01 50,6 86 89
2019-07-01 49,8 84,2 87,1
Upvotes: 0
Views: 402
Reputation: 1450
You cannot assign colour like this,
Plot <- Plot + geom_line(aes(month, !! sym(input$cand)), group = 1, if(input$cand == "Cand_1){
colour = "blue"}
if(input$cand == "Cand_2"){colour = "green"}
if(input$cand == "Cand_2"){colour = "red})
Because colour is a parameter of the aes(). It must appear at top level, like this:
Plot <- Plot + geom_line(aes(month, !! sym(input$cand)), group = 1, colour = <your decision here>)
But also, this parameter serves another purpose. It serves to colour different groups with different colours. What you want is one variable per time. So it won't work either, for this kind of purpose.
What you need is to place the color=
parameter in the geom_line()
call, but outside the aes()
:
Plot <- Plot + geom_line(aes(month, !! sym(input$cand)), group = 1),
colour = if(input$cand == "Cand_1") "blue" else
if(input$cand == "Cand_2")"green" else
if(input$cand == "Cand_3") "red")
There are shorter ways of doing it, also:
color.list <- list(Cand_1 = "blue", Cand_2 = "green", Cand_3 = "red")
Plot <- Plot + geom_line(aes(month, !! sym(input$cand)), group = 1),
colour = color.list[[input$cand]])
Upvotes: 1