I know this topic has arisen some time in different threads of this page, but I am afraid that following the instructions of all of them I have not managed to fix it. I have been trying to solve this problem for a week that seems quite trivial and I can not find the way.
I do not know if it's about differences in the graphics or that there is something I do wrong. The case is as follows. I have two graphics using the ggplot2
package:
library(ggplot2)
data<-data.frame(Age=0,var2=0,var1=0,inf=0,sup=0,ppv=0)
data[1,]<-c(1,1,0.857,0.793,0.904,0.03)
data[2,]<-c(1,2,0.771 ,0.74,0.799,0.056)
data[3,]<-c(1,3,0.763 ,0.717,0.804,0.06)
data[4,]<-c(1,4,0.724 ,0.653,0.785,0.09)
data[5,]<-c(2,1,0.906,0.866,0.934,0.055)
data[6,]<-c(2,2,0.785 ,0.754,0.813,0.067)
data[7,]<-c(2,3,0.660,0.593,0.722,0.089)
data[8,]<-c(2,4,0.544,0.425,0.658,0.123)
pd <- position_dodge(0.2) #
names(data)<-c("Age","var2","var1","inf","sup","ppv")
data$Age<-as.character(data$Age)
data$var2<-as.character(data$var2)
p<- ggplot(data, aes(x=var2, y=var1, colour=Age)) +
geom_errorbar(aes(ymin=inf, ymax=sup), width=.1 , position=pd) +
geom_line(position=pd,aes(group=Age),linetype=c("dashed")) +
geom_point(position=pd,size=3) +
theme_light()+
ylim(0,1) +
scale_color_manual(values=c("1"="grey55","2"="grey15"))+guides(fill=guide_legend(nrow=2,byrow=TRUE)
)
s<- ggplot(data, aes(x=var2, y=ppv, colour=Age)) +
geom_line(position=pd,aes(group=Age),linetype=c("dashed")) +
geom_point(position=pd,size=3) +
theme_light()+
ylim(0,0.2) + scale_color_manual(values=c("1"="grey55","2"="grey15"))+guides(fill=guide_legend(nrow=2,byrow=TRUE)
)
They look like this:
I was wondering if someone would know the way to put them together in a single graph, with the two scales that they currently have, for example, the y axis of the graph p at the left side and the y axis of the graph s at the right side since I can not directly draw both data in a graph due to the radical difference in the scales .
Thank you very much for your time,
Best regards,