[R] plotting multiple data sources

Greg Snow Greg.Snow at imail.org
Thu May 14 17:22:17 CEST 2009


It is not recommended to plot variables on different scales on the same graph.  You can use something like par(mfrow=...) (see ?par) to stack graphs on top of each other (or next to each other, or both) for easy comparison, but each with their own scales/axes.

If you have considered all that and still feel the need to put the data in the same graph, look at the updateusr function in the TeachingDemos package as a way to convert the scales/axis range from the original (date) version to that matching your new data.

For the labeling of the axis, you can use the axis command and tell it exactly which ticks and labels to use (you can have it do every other label, then do the skipped ones down 1 line with a second call), or you can rotate the labels (often ugly, be careful) or other things.  There are also some tools in the plotrix package that help with long labels (and probably other packages as well).

Hope this helps, 

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of myshare
> Sent: Wednesday, May 13, 2009 5:59 PM
> To: r-help at r-project.org
> Subject: [R] plotting multiple data sources
> 
> hi,
> 
> Excuse me asking three questions in a row for a day, but I had
> collected those questions
> as I'm still experimenting with R.
> This one is how do you compose plots with alot of data in one graph.
> First what I currently do is after i generated all the data I need to
> plot.. let say 5-6 arrays
> I do a plot() and then I created a function to scale the other data to
> approx fit the initial range
> so it can fit plotted data and then draw it with lines().
> 
> Here is the example function I use.
> 
> Blah.scale_to_range = function(self,data,range_low=0,range_high,
> skip_cond=(data > 0) ) {
>   ratio <- (range_high - range_low) / (max(data) - min(data))
>   ((data - min(data)) * ratio) + (range_high - range_low)/2
> }
> 
> so far so good.. the problem I have is that I want the x axis to be
> Date range.
> I can plot( x$date, x$values, type='l'), but then later if I want to
> plot the other data with lines()
> I can't plot them anymore, cause it seems that the lines() wants the
> other data X-axis values to
> be dates. I think I was able to draw them referencing the x$date i.e.
> something like this :
> 
> lines(x$date,scaled_array1)
> 
> but isn't there some easier way, especially if it can autoscale the
> new data too.
> Then my next question is how to change the axis labels so it does show
> x-axis label only every
> n-th tick.
> AFAIR I was able to force it to show date every tick with axis() and
> even positioned it vertically,
> but all the dates are overlapped..
> By default dates are very sparsed, so that is why I want to be able to
> control when
> the x-labels-dates to show up.
> 
> 
> thanx
> 
> ______________________________________________
> 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