简体   繁体   English

如何使用 ggplot、geom_line 和 geom_ribbon 在布局中绘制多个 y 轴

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

There are different data sets as bottom.底部有不同的数据集。

1-1.Data Set(cidf_min.csv) 1-1.数据集(cidf_min.csv)
name姓名 number数字 value价值 samples样品 conf配置文件 lower降低 upper level等级
apple苹果 1 1 0.056008 0.056008 100 100 0.95 0.95 0.05458 0.05458 0.059141 0.059141 2 2
apple苹果 2 2 0.048256 0.048256 100 100 0.95 0.95 0.046363 0.046363 0.059142 0.059142 2 2
apple苹果 3 3 0.042819 0.042819 100 100 0.95 0.95 0.040164 0.040164 0.059143 0.059143 2 2
apple苹果 4 4 0.038663 0.038663 100 100 0.95 0.95 0.035155 0.035155 0.059144 0.059144 2 2
apple苹果 5 5 0.035325 0.035325 100 100 0.95 0.95 0.030146 0.030146 0.059145 0.059145 2 2
1-2.Data Set(newdf_min.csv) 1-2.数据集(newdf_min.csv)
name姓名 number数字 value价值 samples样品 conf配置文件 lower降低 upper level等级 max最大限度
apple苹果 2 2 0.01854 0.01854 100 100 0.95 0.95 -0.06963 -0.06963 0.045235 0.045235 2 2 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 (\×"~C[max]*")")+ylab(~"score ("*mu*"C/"*mu*"F)")+theme_bw()
2-1.Data Set(cidf_max.csv) 2-1.数据集(cidf_max.csv)
name姓名 number数字 value价值 samples样品 conf配置文件 lower降低 upper level等级
apple苹果 1 1 0.068832 0.068832 100 100 0.95 0.95 0.061945 0.061945 0.069416 0.069416 2 2
apple苹果 2 2 0.065256 0.065256 100 100 0.95 0.95 0.053687 0.053687 0.065841 0.065841 2 2
apple苹果 3 3 0.060492 0.060492 100 100 0.95 0.95 0.046201 0.046201 0.06155 0.06155 2 2
apple苹果 4 4 0.05585 0.05585 100 100 0.95 0.95 0.039848 0.039848 0.058739 0.058739 2 2
apple苹果 5 5 0.047585 0.047585 100 100 0.95 0.95 0.033555 0.033555 0.056066 0.056066 2 2
2-2.Data Set(newdf_max.csv) 2-2.数据集(newdf_max.csv)
name姓名 number数字 value价值 samples样品 conf配置文件 lower降低 upper level等级 max最大限度
apple苹果 2 2 0.024221 0.024221 100 100 0.95 0.95 -0.04546 -0.04546 0.076362 0.076362 2 2 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 (\×"~C[max]*")")+ylab(~"score ("*mu*"C/"*mu*"F)")+theme_bw()
3-1.Data Set(cidf_mean.csv) 3-1.数据集(cidf_mean.csv)
name姓名 number数字 value价值 samples样品 conf配置文件 lower降低 upper level等级
apple苹果 1 1 0.069673 0.069673 100 100 0.95 0.95 0.069673 0.069673 0.069673 0.069673 2 2
apple苹果 2 2 0.06133 0.06133 100 100 0.95 0.95 0.057955 0.057955 0.062792 0.062792 2 2
apple苹果 3 3 0.060497 0.060497 100 100 0.95 0.95 0.046201 0.046201 0.06155 0.06155 2 2
apple苹果 4 4 0.054623 0.054623 100 100 0.95 0.95 0.044241 0.044241 0.058739 0.058739 2 2
apple苹果 5 5 0.039852 0.039852 100 100 0.95 0.95 0.031906 0.031906 0.043719 0.043719 2 2
3-2.Data Set(newdf_mean.csv) 3-2.数据集(newdf_mean.csv)
name姓名 number数字 value价值 samples样品 conf配置文件 lower降低 upper level等级 max最大限度
apple苹果 2 2 0.014323 0.014323 100 100 0.95 0.95 -0.06793 -0.06793 0.045717 0.045717 2 2 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 (\×"~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.我已经使用ggplotgeom_linegeom_ribbon等代码绘制了 3 个图。

  1. I want to merge plots of p_min , p_max and p_mean .我想合并p_minp_maxp_mean

  2. p_min , p_max and p_mean must locate in y axis. p_minp_maxp_mean必须位于 y 轴。

  3. x axis is number(1,2,3,4,5). x 轴是数字(1,2,3,4,5)。

Let me know how to draw plots of multiple y axis using complex variables in a layout.让我知道如何在布局中使用复杂变量绘制多个 y 轴的图。

What I did I first renamed each dataset to cidf1,cidf2, and cidf3 so that they won't mix.我首先将每个数据集重命名为 cidf1、cidf2 和 cidf3,这样它们就不会混合。 and the same for newdf. newdf 也是一样。 I added a column type that basically has info of what type of graph it is.我添加了一个列type ,它基本上包含它是什么类型的图形的信息。 I was not sure whether you know the tidyvesre, so I used basic R to add a column using $ operator我不确定你是否知道 tidyvesre,所以我使用基本的 R 使用 $ 运算符添加一列

#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.并绘制它们,设置color=type以根据数据集为每条线着色。 I removed other things that you had in your ggplot as they were not related to your question asked.我删除了您在 ggplot 中的其他内容,因为它们与您提出的问题无关。

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如果我误解了你想要做什么,请告诉我,我会更新我的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM