Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

The graph below is currently plots a point at each value of x that is an integer between -10 and 10 and connects them.

What I'd like to do instead is more like this:

enter image description here

First, I'd like to add a red, light line at x = 0.

Second, I'd like to unconnect the points and have them be presented as just a scatter plot.

Third, I'd like to add a known trend line for the 10 points that are from x < 0 and another known trend line for the points that are from x > 0.

Specifically, suppose the trend line for x < 0 is y = -0.002x + 0.003 and the trend line for x > 0 is y = 0.002x + 0.003
(Those are not the actual values.)

Here is the MWE:

\documentclass{beamer}
 \usepackage{graphicx}
 \usepackage{filecontents}
\usepackage{pgfplots}
\usepackage{tikz}
 \usetikzlibrary{arrows,shapes,positioning,fit,shapes.misc,matrix,decorations.text,shapes.geometric}

 \begin{document}

 \begin{filecontents}{drc1.dat}
 -10 0.0635084
 -9  0.037563
 -8  0.0460021
 -7  -0.0020816
 -6  0.0224089
 -5  0.0303281
 -4  0.0101534
 -3  0.0214043
 -2  0.0278317
 -1  -0.0336859
 1   0.0866865
 2   0.0599577
 3   -0.0087226
 4   -0.0334984
5   -0.0582118
6   -0.0628758
7   -0.0703382
8   -0.0815326
9   -0.0941923
10  -0.055196

\end{filecontents}

\frame
 {
 \frametitle{Frame Title}
\centering
\begin{tikzpicture}
  \begin{axis}
    [
      axis x line = bottom,
      axis y line = left,
      width      = 1.0\textwidth,
      height     = 0.60\textwidth,
      title      = Picture Title,
      xmax       =  10.2,
      xmin       = -10.2,
      xshift     = -6cm,
      ymax       =  1.05,
      ymin       = -1.05,
      xtick      = {-10, -5, 0, 5, 10},
      xticklabels= {-10, -5, 0, 5, 10},
      ytick      = {-1, -0.5, 0, 0.5, 1},
      yticklabels= {-1, -0.5, 0, 0.5, 1}
    ]
    \addplot file {drc1.dat};
   \end{axis}
 \end{tikzpicture}
 }

\end{document}
share|improve this question
1  
By "known trend line", do you mean that you've calculated the parameters for the trend line externally? Could you provide the parameters? –  Jake Nov 6 '12 at 14:55
    
@Jake Good question. I've listed equations for the trend lines above. –  J G Nov 6 '12 at 15:16
    
For adding the vertical red line, you might want to take a look at the answers to How can I add a zero line to a plot? –  Jake Nov 6 '12 at 16:15

3 Answers 3

up vote 3 down vote accepted

You can add lines manually either by drawing lines via the pgf command between specific coordinates of the axis coordinate system or by specifying an equation for the particular trend line.

Here is a minimal working example (MWE) illustrating these lines:

\documentclass{beamer}

\usepackage{pgfplots}

\begin{filecontents}{drc1.dat}
-10, 0.0635084
-9,  0.037563
-8,  0.0460021
-7,  -0.0020816
-6,  0.0224089
-5 , 0.0303281
-4,  0.0101534
-3,  0.0214043
-2 , 0.0278317
-1,  -0.0336859
1 ,  0.0866865
2 ,  0.0599577
3 ,  -0.0087226
4 ,  -0.0334984
5  , -0.0582118
6 ,  -0.0628758
7  , -0.0703382
8 ,  -0.0815326
9 ,  -0.0941923
10,  -0.055196

\end{filecontents}
 \begin{document}

\frame
 {
\frametitle{Frame Title}
\centering
 \begin{tikzpicture}
  \begin{axis}
    [
      axis x line = bottom,
      axis y line = left,
      width      = 1.0\textwidth,
      height     = 0.60\textwidth,
      title      = Picture Title,
      xmax       =  10.2,
      xmin       = -10.2,
      xshift     = -6cm,
      ymax       =  1.05,
      ymin       = -1.05,
      xtick      = {-10, -5, 0, 5, 10},
      xticklabels= {-10, -5, 0, 5, 10},
      ytick      = {-1, -0.5, 0, 0.5, 1},
      yticklabels= {-1, -0.5, 0, 0.5, 1}
    ]
    % draw scatter plot
    \addplot[only marks] table[x index = 0, y index= 1] {drc1.dat};
    % draw thin red line at x=0
    \draw[thin, red] (axis cs:0,-1) -- (axis cs:0,1);
    % add trend lines according to predefined parameters
    \addplot[thick, draw=green, mark=none,domain={-10:0}] {-0.01*x+0.05};
    \addplot[thick, draw=blue, mark=none,domain={0:10}] {-0.02*x+0.3};
    \end{axis}
  \end{tikzpicture}
  }

