0

I have 2 lines i am trying to plot with a scale difference of ~100,

using this code


 ggplot(TR_midday_avg, aes(x=day)) +
  geom_line( aes(y=avg_conductance))+
  geom_line( aes(y = avg_area_flow)) +
  scale_y_continuous(
    name = "sapflow",
    sec.axis = sec_axis(~.*0.005, name="Second Axis")
  )

It changes the second y axis scale but the avg_area_flow line still scales to the first y axis.

additionally I would prefer to have the area flow on the 1st, and conductance on the 2nd

df snippet

# A tibble: 6 × 7
    day avg_radial_flow avg_area_flow avg_par avg_temp avg_vpd avg_conductance
  <dbl>           <dbl>         <dbl>   <dbl>    <dbl>   <dbl>           <dbl>
1   103           0.610         161.     808     21.8    0.994          15138.
2   104           0.508         134.     759.    23.9    1.07           13884.
3   105           0.585         153.     890     18.2    1.71            8816.
4   106           0.511         133.     918.    17.9    0.922          16326.
5   107           0.482         124.    1055.    12.5    0.891          16999.
6   109           0.245          60.7    456.     9.38   0.652          23214.
DCB
  • 33
  • 4
  • 3
    Yes, `sec.axis` only changes the axis labels, not your data. You need to apply the inverse transformation to your data. See the example [in this answer](https://stackoverflow.com/a/53703152/903061). If you want to switch first and second axes, invert your transformation and switch the label. – Gregor Thomas Jun 12 '23 at 17:38
  • Other discussions about `sec.axis`: https://stackoverflow.com/q/46672694/3358272, https://stackoverflow.com/q/10183888/3358272, https://stackoverflow.com/q/3099219/3358272 – r2evans Jun 12 '23 at 18:36

1 Answers1

0

Here is a small shiny app, where we can inspect the change of the coeff and see what happens to the lines. Basically all is said by @Gregor Thomas and r2evans. But the rule of thumb is what you multiply in first y-axis you have to divide by the second y-axis:

library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(ggplot2)
library(dplyr)

ui <- dashboardPage(
  dashboardHeader(title = "Shiny Application"),
  dashboardSidebar(disable = TRUE),  # disable the sidebar
  dashboardBody(
    fluidRow(
      column(
        width = 6, offset = 3,  # adjust these for desired positioning
        box(
          width = NULL,
          sliderTextInput(
            inputId = "coeffInput",
            label = "Select Coeff Value:",
            choices = seq(0.001, 0.01, 0.001), 
            selected = 0.005, 
            animate = animationOptions(interval = 1000, loop = TRUE)
          ),
          plotOutput("plotOutput")
        )
      )
    )
  )
)

server <- function(input, output) {
  output$plotOutput <- renderPlot({
    coeff <- input$coeffInput
    
    TR_midday_avg %>%
      mutate(avg_conductance_transformed = avg_conductance * coeff) %>%
      ggplot(aes(x = day)) +
      geom_line(aes(y = avg_area_flow, color = "avg_area_flow"), size = 2) +
      geom_line(aes(y = avg_conductance_transformed, color = "avg_conductance_transformed"), size = 2) +
      scale_y_continuous(
        name = "avg_area_flow",
        sec.axis = sec_axis(~./coeff, name = "avg_conductance")
      ) +
      scale_color_manual(values = c("gold", "darkgreen")) +
      theme_minimal(base_size = 20)
  })
}

shinyApp(ui = ui, server = server)

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66