1

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:

Image of p enter image description here

Image of s enter image description here

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,

Barbara
  • 1,118
  • 2
  • 11
  • 34
J Louro
  • 33
  • 1
  • 7

1 Answers1

2

try this code, you should set aes at new layer.

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) +
  geom_line(position = pd, aes(x = var2, y = ppv * 5, colour = Age, group = Age), linetype = c("dashed"), data = data) +
  geom_point(aes(x = var2, y = ppv * 5, colour = Age, group = Age), position = pd, size = 5) +
  theme_light() +
  scale_color_manual(values = c("1" = "grey55", "2" = "grey15")) + 
  scale_y_continuous(sec.axis = sec_axis(~./5)) +
  guides(fill = guide_legend(nrow = 2, byrow = TRUE))

enter image description here

myincas
  • 1,500
  • 10
  • 15
  • Hello Myincas, Thanks for the reply, but I'm afraid the question is another. Being that variable in a smaller scale what I wanted was that there were two scales, on the right y axis would be the scale of the variable ppv (chart s) that would go from 0 to 0.2 and to the left y axis the scale of the variable var1 (chart p) that would go from 0 to 1. Thus the differences between the groups in the variable ppv would be better appreciated, than if they do not come out very small. Best Regards, – J Louro Nov 30 '17 at 09:58
  • oh, i misunderstood your question. `scale_y_continuous(sec.axis = sec_axis())` can add second axis in your plot and `scale` your data. – myincas Nov 30 '17 at 10:15