0

I need to make a histogram for my variable which is 'travel time'. And inside that, I need to plot the regression(correlation) data i.e. my observed data vs predicted. And I need to repeat it for different time of day and week(in simple words, make a matrix of such figure using par function). for now, I can draw histograms and arrange that in matrix form but I am facing a problem in inside plot (plotting x and y data together with y=x line, and arranging them within their consecutive histograms plot, in a matrix ). How can I do that, as in the figure below. Any help would be appreciated. Thanks!

enter image description here

r2evans
  • 141,215
  • 6
  • 77
  • 149
santosh
  • 19
  • 6
  • 1
    Please read about [how to ask questions](http://stackoverflow.com/help/how-to-ask), as well as producing [reproducible questions](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). This includes showing *sample* representative data and *attempted* code. Often it significantly helps to present a simplified problem, perhaps using standard datasets, instead of having to spend a lot of time explaining all of your variables' meaning and/or including unrelated variables. – r2evans Jan 29 '17 at 07:08
  • @r2evans : thanks for your suggestion. – santosh Jan 29 '17 at 16:22

1 Answers1

1

One way to do this is to loop over your data and on every iteration create a desired plot. Here is one not very polished example, but it shows the logic how plotting a small plot over larger plot can be done. You will have to tweak the code to get it work in the way you need, but it shouldn't be that difficult.

# create some sample dataset (your x values)
a <- c(rnorm(100,0,1))
b <- c(rnorm(100,2,1))
# create their "y" values counterparts
x <- a + 3
y <- b + 4
# bind the data into two dataframes (explanatory variables in one, explained in the other)
data1 <- cbind(a,b)
data2 <- cbind(x,y)

# set dimensions of the plot matrix
par(mfrow = c(2,1))
# for each of the explanatory - explained pair
for (i in 1:ncol(data2))
{
        # set positioning of the histogram
        par("plt" = c(0.1,0.95,0.15,0.9))
        # plot the histogram
        hist(data1[, i])
        # set positioning of the small plot
        par("plt" = c(0.7, 0.95, 0.7, 0.95))
        # plot the small plot over the histogram
        par(new = TRUE)
        plot(data1[, i], data2[, i])
        # add some line into the small plot
        lines(data1[, i], data1[, i])
}
ira
  • 2,542
  • 2
  • 22
  • 36
  • Sure. If it solves your problem, feel free to accept the answer as a solution :) – ira Jan 29 '17 at 16:32