Reputation: 11
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
stepPlot <- 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 <- min(Data[xVar])-1
Xmax <- max(Data[xVar])+1
BreakIncreX <- round((Xmax-Xmin)/6)
GroupBreaks <-unique(Data$trt_label)
#labelPosiX <-min(Data[xVar])+2
# define y maximal limit (limitYMax),y break increments (BreakIncreY)
library(plyr)
limitYMax <- round_any(max(Data[yVar]), 100, f = ceiling)
BreakIncreY <- 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)** }
}
My old way to add text works but I am hoping to move the geom_text into the function.
source("C:/Now/R/Rfunction_stepPlot.R")
fig17b <-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)
Upvotes: 1
Views: 2566
Reputation: 22496
Here's one way you can pass the (x,y) positions of geom_text
to a function:
In general, make it a part of the data frame that ggplot is plotting. (Using SimonO101's suggestion, here's how it works for mtcars
.)
plotFunction <- function (df, labelPosiY) {
df$xPos = df$cyl #add columns to the data frame
df$yPos = labelPosiY
p <- ggplot(data=df, aes(x=cyl, y=mpg)) + geom_step(aes(colour=gear, direction='hv',size=2))
p <- p + geom_text(aes(xPos, y=yPos, label="test"), colour="black",size=5)
return (p)
}
Now, calling
plotFunction(mtcars, 17)
produces
You can try making the geom_text
part of your code work, and then bringing in all the other aspects of your plot.
Upvotes: 1