2

I'm trying to make a composite plot that has a histogram at the top and a dotplot on the bottom. They are supposed to display data on the same scale, so I want the scales to match.

Here is some code that almost does what I want:

ability <- rnorm(50)
parameters <- data.frame(Item=paste("Q",1:10),Difficulty=rnorm(10))
xrange<- range(c(ability,parameters$Difficulty))

print(histogram(ability,endpoints=xrange,xlim=xrange*1.04),
      position=c(0,.68,1,1),more=TRUE,newpage=TRUE)
print(dotplot(Item~Difficulty,parameters,xlim=xrange*1.04),
      position=c(0,0,1,.7))

Here is the output of this functionhistogram+dotplot

The problem is the tick marks in the two functions don't quite line up. The two plots allocate different amounts of space for ticks on the left, and the histogram has ticks on the right, while the dotplot doesn't. I can't quite figure out how to change that (I suspect it is buried somewhere in the scales argument).

Also, using print and position is a bit kludgy, is there a better more general way to do this.

Thanks in advance.

ralmond
  • 116
  • 5
  • If you're willing to use `ggplot` [here are a few examples](http://stackoverflow.com/questions/8545035/scatterplot-with-marginal-histograms-in-ggplot2). – chappers Nov 03 '15 at 05:39

1 Answers1

0

I figured out how to do it in the base R graphics:

layout(matrix(c(1,2),2,1,byrow=TRUE),heights=c(1,3))
par(mar = c(3.1, 4.1, 3.1, 2.1))
hist(ability,xlim=xrange*1.2,xlab="",col="cyan")
par(mar = c(3.1, 4.1, 0.1, 2.1))
dotchart(parameters$Difficulty,parameters$Item,xlim=xrange*1.2,main="")

This produced what I needed.

ralmond
  • 116
  • 5