Does anyone know how to do a scatterplot inside a square pie chart? I know I'm supposed to provide a MWE but but I'm very clueless of where to begin. What I'd need is 2 square pie charts within the same figure, and each pie filled with different scatter symbols. Basically I want to show how the distributions of 2 categories line up in one group - hence the combination of pie charts and scatter plots in one. I haven't been able to locate any example, but I'm hoping some masters here would know how to do it! Thank you in advance for any help you can give.
Asked
Active
Viewed 216 times
3
1 Answers
2
This does something of that sort. It uses this answer to fill in symbols at random positions such they do not overlap. For instance, with
\path (-3,-3) pic{random symbols={xmax=3,ymax=6,symbols={{5*s},{2*o},{1*+}}}};
you instruct TikZ to put these symbols in a rectangle with lower left coordinate (-3,-3), width 3cm and height 6cm. The symbols are 5 stars (5*s), 2 circles (2*o) and one cross (1*+). These symbols are defined in separate pics. The underlying chart is done with pgf-pie.
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{shapes.geometric}
\usepackage{pgf-pie}
\newcounter{nsymbols}
\tikzset{pics/random symbols/.style={code={\setcounter{nsymbols}{1}
\tikzset{random symbols/.cd,#1}
\def\pv##1{\pgfkeysvalueof{/tikz/random symbols/##1}}%
\def\mysplit##1*##2;{\edef\myn{##1}\edef\myitem{##2}}
\edef\temp{\pv{symbols}}
\edef\tmplist{"x"}
\foreach \X in \temp
{\expandafter\mysplit\X;
\foreach \Y in {1,...,\myn}
{\stepcounter{nsymbols}
\xdef\tmplist{\tmplist,"\myitem"}}}
\edef\nmax{\number\value{nsymbols}}
\setcounter{nsymbols}{1}
\pgfmathsetmacro\xlist{\pv{r}+rnd*(\pv{xmax}-2*\pv{r})}
\pgfmathsetmacro\ylist{\pv{r}+rnd*(\pv{ymax}-2*\pv{r})}
\pgfmathsetmacro{\myitem}{{\tmplist}[1]}
\path (\xlist,\ylist)pic{\myitem};
\foreach \XX in {1,...,\pv{n}}
{\pgfmathsetmacro\x{\pv{r}+rnd*(\pv{xmax}-2*\pv{r})}
\pgfmathsetmacro\y{\pv{r}+rnd*(\pv{ymax}-2*\pv{r})}
\xdef\collision{0}
% check if the mark is too close to the percentage
\pgfmathsetmacro\checkdistance{4*sqrt(pow(\x-\pv{xmax}/2,2)+pow(\y-\pv{ymax}/2,2))}
\ifdim\checkdistance pt<\pv{R} pt
\xdef\collision{1}
\fi
\foreach \element [count=\YY starting from 0] in \xlist{
\pgfmathsetmacro\checkdistance{sqrt(({\xlist}[\YY]-(\x))^2+({\ylist}[\YY]-(\y))^2)}
\ifdim\checkdistance pt<\pv{R} pt
\xdef\collision{1}
\breakforeach
\fi
}
\ifnum\collision=0
\xdef\xlist{\xlist,\x}
\xdef\ylist{\ylist,\y}
\stepcounter{nsymbols}
\ifnum\value{nsymbols}<\nmax
\pgfmathsetmacro{\myitem}{{\tmplist}[\value{nsymbols}]}
\path (\x,\y) pic{\myitem};
\fi
\fi
\unless\ifnum\value{nsymbols}<\nmax
\breakforeach
\fi
}
}
},
random symbols/.cd,n/.initial=50,% tries
xmax/.initial=3,% width
ymax/.initial=3,% height
r/.initial=0.15,%radius
R/.initial=0.4,% grace distance, should be greater than 2*r
symbols/.initial={{2*s}},
/tikz/.cd,
pics/s/.style={code={\node[star,draw,fill=orange,minimum size=3mm,inner sep=0pt]{};}},
pics/+/.style={code={\draw (-1.5mm,0mm) -- (1.5mm,0mm) (0,-1.5mm) -- (0,1.5mm);}},
pics/o/.style={code={\draw (0,0) circle[radius=1.5mm];}},
}
\begin{document}
\begin{tikzpicture}[font=\sffamily]
\pie[square,
color={red,blue,yellow},
text=inside,
text=legend,
]{50/A,30/B,20/C}
\pgfmathsetseed{42}
\path (-3,-3) pic{random symbols={xmax=3,ymax=6,symbols={{5*s},{2*o},{1*+}}}};
\path (0,-3) pic{random symbols={xmax=3,ymax=3.6,symbols={{1*s},{1*o}}}};
\path (0,0.6) pic{random symbols={xmax=3,ymax=2.4,symbols={{1*o},{5*+}}}};
\end{tikzpicture}
\end{document}
-
Wow thank you so much! Not just for writing the code but also for explaining to me how it works. – Lilly Nguyen Feb 18 '20 at 16:06
-
2


yellowperson data point belongs in the top left corner? – Alexander Feb 18 '20 at 13:43\documentclass[tikz,border=3mm]{standalone} \usepackage{pgf-pie} \begin{document} \begin{tikzpicture}[xscale = 1.75, font=\sffamily] \pie[square, color={red,blue,yellow}, text=inside, text=legend, ]{50/A,30/B,20/C} \end{tikzpicture} \end{document}. I am unfortunately a bit lost with regards to how the markers are to be placed. – Feb 18 '20 at 14:17