[R] Automaticall adjust axis scales

R. Michael Weylandt michael.weylandt at gmail.com
Thu Mar 22 18:17:39 CET 2012


Simply calculate it before.... it's not hard:

ylim = range(sapply(datalist, function(x) range(x$dataset$Utilization)))

should work, but it's untested.  Then pass that to plot().

If your data set is large enough that you want to do a single loop,
simply create a list of the density objects and get the bounds
appropriately after that.

Michael

On Thu, Mar 22, 2012 at 1:10 PM, Alaios <alaios at yahoo.com> wrote:
> Hmm.. this how my code looks like inside my function
>
> plot(density(100*datalist[[1]]$dataset$Utilization), xlab="Overall utilization [ % ]", main="", ...)
>     colorList=colors()[seq(10,100,10)]
>     colorList[1]="black"
>     if (length(datalist) > 1) {
>         for (i in 2:length(datalist)) {
>             lines(density(100*datalist[[i]]$dataset$Utilization), lty=i, col= colorList[i])
>         }
>     }
>
> given on input a datalist the code above just by reading element by element of the list adds the corresponding line(..)
>
> I fail to see how I can calculate the min and max and then call the first plot with the right lim.
>
> Could you please help me?
> Regards
> Alex
>
>
>
> ________________________________
>  From: Jim Lemon <jim at bitwrit.com.au>
>
> Cc: R help <R-help at r-project.org>
> Sent: Thursday, March 22, 2012 8:13 AM
> Subject: Re: [R] Automaticall adjust axis scales
>
> On 03/22/2012 05:42 AM, Alaios wrote:
>> Would it be possible to change the axis at the end?
>> My data sets seem to be quite large so I was thinking for the plot and
>> the consequent lines to keep always
>> the current minimum and maximum like
>> plot(x)
>> minimum=min(x)
>> lines(x1)
>> minimum=c(minimum,x1)
>> lines(x2)
>> minimum=c(minimum,x2)
>> then if there is a good way for altering axis lines I could put the
>> limits I want to.
>
> Hi Alex,
> One way or another you are going to have to set the limits in your first call to "plot". You could do the plot first and collect the limits along the way, then do it again, but I can't see that this has any advantage over calculating the limits for the entire set of data that is to be plotted at the beginning and then doing the plots. If you want to get the limits for each data set separately, maybe:
>
> range1<-range(x1)
> range2<-range(x2)
> range3<-range(x3)
> ylim<-range(c(range1,range2,range3))
>
> However, I suspect that this is of more use in helping you understand what is happening than actually getting it done.
>
> Jim
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list