ListPlot within Plot (example post)

I want to place a 2D plot of fit residuals in a corner of a plot of the data+fitted curve.

In Mathematica 6 and later there is a special function, Inset, for that purpose.

Here is a simple, realistic example. The residuals of a fit are the differences between the data and the fitted curve.

Data Initialization

In[1]:= dat = {{0, 1.28086}, {1, 1.56017}, {2, 1.91442}, {3, 2.09588}, {4, 2.41924}, 
               {5, 2.37783}, {6, 2.57325}, {7, 2.58195}, {8, 2.43806}, {9, 2.34434}, 
               {10, 2.12974}}; 

Fit to a Line

In[2]:= linfitmodel = b*x + c; 

In[3]:= linfitparams = FindFit[dat, linfitmodel, {b, c}, x]
Out[3]= {b->0.0916195,c->1.69788}

In[4]:= linfitresids = dat - Table[{0, linfitmodel /. linfitparams}, {x, 0, 10}]; 

In[5]:= Show[ListPlot[dat, PlotStyle -> Directive[Red, PointSize]], 
             Plot[Evaluate[linfitmodel /. linfitparams], {x, 0, 10}], 
             Graphics[Inset[
                  ListLinePlot[linfitresids, ImageSize -> 100, PlotLabel -> "Residuals"], 
                {8, 1.5}]], 
             AxesOrigin -> {0, 1}, PlotRange -> All]

The residuals curve shows structure - down at the ends and high in the middle.
This suggests that a higher-order term might be useful.

Fit to a Quadratic Curve

In[6]:= quadfitmodel = a*x^2 + b*x + c; 

In[7]:= quadfitparams = FindFit[dat, quadfitmodel, {a, b, c}, x]
Out[7]= {a->-0.0309372,b->0.400991,c->1.23382} 

In[8]:= quadfitresids = dat - Table[{0, quadfitmodel /. quadfitparams}, {x, 0, 10}]; 
In[9]:= Show[ListPlot[dat, PlotStyle -> Directive[Red, PointSize]], 
             Plot[Evaluate[quadfitmodel /. quadfitparams], {x, 0, 10}], 
             Graphics[Inset[
                ListLinePlot[quadfitresids, ImageSize -> 100, PlotLabel -> "Residuals"], 
                {8, 1.5}]], 
            AxesOrigin -> {0, 1}, PlotRange -> All]

The residuals are small and random, which suggests a decent fit.

In[1]:= ?Inset   
    Inset[obj] represents an object obj inset in a graphic. 
    Inset[obj, pos] specifies that the inset should be placed at 
            position pos in the graphic. 
    Inset[obj, pos, opos] aligns the inset so that position opos 
            in the object lies at position pos in the enclosing graphic. 
    Inset[obj, pos, opos, size] specifies the size of the inset in the 
            coordinate system of the enclosing graphic.  
    Inset[obj, pos, opos, size, dirs] specifies that the axes of the 
           inset should be oriented in directions dirs.