0

I am trying to make a dual y-graph with ggplot on R, however my lines do not respond with changes in the secondary axis.

theme_set(theme_economist())

ggplot(Base) + aes(data) + geom_smooth(aes(y = taxa_consumo))

g1 = ggplot(Base) + aes(data) + geom_smooth(aes(y = saldo_consumo, col = "Saldo da carteira para consumo"))

g2 = g1 + geom_smooth(aes(y = taxa_consumo, col = "Taxa média para consumo"))

g3 = g2 + scale_y_continuous(sec.axis = sec_axis(~./100, name = "Taxa média para consumo"))

g4 = g3 + scale_color_manual(values = c("brown", "gold")) +
  labs(title = "Saldo e Taxa de Microcrédito para Consumo", x = NULL, colour = NULL,
       y = "Saldo da carteira para consumo")

g4

That is my result. Someone has a suggestion?

enter image description here

aosmith
  • 34,856
  • 9
  • 84
  • 118
  • Stack Overflow has quite a few questions & answers on how to create a secondary (y-) axis. Please take a look at the linked posts. They provide details on how to correctly determine the axis transformation. – Maurits Evers Nov 05 '21 at 00:06
  • FYI, secondary axes in ggplot are annotations that have no bearing on the appearance of your data. If you want data to look like it relates to the secondary axis, you need to scale it yourself based on the primary axis. – Jon Spring Nov 05 '21 at 00:08

0 Answers0