\end{document}

Please also consult the pgfplots manual for further options.

share|improve this answer

With pgfplotstable you can do it.

\documentclass{article}
\usepackage{graphicx}
\usepackage{filecontents}

\usepackage{pgfplotstable}
\pgfplotsset{compat=1.7}

\usepackage{tikz}
\usetikzlibrary{arrows,shapes,positioning,fit,shapes.misc,matrix,decorations.text,shapes.geometric}

\begin{filecontents*}{drc1.dat}
A B
-10 0.0635084
-9  0.037563
-8  0.0460021
-7  -0.0020816
-6  0.0224089
-5  0.0303281
-4  0.0101534
-3  0.0214043
-2  0.0278317
-1  -0.0336859
1   0.0866865
2   0.0599577
3   -0.0087226
4   -0.0334984
5   -0.0582118
6   -0.0628758
7   -0.0703382
8   -0.0815326
9   -0.0941923
10  -0.055196
\end{filecontents*}


\begin{document}

\begin{tikzpicture}
  \begin{axis}
    [
      axis x line = bottom,
      axis y line = left,
      width      = 1.0\textwidth,
      height     = 0.60\textwidth,
      title      = Picture Title,
      xmax       =  10.2,
      xmin       = -10.2,
      xshift     = -6cm,
      ymax       =  1.05,
      ymin       = -1.05,
      xtick      = {-10, -5, 0, 5, 10},
      xticklabels= {-10, -5, 0, 5, 10},
      ytick      = {-1, -0.5, 0, 0.5, 1},
      yticklabels= {-1, -0.5, 0, 0.5, 1}
    ]
    \addplot table {drc1.dat};
    \addplot table [x=A,
                    y={create col/linear regression={y=B}}] {drc1.dat};
   \end{axis}
 \end{tikzpicture}

\end{document}

Notice that I added titles to the columns, that make things easier.

enter image description here

share|improve this answer
    
Thank you so much for this answer. It does not quite do what I'd like, however. First, I don't want the points connected. Second, I'm not sure why there are both red points and blue points since there should just be one value for each value of x. Third, there should be separate trend lines for x < 0 from x > 0. As I wrote in the original post, I do not need Beamer to estimate the parameters of the best; I can feed those in. –  J G Nov 6 '12 at 15:30
    
You can use all the \addplot commands you want and also customize the appearance of plots. Just look at the pgfplots manual. –  egreg Nov 6 '12 at 15:35
    
that's a very useful package i had never heard of... –  joão gândara Nov 6 '12 at 16:05
    
Why do you reference the file and print the data too? –  Magpie Jan 28 '13 at 23:35

Without the data, i can only guide through the necessary steps to create a graph like the one you want. If you provide the data, i can complete my answer.

To get the "scatter plot" on the left part of the graph, you should use something:

\addplot[only marks,mark=x,restrict x to domain=-10:0]%
         table [col sep = comma, y index = 1] {data.dat}

For the right side part of the graph use

\addplot[only marks,mark=*,restrict x to domain=0:10]%
         table [col sep = comma, y index = 1] {data.dat}

Using "restrict x to domain" you are able to only plot a part of the data. Note that i've guessed the option on the table. You should adjust them according to your needs.

To get the line, you can declare a general linear function that takes two parameters

\pgfmathdeclarefunction{lineregression}{2}{\pgfmathparse{#1*x + #2}}

To plot the line on the left side you can do

\addplot[domain=-10:0]{lineregression(-0.002,0.003)}

and to plot the line on the right you can do

\addplot[domain=0:10]{lineregression(0.002,0.003)}

The dashed line at x = 0 can be done with

\addplot [dashed, no markers, red, thin] coordinates%
         {(0,\pgfkeysvalueof{/pgfplots/ymin}), (0,pgfkeysvalueof{/pgfplots/ymax})}

The right side of the plot can be obtained in a similar way.

I suppose this answer only guides you in the necessary steps. I can provide a complete answer if you provide the data you want to plot.

share|improve this answer
    
Thank you for your answer! The edited post should contain the data and the parameters for the trend lines. –  J G Nov 6 '12 at 15:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.