[R] Plots with same x-axes

Marc Schwartz MSchwartz at MedAnalytics.com
Fri Jan 21 04:30:53 CET 2005


On Fri, 2005-01-21 at 01:48 +0100, Robin Gruna wrote:
> Hi,
> I want to plot two graphics on top of each other with layout(), a
> scatterplot and a barplot. The problems are the different x-axes
> ratios of the plots. How can I align the two x-axes?   Thank you very
> much,
> Robin


Robin,

Here is an example:

# Set the layout, smaller plot on top for the 
# barplot region
nf <- layout(c(2, 1), heights = c(1, 3))
layout.show(nf)

# Create the data
x <- rnorm(50)
y <- rnorm(50)

# Set the margins for the scatterplot so that they will match with the
# barplot settings
par(mar = c(3, 3, 0, 3))

# now do the scatterplot
plot(x, y)

# Get the hist data for x
xhist <- hist(x, plot = FALSE)

# Set the margins for the barplot to use more of the plot
# region
par(mar = c(0, 3, 1, 3))

# now plot that barplot on top
# Set the 'space' argument to 0 so that the bars are
# next to each other
barplot(xhist$counts, axes = FALSE, space = 0)

HTH,

Marc Schwartz




More information about the R-help mailing list