I would like to create a family of barplots with the same xlimits. Is there a way to "read" the xlimits from the first graph so I can apply it to the subsequent ones? I have tried just taking the min and max of the x data and the plot doesn't show. cheers [[alternative HTML version deleted]]
I'd recommend you read the code for barplot (it's all in R; just type barplot.default at the prompt) then emulate the xlim calculation prior to starting your series of plots, calling each plot with the same xlim. Reading the base package coding is always VERY instructive. Takes time, but it's worth it.> -----Original Message----- > From: Paul Sorenson [mailto:Paul.Sorenson at vision-bio.com] > Sent: 13 November 2003 05:54 > To: r-help at stat.math.ethz.ch > Subject: [R] xlims of barplot> I would like to create a family of barplots with the same xlimits. Is > there a way to "read" the xlimits from the first graph so I > can apply it to > the subsequent ones?Simon Fear Senior Statistician Syne qua non Ltd Tel: +44 (0) 1379 644449 Fax: +44 (0) 1379 644445 email: Simon.Fear at synequanon.com web: http://www.synequanon.com Number of attachments included with this message: 0 This message (and any associated files) is confidential and\...{{dropped}}
On Wed, 2003-11-12 at 23:54, Paul Sorenson wrote:> I would like to create a family of barplots with the same xlimits. Is > there a way to "read" the xlimits from the first graph so I can apply it to > the subsequent ones? > > I have tried just taking the min and max of the x data and the plot doesn't > show. > > cheersA point of clarification: When you say x axis limits, are you referring to the bar heights in a horizontal barplot or are you referring to the range of the x axis with vertical bars? A critical difference. In the former situation, you can explicitly set the x axis limits using the 'xlim' argument to include the range of values in your various sets of data. Thus, you can use: # Get the maximum x value in the datasets # Presumes that 'MyData' contains all values max.x <- max(MyData) # Now use this format for EACH barplot barplot(...., horiz = TRUE, xlim = c(0, max.x)) That will result in the x axis being the same in each plot. You might want to use something like 'xlim = c(0, max.x * 1.25)', which will give you some additional space above the bars (ie. for a legend, etc.). Note that if you use the range of x values (instead of 0 and max) and set xlim to that min/max pair, you will get a "funny" result. For example: barplot(1:5, horiz = TRUE, xlim = c(1, 5)) Since par("xpd") is set to TRUE by default in barplot(), the bases of the bars will actually appear beyond the left side of the plot region. You can eliminate that effect by using: barplot(1:5, horiz = TRUE, xlim = c(1, 5), xpd = FALSE) However, you will not see a bar for your minimum value. If you are talking about a vertical barplot, then the x axis range will be the same for each barplot under the following conditions, without having to set it: 1. You have the same number of bars in each plot 2. You do not change the values of 'space', 'width' or 'beside' across the plots. Also, keep in mind that the bars are NOT centered over integer values on the respective axis. You can get the bar center positions by using: mp <- barplot(...) where 'mp' will contain the bar midpoints, which is useful for subsequent annotation, etc. See ?barplot for more help and examples. HTH, Marc Schwartz
Thanks - that is a very valuable tip. -----Original Message----- From: Simon Fear [mailto:Simon.Fear@synequanon.com] Sent: Thursday, 13 November 2003 8:53 PM To: Paul Sorenson; r-help@stat.math.ethz.ch Subject: RE: [R] xlims of barplot I'd recommend you read the code for barplot (it's all in R; just type barplot.default at the prompt) then emulate the xlim calculation prior to starting your series of plots, calling each plot with the same xlim. Reading the base package coding is always VERY instructive. Takes time, but it's worth it. [[alternative HTML version deleted]]
-----Original Message----- From: Marc Schwartz [mailto:MSchwartz@MedAnalytics.com] Sent: Friday, 14 November 2003 12:49 AM To: Paul Sorenson Cc: r-help@stat.math.ethz.ch Subject: Re: [R] xlims of barplot On Wed, 2003-11-12 at 23:54, Paul Sorenson wrote:>> I would like to create a family of barplots with the same xlimits. Is >> there a way to "read" the xlimits from the first graph so I can apply it to >> the subsequent ones? >> >> I have tried just taking the min and max of the x data and the plot doesn't >> show. >> >> cheers > >A point of clarification: > >When you say x axis limits, are you referring to the bar heights in a >horizontal barplot or are you referring to the range of the x axis with >vertical bars? A critical difference....>If you are talking about a vertical barplot, then the x axis range will >be the same for each barplot under the following conditions, without >having to set it: > >1. You have the same number of bars in each plot >2. You do not change the values of 'space', 'width' or 'beside' across >the plots. > >Also, keep in mind that the bars are NOT centered over integer values on >the respective axis. You can get the bar center positions by using:Sorry for being vague, it is the latter case, vertical bars. The data doesn't satisfy condition 1. The family of 6 plots is datestamped data, the first plot showing all defects, then each subsequent plot showing defects of each severity level we define. The min and max of each of the subsequent datasets is in general a subset of the full dataset. I can easily plot them but it would be nice to keep the same x limits on each graph. The x data is POSIXct although I suspect that is not relevant. [[alternative HTML version deleted]]
-----Original Message----- From: Marc Schwartz [mailto:MSchwartz@MedAnalytics.com] Sent: Friday, 14 November 2003 9:44 AM To: Paul Sorenson Cc: r-help@stat.math.ethz.ch Subject: RE: [R] xlims of barplot>On Thu, 2003-11-13 at 15:53, Paul Sorenson wrote: > >SNIP > >> Sorry for being vague, it is the latter case, vertical bars. The data >> doesn't satisfy condition 1. The family of 6 plots is datestamped data, >> the first plot showing all defects, then each subsequent plot showing >> defects of each severity level we define. The min and max of each of the >> subsequent datasets is in general a subset of the full dataset. I can >> easily plot them but it would be nice to keep the same x limits on each >> graph. The x data is POSIXct although I suspect that is not relevant. > > >OK...I think I understand what you are doing. > >You want a series of barplots that have "space" for the same number of >vertical bars along the x axis, but there may be gaps in the series for >any given barplot. Presumably, those gaps may be anywhere in the time >series along the x axis.Correct and most problematically at the ends.>Hint: barplot() will leave gaps in the bar series where an NA appears in >the vector or in a matrix column of height values. > >... > >So, the key is to be sure that the vector or matrix has the same number >of elements or matrix columns in each dataset. For your incomplete >datasets, pad each series with NA's to fill out the missing entries in >the time series.That sounds like a way forward. I just need to go back to the basics and learn how to add "rows" to data.frames. I am sure it won't be hard, its just my personal learning curve with several new data types (factors, tables, data.frames vs vectors, lists, arrays which I am more familiar with). For example, yesterday I tried max(myFactor) and it gave me an error (something like "must be a vector"), even though to my naive way of thinking myFactor clearly had a numeric max. Thanks [[alternative HTML version deleted]]