3

Preamble: I'm a absolute newbie to pgfplots, pgfplotstable, tikz

I scanned this post How do I draw a scatterplot with a function plot?

What I want to achieve is a multi-variable-scatterplot like this one.

How far I got:

\documentclass{article}

\usepackage{pgfplots, pgfplotstable}

\begin{document}

\begin{tikzpicture}
\pgfplotstableread{ % Read the data into a table macro
A    B    C    D    E    F    G    H    I
1.00 4.50 1.00 3.00 3.67 6.00 2.00 2.00 6.00
2.67 3.00 n.b. 3.00 2.67 6.00 5.00 2.50 5.00
3.67 4.50 1.00 3.00 3.00 5.00 n.b. 4.00 4.00
4.00 6.00 1.00 3.50 2.67 1.00 2.00 3.50 3.50
}\datatable

\begin{axis}[
    title=Correlation A - B,
    xlabel={A},
    ylabel={B},
]
    \addplot table [x=A, y=B, only marks] {\datatable};
\end{axis}
\begin{axis}[
    title=Correlation A - E,
    xlabel={A},
    ylabel={E},
]
    \addplot table [x=A, y=E, only marks] {\datatable};
\end{axis}
\end{tikzpicture}

\end{document}

But the two tables are laid on top of each other.

I also have some more questions:

  1. do I need to import pgfplots AND pgfplotstable? WHY?
  2. i should use something like \pgfplotsset{compat=1.15}, shouldn't I? version 1.3?
  3. is any other option for \addplot useful (scatter, scatter src=\thisrow{class}, ...)?
  4. how to handle my empty values "n.b."? (How to let these be ignored)
  5. in order to safe some space, I want my scala go from 1 to 6 and have NO gap between the x-scala and the y-axis (analogously the other way round)
2

Questions :

  1. do I need to import pgfplots AND pgfplotstable? WHY?

Well, these are two distinct packages.

There are a lot of things one can do with pgfplots without pgfplotstable (presumably also true the other way around..)

So it would make sense to not load both packages automatically.

  1. i should use something like \pgfplotsset{compat=1.15}, shouldn't I? version 1.3?

Unless something is wrong, just use \pgfplotsset{compat=1.15} : latest version.

  1. is any other option for \addplot useful (scatter, scatter src=\thisrow{class}, ...)?

I think these graphs look ok.

  1. how to handle my empty values "n.b."? (How to let these be ignored)

** original answer : ** Just using the data without changing anything seems to do the trick. (no it raises errors, that can be ignored)

** edit : ** The correct way to deal with those is using nan for non-available values.

  1. in order to safe some space, I want my scala go from 1 to 6 and have NO gap between the x-scala and the y-axis (analogously the other way round)

Sorry, don't understand this question.

See if setting xmax = 6 is what you want.

The output

This is using the groupplots library. It is also possible to do the same without it, as explained in the manual (search groupplots).

enter image description here

The code

\documentclass[12pt,tikz]{standalone}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=1.15}
\usepgfplotslibrary{groupplots}
\begin{document}
\begin{tikzpicture}
  \pgfplotstableread
  { % Read the data into a table macro
    A    B    C    D    E    F    G    H    I
  1.00 4.50 1.00 3.00 3.67 6.00 2.00 2.00 6.00
  2.67 3.00 nan  3.00 2.67 6.00 5.00 2.50 5.00
  3.67 4.50 1.00 3.00 3.00 5.00 nan  4.00 4.00
  4.00 6.00 1.00 3.50 2.67 1.00 2.00 3.50 3.50
  }\datatable

  \begin{groupplot}
    [
      group style=
      {
        group size = 1 by 2,
        xlabels at=edge bottom,
        vertical sep = 1.5cm,
      },
      xmin = 1, xmax = 6,
      xlabel = {A},
    ]
    \nextgroupplot
      [
        title=Correlation A - G,
        ylabel={G},
      ]
    \addplot table [x=A, y=G, only marks] {\datatable};
    \nextgroupplot
      [
        title=Correlation A - E,
        ylabel={E},
      ]
    \addplot table [x=A, y=E, only marks] {\datatable};
  \end{groupplot}
\end{tikzpicture}
\end{document}
  • When I compile your code, the line \pgfplotsset{compat=1.15} throws 13 errors like ! Package pgfkeys Error: Choice '1.15' unknown in choice key '/pgfplots/compat/anchors'.. Also the value n.b. still throws 1 error calling ! Package PGF Math Error: Could not parse input 'n.b.' as a floating point number, sorry. The unreadable part was near '.b.'.. (deleting the dots, changing to nb, makes no difference) – Ben Aug 12 '17 at 11:47
  • 1
    @Ben Yeah you're right. Sorry for the fake news, I was running in noninteractive mode and didn't spot the errors. Please see if it's better with nans. – marsupilam Aug 12 '17 at 12:13
  • Thanks, that helped! \pgfplotsset{compat=1.15} still throws 13 errors, version 1.14 also does. Version 1.13 works perfectly fine for me – Ben Aug 12 '17 at 16:56
  • @Ben I'm by no means an expert of TeX installs, but it sounds to me like you don't have those pgfplots versions installed. Maybe you want to check (I don't think that it's very important or urgent...) – marsupilam Aug 12 '17 at 17:07
0

Are you also newbie in R language? For statistical graphs often the easiest option is the R/LaTeX integration using knitr (a R library). If you have no idea about but you are willing to try it, install R, save this example with the .Rnw extension, open the file with the RStudio editor and click on the compile button.

\documentclass[12pt,twocolumn]{article}
\begin{document}
<<echo=F, dev="tikz", fig.cap="Example correlation", out.width='\\linewidth'>>=
x <-  read.table(textConnection(
"A    B    C    D    E    F    G    H    I
1.00 4.50 1.00 3.00 3.67 6.00 2.00 2.00 6.00
2.67 3.00 n.b. 3.00 2.67 6.00 5.00 2.50 5.00
3.67 4.50 1.00 3.00 3.00 5.00 n.b. 4.00 4.00
4.00 6.00 1.00 3.50 2.67 1.00 2.00 3.50 3.50
"),header=T,na.strings = "n.b.")
with(x,plot(A,H,
ylim=c(1,6),xlim=c(1,6),                     # limits (optional)
col="blue",pch=19,cex=1.5,cex.lab=1.5,cex.axis=1.5 # more options
))  
@
\end{document}

mwe

Another reason to follow this approach is that probably you want to do more that render a graph with these data, for instance, show the significance of this correlation in the text.

The usual is just copy & paste the p value obtained with SPSS, STATA, SAS or some other statistical program, but if the data changed in the latex table, the p value will be outdated, so you will change the data in the statistical program, make gain the correlation test, copy & paste again.

But with this approach, after the end of R chunk (line with '@' alone) you can insert:

The A-H correlation is not significant (p=\Sexpr{with(x,cor.test(A,H))$p.value})

and this will produce:

The A-H correlation is not significant (p=0.09641)

Then, no matter as the table in data in your .Rnw change, both the graph and your signification levels in text will be always both updated automatically. In a large project this can be a great help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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