long time listener and first time caller here
For a project I am working on, I often end up graphing the same graphs with just different response variables. So I am trying to write a function based on a ddply()
code and a ggplot()
code I keep reusing:
(df.smpl is the dataframe I am working with, genotype is the treatment I am interested in, and var is the stand-in for a response variable I am interested in)
const.gra<-function(var){
## First, summarise the data to be used in subsequent ggplot code
summ<-ddply(df.smpl, "genotype", summarise,
N = length(var),
mean = mean(var),
sd = sd(var),
se = sd/sqrt(N))
# Now graph
ggplot(data=summ, aes(genotype, mean))+
geom_col(position = "dodge")+
geom_errorbar(aes(ymin=mean-se, ymax=mean+se),
width=.2,
position=position_dodge(.9))+
scale_x_discrete(name = "Genotype",
breaks=c("K","PW", "AW"),
labels=c("Plant K", "Plant PW", "Plant AW"))+
scale_y_continuous(name = "Title")+
theme(legend.position = "none",
legend.justification = c(1,1),
panel.background = element_rect(fill = "white"),
legend.key = element_rect(fill = "white"),
axis.line = element_line(colour = "black"),
axis.ticks.x = element_blank(),
axis.text = element_text(size = 14),
axis.title = element_text(size = 14),
legend.text = element_text (size = 14),
legend.title = element_text (size = 14))
}
const.gra(df.smpl$bgbm..mg.)
But the above codes yield the following error messages.
Error in as.double(x) : cannot coerce type 'closure' to vector of type 'double' In addition: Warning message: In mean.default(var) : argument is not numeric or logical: returning NA
Tried solving it on my own but have been very unsuccessful so far. The codes run just fine if I were to run them verbatim outside of the function.
Based on some answers I have found online re: the error code, I tried subbing out some strings that sounded like they could be common base r function names or something, but no luck thus far... :(