With help from @Nick Criswell I have learned how to create a make plot function and lapply using ggplot2 and aes_string. loop Freq plot with GGPLOT aes_string
I am having trouble modifying the code to only plot values greater than a given threshold. Before using the function approach I coded the following that worked:
p <- ggplot(subset(DAT, al.sum>10), aes(word, al.sum, y=freq))
Question: how do I modify the following code to only plot values over a given threshold? i.e. I only want each record to be included in the frequency plot if the count is greater than 10. From the code below with this condition for al.sum only Dot_Matricks would be included in the plot.
#set work DIR
setwd("C:/A")
#Data create
DAT <- read.table(text = "ID a1.sum b3.sum c6.sum d9.sum
April_Showers 10 5 15 0
Anita_Job 2 3 1 14
Candy_Cain 4 7 14 17
Crystal_Ball 6 8 16 12
Dot_Matricks 15 9 0 1
Kay_Largo 4 10 5 13",
header = TRUE, stringsAsFactors = FALSE)
#Plot Data function
library(ggplot2)
make_plots = function(data, column){
ggplot(data, aes_string(x = "ID", y=column)) +
geom_bar(stat="identity", fill="blue", color="green") +
theme(plot.background = element_rect(fill = "white"),
panel.background = element_rect(fill = "white"),
panel.grid.major = element_line(colour = "white",size=0.25),
panel.grid.minor = element_blank(),
axis.text.x=element_text(size=10,angle=90, hjust=1,
face="plain", family="serif"),
axis.text.y=element_text(size=10, hjust=1, face="plain", family="serif"),
axis.line.x = element_line(color="black", size = 0.50),
axis.line.y = element_line(color="black", size = 0.5))
}
#lapply function
myplots <- lapply(colnames(DAT[2:ncol(DAT)]), make_plots, data = DAT)
#collect names of each col that end with .sum
n <- names(DAT[grep("*.sum",names(DAT))])
#save each plot as .png
for (i in 1:length(n)){
print(myplots[i])
ggsave(filename=paste0(i,".png"))
}