title: how to tweak geom_text() in a ggplot2 function in R
body: <p>I am creating a step plot using a self-defined function "stepPlot" which is working. I got stuck when I attempted to put a geom_text() inside this function. Interestingly, geom_text() works when it's not in the function. Can any one help me tweak the geom_text(). There are two parts: (1) "labelPosiX" is the horizontal position of the label text, and (2) geom_text() where is at the end of the function. "labelPosiY", the vertical position of the label, will be manually specified with a number. These two lines of codes have been inactivated. Thanks in advance</p>
  
  <pre><code>stepPlot &lt;- function(Data,xVar, yVar, LegendTitle="", GroupLabels, Plottitle="",labelPosiY, labelText="A"
                       # plot specifications remain the same over data subsets. Ignore these setting when calling the function  
                       GroupColour=c("black","blue","orange"), LineTypeGroup=c("solid","solid","solid"), LineSize=1,
                       LegendPosition=c(0.5,0.8),
                       YaxisTitle="", YAxisTitleSize=element_blank(),
                       XAxisText=element_text(size=20),AxisTextSize=15,LegendTitleSize=10, LegendTextSize=10,LegendKeySize=10,
                       PlotTitleSize=15
                        ){
  # define x limits (Xmin, Xmax), x break increments (BreakIncreX),level of breaks (GroupBreaks),horizontal position of label text (labelPosiX)
    Xmin &lt;- min(Data[xVar])-1 
    Xmax &lt;- max(Data[xVar])+1
    BreakIncreX &lt;- round((Xmax-Xmin)/6)
    GroupBreaks &lt;-unique(Data$trt_label) 
    #labelPosiX &lt;-min(Data[xVar])+2
  
  # define y maximal limit (limitYMax),y break increments (BreakIncreY)   
    library(plyr)
    limitYMax &lt;- round_any(max(Data[yVar]), 100, f = ceiling)
    BreakIncreY &lt;- round_any(max(Data[yVar])/5, 100, f = ceiling)
  
  
  # step plot  
    ggplot(Data, aes_string(x=xVar, y=yVar, group='trt_label'))+
    geom_step(aes(colour=trt_label, linetype=trt_label), direction='hv',size= LineSize)+ #specify step curve from different group with colours, colour by default
    scale_y_continuous(YaxisTitle, limits=c(0,limitYMax), expand=c(0,0), breaks=seq(0,limitYMax,by=BreakIncreY))+
    scale_x_continuous("Age of adults in days", limits=c(Xmin, Xmax), expand=c(0,0), breaks=seq(Xmin,Xmax,by=BreakIncreX)) +
    scale_colour_manual(name=LegendTitle,
                                      breaks=GroupBreaks,
                                      labels=GroupLabels,
                                      values=GroupColour
                        )+  # change default colours to manually specified grey scale
    scale_linetype_manual(name  =LegendTitle,
                          breaks=GroupBreaks,
                          labels=GroupLabels,
                          values=LineTypeGroup
                          )+    
    guides(colour = guide_legend(LegendTitle), linetype = guide_legend(LegendTitle))+ # merge two legends into a single one                                           
    theme_bw() +   # maek background theme black and white                   
    theme(axis.title.x = element_blank(), #font size of x axis title
          axis.title.y = YAxisTitleSize, #font size of y axis title
          axis.text.x  = XAxisText,                              #font size of x axis text 
          axis.text.y  = element_text(size=AxisTextSize),                              #font size of y axis text  
          legend.position=LegendPosition,
          legend.title=element_text(size=LegendTitleSize),                                #font size of legend title
          legend.text = element_text(colour="black", size = LegendTextSize, face = "bold"), #font size of legend text
          legend.key.size=unit(LegendKeySize,'points'), ## ben - added to shrink the legend 
          legend.background=element_blank(), ## ben - added to get rid of white background
          panel.grid.major = element_line(size = 0.5, colour = '#FFFFFF'),
          panel.grid.minor = element_line(colour = NA), # colour = NA to suppress gridlines, reappear if colour='black'
          plot.title=element_text( face="bold", size=PlotTitleSize) # aduust plot title size
          )+
    ggtitle(Plottitle)
  
    # add label text
    **#+ geom_text(aes(labelPosiX, labelPosiY, label="test"), colour="black",size=5)**                                   }
                                           }
  </code></pre>
  
  <p>My old way to add text works but I am hoping to move the geom_text into the function. </p>
  
  <pre><code>source("C:/Now/R/Rfunction_stepPlot.R")
  fig17b &lt;-stepPlot(Data=df17b,xVar= "age", yVar='mean_cumSumDurLeftByBeeAge', 
  LegendTitle="Precocious topical",GroupLabels=c("acetone", "untreated", "methoprene"),
  Plottitle="weighed hive"
  )+
  geom_text(aes((min(df17b$age)+2), 3700, label="A"), colour="black",size=5)
  </code></pre>