How to add a legend without use pgfplots?
Welcome! Adding a legend is rather easy. All you need is a matrix
and a pic
,
\path (current bounding box.north east)
node[matrix,anchor=north east,draw,nodes={anchor=center},inner sep=2pt] {
\pic{sample=black}; & \node{$BR_A$}; \\
\pic{sample={Dotted,ultra thick,color=darkpastelgreen}}; & \node{$BR_B$}; \\
};
Here, the pic
sample was defined to feature the style of the plot you use in your picture. One could make it more automatic but this would merely mean switching to pgfplots
, which you do not seem to want. One may improve your code in many ways, but this requires more input from your side. A rather obvious way of improving it is to use the nice dotted line from here.
Anyway, here is a somewhat more ergonomic code that is closer to your target picture. It avoids duplication of coordinates (to some extent) and uses -|
and similar tricks.
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}
\definecolor{darkpastelgreen}{RGB}{105,186,72}
\begin{document}
\begin{figure}[h!]
\centering
\begin{tikzpicture}[>=stealth,scale=pi/2,
pics/sample/.style={code={\draw[#1] (0,0) --(0.6,0) ;}},
Dotted/.style={% https://tex.stackexchange.com/a/52856/194703
dash pattern=on 0.1\pgflinewidth off #1\pgflinewidth,line cap=round,
shorten >=#1\pgflinewidth/2,shorten <=#1\pgflinewidth/2},
Dotted/.default=3]
\draw [<->] (0,4) node[left] {$S_{A}$} -- (0,0) node[below left] {$0$}
-- (4,0) node [below] {$S_{B}$};
\draw [-] (0,3)node[left] {$100$} -- (3,0) node[below] {$100$};
\draw [Dotted,ultra thick, color=darkpastelgreen] (0,3)-- (3,0);
\draw [dashed, color=lightgray] (0,3) -| (3,0);
\draw[decoration={brace,raise=5pt},decorate,thick]
(0,2.9) -- node[above right=2ex,xshift=-2ex,align=left] {Agreement\\ Zone} (2.9,0);
\path (current bounding box.north east)
node[matrix,anchor=north east,draw,nodes={anchor=center},inner sep=2pt] {
\pic{sample=black}; & \node{$BR_A$}; \\
\pic{sample={Dotted,ultra thick,color=darkpastelgreen}}; & \node{$BR_B$}; \\
};
\end{tikzpicture}
\end{figure}
\end{document}
This is my suggestion. Instead of pgfplots
, I use TikZ only: putting pic
(name circ
is a small colored circle) along path
.
An other way is using decoration along path.
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}
\begin{document}
\begin{figure}[h!]
\centering
\begin{tikzpicture}[>=latex,
circ/.pic={\fill[green!50!black] (0,0) circle(1.5pt);}]
\draw (0,3)--(3,0);
\draw[<->] (0,4)--(0,0)--(5,0);
\draw[dashed,lightgray] (0,3)-|(3,0);
\draw[decoration={brace,raise=3pt},decorate]
(0,3)--(3,0);
\foreach \i in {0,.05,...,1.05}
\path (0,3)--(3,0) pic[pos=\i]{circ};
\path
(0,3) node[left]{$100$}
(3,0) node[below]{$100$}
(0,0) node[below left]{0}
(5,0) node[below]{$S_{B}$}
(0,4) node[left]{$S_{A}$}
(2.2,2.2) node[align=left,scale=.8] {Agreement\\Zone};
% for legend
\begin{scope}[local bounding box=L,shift={(4.5,3.5)}]
\path (0,0) node (A) {$BR_A$}
(-90:.4) node (B) {$BR_B$};
\draw (A.west)--+(180:.6);
\foreach \i in {0,.3,...,1}
\path (B.west)--+(180:.6) pic[pos=\i]{circ};
\end{scope}
\draw ([xshift=-2mm]L.south west) rectangle (L.north east);
\end{tikzpicture}
\end{figure}
\end{document}