If your data is already ordered as you want, you can use forcats::fct_inorder()
ggplot(p3_tab1_2, aes(fill=tipo_trenes, y=trenes, x=fct_inorder(lineas))) +
....
If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.