1

I am attempting to visualize responses of a questionnaire study in which respondents have answered a group of semantic differentials. As such I want to label both ends of each response category, which mean labelling both the left and right hand side of the y-axis. What I currently have is this:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz,pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
legend cell align=left,
legend style={at={(0.5,1.1)},anchor=north},
legend style={draw=none},
legend columns=-1,
%enlarge x limits=0.,
xbar,
bar width=6pt,
%x coord trafo/.code={\pgfmathparse{-x}},
xmin=0,
xmax=7,
ytick={1,2,3,4,5,6,7},
font=\footnotesize,
%xticklabels={1,2,3,4,5,6,7},
yticklabels={%
    Incompetent,
    Ignorant,
    Unpredictable,
    Irresponsible,
    Unintelligent,
    Compliant,
    Foolish
}
]

\addplot[nodes near coords align={left},
draw=black,fill=black]
table{
x   y   label
2   1   1
3   2   1
2   3   1
5   4   1
5   5   1   
4   6   1
7   7   1
};

\addplot[nodes near coords align={left},
draw=gray,fill=gray]
table{
x   y   label 
1   1   1
5   2   1
1   3   1
2   4   1
3   5   1   
1   6   1
2   7   1
};

\legend{C1, C2}
\end{axis}
\end{tikzpicture}


\end{document}

Which produces this: enter image description here

How do I add labels to the right hand side of the graph? (if it at all is possible)

  • 1
    Have you had a look in the manual? Chapter 4.9.11 Two Ordinates (y-axis) or Multiple Axes sounds promising. You basically draw two axes systems on top of each other. – Dr. Manuel Kuehner May 19 '18 at 11:53
3

Straight from the manual.

% Preamble: \pgfplotsset{width=7cm,compat=1.16}
\begin{tikzpicture}
% let both axes use the same layers
\pgfplotsset{set layers}
\begin{axis}[
scale only axis,
xmin=-5,xmax=5,
axis y line*=left, % the ’*’ avoids arrow heads
xlabel=$x$,
ylabel=First ordinate,
]
\addplot {x^2};
\end{axis}
\begin{axis}[
scale only axis,
xmin=-5,xmax=5,
axis y line*=right,
axis x line=none,
ylabel=Second ordinate,
]
\addplot [red] {3*x};
\end{axis}
\end{tikzpicture}

The scale only axis bit is important!

enter image description here

| improve this answer | |
  • Thanks, just what I was looking for. In fact I did have a look at the manual, but my search was probably too narrow. Thanks again! – Lars Christian Jensen May 19 '18 at 14:41

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.