2

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:

enter image description here

and

enter image description here

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

matteo
  • 4,683
  • 9
  • 41
  • 77
  • I don't know of any really _good_ options. A little searching turned up [this](http://stackoverflow.com/q/7198178/324364). Alternatively, you could not do a legend at all and instead using something like the **directlabels** package to label individual lines. – joran Mar 19 '15 at 19:20

0 Answers0