this is my small script in order to make some batch plot in ggplot:
by(database_per_grafici, database_per_grafici$variable, function(i){
ggplot(subset(i, !is.na(value)))+
stat_ecdf(aes(x=value, color=gruppo), size=2)+
scale_y_continuous(labels = percent) +
theme_bw() +
theme(panel.grid.major = element_line(colour = "black", size= 0.3))+
theme(legend.text = element_text(size = 60)) +
theme(legend.justification=c(1,0), legend.position=c(1,0))+
theme(legend.title=element_blank()) +
theme(axis.text.x = element_text(size=16)) +
theme(axis.title.x = element_text(size=20))+
xlab("mg/kg")+
theme(axis.text.y = element_text(size=16)) +
theme(axis.title.y = element_blank()) +
guides(colour = guide_legend(override.aes = list(size=10))) +
ggsave(sprintf("%s.png", unique(i$variable), width = 30,
height = 20, units = "cm"))
})
but there is a problem with the legend as you can see in the two following pictures:
and
the problem is that in some plots, the legend overlays the plot itself.
Is there a way to set the legend position according to the dataframe values?
Thanks