I'm trying to do the same graph over multiple dataframes that have the same variables with different values. I have n dataframes called df_1, df_2 ... df_n and my code goes like this :
#Create dataframes(In this example n = 3)
df_1 <- data.frame(a1 = 1:1000,
b1 = 1:1000)
df_2 <- data.frame(a1 = 1:1000,
b1 = 1:1000)
df_3 <- data.frame(a1 = 1:1000,
b1 = 1:1000)
##Store dataframes in list
example.list<-lapply(1:3, function(x) eval(parse(text=paste0("df_", x)))) #In order to store all datasets in one list using their name
names(example.list)<-lapply(1:3, function(x) paste0("df_", x))
#Graph and save for each dataframe
for (i in example.list){
benp <- ggplot(i, aes(x=b1)) +
geom_histogram(fill="steelblue", aes(y=..density.., alpha=..count..), bins=60) +
labs(title="Beneficios", subtitle="") + ylab("Densidad") +
xlab("Beneficios ($millones)") +
geom_vline(aes(xintercept=mean(b1)), color="red4",linetype="dashed") +
theme(legend.position = "none") +
annotate("text", x= mean(b1), y=0, label=round(mean(b1), digits = 2),
colour="red4", size=3.5, vjust=-1.5, hjust=-0.5)
ggsave(benp, file=paste0(i,"_histogram.png"))
}
I'm getting error message "Error in mean(b1): object b1 not found". I don't know how to tell R that b1 comes from dataframe i. Does anybody knows what's wrong with my code or if there is some easier way to plot over multiple dataframes? Thanks in advance!
question from:https://stackoverflow.com/questions/66054201/ggplot-over-multiple-dataframes-with-loops