Hello Everyone,
I have a question regarding the effective use of Ggplot2 to create plots with 2 y axis.
I have the following example data frame:
'''
Date <- c("2021-01-01", "2021-01-05", "2021-01-10", "2021-01-15")
Var1 <- c(0.75, 0.5, 0.63, 0.9)
Var2 <- c(100,200,352, 301)
Var3 <- c(18,22,17,19)
Var4 <- c(1000, 987,905,1002)
DF <- data.frame(Date, Var1, Var2, Var3, Var4)
'''
In the UI I have the following filters:
'''
selectInput("SelectYVariable1", "Select Y axis Variable", choices = c("Var1", "Var2", Var3", Var4")),
selectInput("SelectYVariable2", "Select Secondary Y axis Variable", choices = c("Var1", "Var2", Var3", Var4"))
'''
Based on the users' input into the above selectInputs, I would like a Ggplot2 that graphs the two selected variables with the following:
- a double y axis scatterplot connected with lines
- a primary and secondary y axis title that updates based on the graphed variables
- As you can tell based on the sample data, the value range varies quite a bit between the different variables. However, despite the fact that there is no observable relationship between the primary and secondary axis values, this data has value being seen together on a double y axis graph... Therefore, I would like to set individualized y axis ranges for each of the variables. This way if Var1 is selected a completely different y axis range will be displayed in comparison to when Var2 is selected.
- a legend that updates based on the variables graphed.
So far I have the following code but am having particular difficulty with goals 2-4
'''
ggplot(DF, aes(x=Date)) +
geom_point( aes(y=.data[[input$SelectYVariable1]]), size=2, color="Black") +
geom_point( aes(y=.data[[input$SelectYVariable2]]), size=2, color="Red") +
scale_y_continuous(
sec.axis = sec_axis(~. +0)) +
theme_bw()+
theme(axis.line = element_line(colour = "Black"),
axis.title.y = element_text(size=8),
axis.title.y.right = element_text(size=8),
axis.title = element_text(face="bold"),
axis.text.x = element_text(angle = 45, hjust=1))
'''
Thank you for any help and knowledge you are willing to share on this topic. I look forward to discussing this with you further.