Jay Schyler Raadt
Jay Schyler Raadt

Reputation: 75

Fit a line to small multiples

I want to fit a line that goes through the mean of sampling distributions on a shared plot. This code creates a similar data set to the one I am using. It creates a sampling distribution and plots the distributions on the same graphs. Then, I draw a line going through the mean of the distributions. However, I want a line that fits all of the means of the distributions. I'm thinking of something like this graphic found here.

means<-c(NULL)

sample<-rnorm(1000,-0.2,0.1)
A<-hist(sample,plot=FALSE)
means<-c(means,mean(sample))
sample<-rnorm(1000,-0.1,0.1)

B<-hist(sample,plot=FALSE)
means<-c(means,mean(sample))

sample<-rnorm(1000,0,0.1)
C<-hist(sample,plot=FALSE)
means<-c(means,mean(sample))

sample<-rnorm(1000,0.1,0.1)
D<-hist(sample,plot=FALSE)
means<-c(means,mean(sample))

sample<-rnorm(1000,0.2,0.1)
E<-hist(sample,plot=FALSE)
means<-c(means,mean(sample))


plot(NULL,type="n",
     xlim=c(0,1250),
     ylim=c(min(A$breaks,B$breaks,C$breaks,D$breaks,E$breaks),
            max(A$breaks,B$breaks,C$breaks,D$breaks,E$breaks)),
     xaxt="n",
     xlab="Mean",
     ylab="Sampling Distribution of The Mean")
labels<-c("-0.2","-0.1","0","0.1","0.2")

y.coord<-0
rect(y.coord, A$breaks[1:(length(A$breaks) - 1)], A$counts, A$breaks[2:length(A$breaks)])
axis(side=1,at=y.coord,labels=labels[1],las=3)

y.coord<-max(A$counts)+50
rect(y.coord, B$breaks[1:(length(B$breaks) - 1)], y.coord+B$counts, B$breaks[2:length(B$breaks)])
axis(side=1,at=y.coord,labels=labels[2],las=3)

y.coord<-y.coord+max(B$counts)+50
rect(y.coord, C$breaks[1:(length(C$breaks) - 1)], y.coord+C$counts, C$breaks[2:length(C$breaks)])
axis(side=1,at=y.coord,labels=labels[3],las=3)

y.coord<-y.coord+max(C$counts)+50
rect(y.coord, D$breaks[1:(length(D$breaks) - 1)], y.coord+D$counts, D$breaks[2:length(D$breaks)])
axis(side=1,at=y.coord,labels=labels[4],las=3)

y.coord<-y.coord+max(D$counts)+50
rect(y.coord, E$breaks[1:(length(E$breaks) - 1)], y.coord+E$counts, E$breaks[2:length(E$breaks)])
axis(side=1,at=y.coord,labels=labels[5],las=3)

abline(a=means[1],b=0,col="red")
abline(a=means[2],b=0,col="red")
abline(a=means[3],b=0,col="red")
abline(a=means[4],b=0,col="red")
abline(a=means[5],b=0,col="red")

This question is a follow up to my question here. As you can see, I created a work around to graph all the small multiples. However, I'm still having trouble with the line of fit.

I appreciate your consideration of this problem.

Upvotes: 0

Views: 41

Answers (1)

shea
shea

Reputation: 528

I knew that lines() or abline() would add a line, but for some reason, all my dummy attempts kept screwing up. Then I looked at your code again and I see this time that the x axis is not really -0.2 - 0.2, it's 0 - 1250. That's why my attempts on my computer were making almost vertical lines.

So, with that in mind:

x_line_val <- seq(1, 1250, length.out=5)
abline(lm(means ~ x_line_val), col="blue")

You can use also use lines(x=x_line_val, y=means) instead of abline() and the line is more like a line segment.

To print the line equation in the graph, this is kind of a skeleton, but you can customize it:

figs1 <- summary(lm(means~x_line_val))$coef[c(1,2)] #get your intercept and beta1 values
eq1 <- paste0("y ~ ",round(figs1[1],5)," + x",round(figs1[2],5)) #write out equation
text(x=600, y=0.48, labels=eq1) #add equation to plot

Of course, this equation is for an x/independent value scaled a few magnitudes greater than what you are illustrating, so I think the equation is really incorrect. At least it doesn't actually represent what the graph shows. So you need to need to do a new regression where 'x_line_val' is replaced by a new version at the right scale.

Upvotes: 0

Related Questions