ggplot two axis on geom_line with tidy data

Hello,
I am learning a bit of ggplot.
I know I have to tidy my data.
Here in the example, I want to plot two lines with geom_line. The lines should be: unemploy and pasavert.
As you will see on the plot, pasvert doesn't show any info. Its just a flat line.
On excel, "you can adjust the secondary axis labels to the range of pasvert".

economics_tidy <- economics %>%
  select(date, unemploy, psavert)  %>%  
  pivot_longer(cols = c(unemploy, psavert), names_to = "variable", values_to = "value")


ggplot(economics_tidy, aes(x = date, y = value, color = variable)) +
  geom_line() +
  scale_x_date(date_labels = "%Y-%m") +
  scale_y_continuous(name = "unemploy", sec.axis = sec_axis(~. * 15, name = "psavert"))+
  labs(title = "Ejemplo de gráfico con eje secundario", x = "Fecha", y = "Valor")

No matter what I do in scale_y_continuos, in especific inside of:
sec_axis.(~.*15...
I am just changing the labels of the secondary axis, but in no way scaling the data to plot.
Is it possible to do that on tidy data?
Or I need to separate the columns in unemploy and pasavert?
I can't find infor about this issue.

As always, thanks four time and interest.

Hello,
Is this correct?

economics_tidy %>% 
  mutate(value=if_else(variable=="psavert",value*1000,value)) %>% 
  ggplot(aes(x = date, y = value, color = variable)) +
  geom_line() +
  scale_x_date(date_labels = "%Y-%m") +
  scale_y_continuous(name = "unemploy", sec.axis = sec_axis(~./1000 , name = "psavert"))+
  labs(title = "Ejemplo de gráfico con eje secundario", x = "Fecha", y = "Valor")

Without some test data it is difficulte to say.

Could you supply some sample data? A handy way to supply some sample data is the dput() function. In the case of a large dataset something like dput(head(mydata, 100)) should supply the data we need. Just do dput(mydata) where mydata is your data. Copy the output and paste it here.

Economics is a dataset that provides ggplot.

Economics does not seem to be a common data set. What packages (libraries) do you load?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.