I looked through many answers, but still have problems programming a function for a plot in ggplot2. Here is a sample dataset:
d<-data.frame(replicate(2,sample(0:9,1000,rep=TRUE)))
colnames(d)<-c("fertilizer","yield")
Now I write the following function - I only want to give x and y to the function:
test <- function(explanatory,response)
{
plot<- ggplot(d, aes(x =explanatory, y=response)) +
geom_point()+
ggtitle("Correlation between Fertilizer and Yield") +
theme(plot.title = element_text(size = 10, face = "bold"))+
geom_smooth(method=lm, se=FALSE) +
annotate("text", x=800, y=20, size=5,label= cor6)
plot
}
When I call this function with this,
test("fertilizer","yield")
I get a graph without any scatter points like this:
Could anybody help me? I really want to learn to write functions in R.