how to use ggplot, geom_line and geom_ribbon to draw multiple y axis in a layout

219 Views Asked by At

There are different data sets as bottom.

1-1.Data Set(cidf_min.csv)
name number value samples conf lower upper level
apple 1 0.056008 100 0.95 0.05458 0.059141 2
apple 2 0.048256 100 0.95 0.046363 0.059142 2
apple 3 0.042819 100 0.95 0.040164 0.059143 2
apple 4 0.038663 100 0.95 0.035155 0.059144 2
apple 5 0.035325 100 0.95 0.030146 0.059145 2
1-2.Data Set(newdf_min.csv)
name number value samples conf lower upper level max
apple 2 0.01854 100 0.95 -0.06963 0.045235 2 2
  '''code'''
  cidf<-read.csv("D:/cidf_min.csv")
  newdf<-read.csv("D:/newdf_min.csv")
  p_min<-ggplot(cidf, aes(x=number, y=value, group=name))+geom_line(aes(color=level))+geom_ribbon(aes(ymin=lower, ymax=upper, fill=level, group=name), alpha=0.3)+geom_text(data=newdf, aes(label=name, color=level), hjust=-0.2, vjust=0.5, size=3, show.legend=F)+coord_cartesian(xlim=c(0,max(cidf$number)*1.2))+xlab(~"Con (\u00D7"~C[max]*")")+ylab(~"score ("*mu*"C/"*mu*"F)")+theme_bw()
2-1.Data Set(cidf_max.csv)
name number value samples conf lower upper level
apple 1 0.068832 100 0.95 0.061945 0.069416 2
apple 2 0.065256 100 0.95 0.053687 0.065841 2
apple 3 0.060492 100 0.95 0.046201 0.06155 2
apple 4 0.05585 100 0.95 0.039848 0.058739 2
apple 5 0.047585 100 0.95 0.033555 0.056066 2
2-2.Data Set(newdf_max.csv)
name number value samples conf lower upper level max
apple 2 0.024221 100 0.95 -0.04546 0.076362 2 2
  '''code'''
  cidf<-read.csv("D:/cidf_max.csv")
  newdf<-read.csv("D:/newdf_max.csv")
  p_max<-ggplot(cidf, aes(x=number, y=value, group=name))+geom_line(aes(color=level))+geom_ribbon(aes(ymin=lower, ymax=upper, fill=level, group=name), alpha=0.3)+geom_text(data=newdf, aes(label=name, color=level), hjust=-0.2, vjust=0.5, size=3, show.legend=F)+coord_cartesian(xlim=c(0,max(cidf$number)*1.2))+xlab(~"Con (\u00D7"~C[max]*")")+ylab(~"score ("*mu*"C/"*mu*"F)")+theme_bw()
3-1.Data Set(cidf_mean.csv)
name number value samples conf lower upper level
apple 1 0.069673 100 0.95 0.069673 0.069673 2
apple 2 0.06133 100 0.95 0.057955 0.062792 2
apple 3 0.060497 100 0.95 0.046201 0.06155 2
apple 4 0.054623 100 0.95 0.044241 0.058739 2
apple 5 0.039852 100 0.95 0.031906 0.043719 2
3-2.Data Set(newdf_mean.csv)
name number value samples conf lower upper level max
apple 2 0.014323 100 0.95 -0.06793 0.045717 2 2
  '''code'''
  cidf<-read.csv("D:/cidf_mean.csv")
  newdf<-read.csv("D:/newdf_mean.csv")
  p_mean<-ggplot(cidf, aes(x=number, y=value, group=name))+geom_line(aes(color=level))+geom_ribbon(aes(ymin=lower, ymax=upper, fill=level, group=name), alpha=0.3)+geom_text(data=newdf, aes(label=name, color=level), hjust=-0.2, vjust=0.5, size=3, show.legend=F)+coord_cartesian(xlim=c(0,max(cidf$number)*1.2))+xlab(~"Con (\u00D7"~C[max]*")")+ylab(~"score ("*mu*"C/"*mu*"F)")+theme_bw()



  

I already drew 3 plots using code of ggplot, geom_line and geom_ribbon etc.

  1. I want to merge plots of p_min, p_max and p_mean.

  2. p_min, p_max and p_mean must locate in y axis.

  3. x axis is number(1,2,3,4,5).

Let me know how to draw plots of multiple y axis using complex variables in a layout.

1

There are 1 best solutions below

0
yuliaUU On

What I did I first renamed each dataset to cidf1,cidf2, and cidf3 so that they won't mix. and the same for newdf. I added a column type that basically has info of what type of graph it is. I was not sure whether you know the tidyvesre, so I used basic R to add a column using $ operator

#Set 1
cidf1 <- read.csv("cidf_min.csv")
cidf1$type ="P_min"
newdf1<-read.csv("newdf_min.csv")
newdf1$type ="P_min"
 
#Set 2
cidf2<-read.csv("cidf_max.csv")
cidf2$type ="P_max"
newdf2<-read.csv("newdf_max.csv")
newdf2$type ="P_max"

#Set 3
cidf3<-read.csv("cidf_mean.csv")
cidf3$type ="P_mean"
newdf3<-read.csv("newdf_mean.csv")
newdf3$type ="P_mean"
  

then I combined them together:

cidf = rbind(cidf1,cidf2,cidf3)
newdf =rbind(newdf1,newdf2,newdf3)

And plotted them, setting color=type to color each line according to the dataset. I removed other things that you had in your ggplot as they were not related to your question asked.

ggplot(cidf, aes(x=X, y=value))+geom_line(aes(color=type)) +
  geom_ribbon(aes(ymin=lower, ymax=upper, fill=type), alpha=0.3) +
  xlab(~"Con (\u00D7"~C[max]*")")+ylab(~"score ("*mu*"C/"*mu*"F)")+theme_bw()

So it is very close to what you are looking for. Let me know if I misinterpret what you want to do and I will update my code