0

Hi I was looking as crazy but I can´t find how put a geom bar and geom line on the same chart if they have different database and different axis, I have a code but the geom line does not correspond to the second axis. My code is this:

library(ggplot2)
library(ggthemes)                                                             
library(scales)

Mes<- "Agosto"

AXIS1_MIN = min(Base1$Porcentaje)                                                                   
AXIS1_MAX = max(Base1$Porcentaje)                                                                    
AXIS2_MIN = min(Base2$Porcentaje_Activo)                                                                    
AXIS2_MAX = max(Base2$Porcentaje_Activo)

stv_1<- function(Porcentaje) rescale(Porcentaje, to = c(AXIS1_MIN, AXIS1_MAX))                                                                      
stv_2<- function(Porcentaje_Activo) rescale(Porcentaje_Activo, to = c(AXIS2_MIN, AXIS2_MAX))   

x<-ggplot()+ scale_fill_economist()+ 
geom_bar(aes( y= Porcentaje, x= Fecha, fill= Producto), data= Base1, stat="identity")+
geom_line(data=Base2 ,aes( y = stv_1(Porcentaje=Porcentaje_Activo), x=Fecha_, group=Producto_),size=.6)+
geom_point(data=Base2 ,aes( y=stv_1(Porcentaje=Porcentaje_Activo), x=Fecha_, group=Producto_),size=2.25)+
scale_y_continuous(labels = scales::percent,limits = c(-1,1),sec.axis = sec_axis( ~ stv_2(.), labels = scales::percent, name = "Rendimiento Activo"))+
scale_x_date(breaks = seq(as.Date("2017-01-31"), as.Date("2017-07-31"), by="month"), labels=date_format("%d-%m"))+

plot(x)
Greg
  • 1
  • 1
  • 2
    Welcome to StackOverflow. Please read [How to make a great reproducible example in R?](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to make your question more likely to get an answer. Also you may want to take the [tour](https://stackoverflow.com/tour) and read [how to ask?](https://stackoverflow.com/help/how-to-ask) – M-- Aug 31 '17 at 19:11
  • There's too much irrelevant code here. We don't need to know where you stored your files or how you processed them. Providing a minimal example of what each dataset looks like *at the point when you're ready to use it* in ggplot would be more straightforward. – Z.Lin Sep 01 '17 at 04:39

0 Answers0