Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将函数传递给ggplot密度图_R_Ggplot2_Label_Density Plot - Fatal编程技术网

R 将函数传递给ggplot密度图

R 将函数传递给ggplot密度图,r,ggplot2,label,density-plot,R,Ggplot2,Label,Density Plot,我有三个data.frames和一个字符串作为函数的输入,我需要函数在其中一个dataframes中搜索字符串,并在密度图中标记相应的值。bg0和bg1有一列x和,我用它来绘制密度图 bg0 = data.frame(x = c(1.2, 2.567, 0.9188, 0.52623)) bg1 = data.frame(x = c(7.98, 9.867, 4.678, 2.877)) matrix = data.frame(sample_id = c(AA, BB, CC, FF, EE),

我有三个data.frames和一个字符串作为函数的输入,我需要函数在其中一个dataframes中搜索字符串,并在密度图中标记相应的值。bg0和bg1有一列x和,我用它来绘制密度图

bg0 = data.frame(x = c(1.2, 2.567, 0.9188, 0.52623))
bg1 = data.frame(x = c(7.98, 9.867, 4.678, 2.877))
matrix = data.frame(sample_id = c(AA, BB, CC, FF, EE),
                    BF = c(1.2, 2.567, 5.98, 7.098, 10.987))
例如,如果搜索字符串为AA,则程序必须在
矩阵
数据框中搜索该字符串,并在密度图上标记其
样本id
BF

这是我试过的

plotter <- function(bg0, bg1, matrix, string){
  if (string %in% matrix$sample_id) {
    p1 = ggplot(data = bg0, aes(x=x,fill = "blue")) + 
      geom_density(alpha = .3) + 
      geom_density(data = bg1, aes(x=x,fill = "green")) + 
      geom_label(label=sprintf('n = %s', matrix$sample_id))
    pdf(outfile, width=50, height=20)
    print(p1)
    dev.off() 
  }}
绘图仪已编辑:

plotter <- function(bg0, bg1, matrix, string){
                  if (nrow(matrix[which(matrix$sample_id==string),])!=0) {
                   mylabel = paste('BF = ',matrix[which(matrix$sample_id==string),]$BF,sep=" ")
                   p1 = ggplot(data = bg0, aes(x=x,fill = "blue") )+ 
                   geom_density(alpha = .3) + 
                   geom_density(data = bg1, aes(x=x,fill = "green"))+
                   geom_vline(xintercept = matrix[which(matrix$sample_id==string),]$BF, 
                   linetype="dotted", color = "blue", size=1.5)+
                   geom_text(aes(x=matrix[which(matrix$sample_id==string),]$BF, 
                   label=mylabel, y=.6), colour="blue",hjust = -0.5) 
                  #pdf(outfile, width=50, height=20)
                  return(print(p1))
                  dev.off() 
                  }
             }
  plotter(bg0, bg1, matrix, "BB")

绘图仪我看不到ggplot代码中使用的BF?!我想在geom_标签()中的矩阵$sample旁边使用它,那么您是否希望标题为n=1.2 AA?不,我希望在密度图上的BF=1.2位置Hanks Shirin用于代码,但我需要在密度图上标记这1.2,如中所示,标签应位于密度图上BF 1.2的确切位置,不是标题。因此,在这种情况下,标签应该位于绿色分发的某个位置。完成!您还可以使用几何点而不是几何线,并选择y作为标签显示的位置!完美的非常感谢。