0

I've really appreciated the help I've gotten here so far, so I'm asking one more question. :)

I have made a histogram in Overleaf that I am quite happy with. But two things are missing - two lines like these:

enter image description here enter image description here

How do I add these? Thanks in advance!

My code (quite messy):

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{setspace}
\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{caption}
\usepackage{subcaption}
\pgfplotsset{width=7.5cm,compat=1.12}
\usepgfplotslibrary{fillbetween}
\usepackage{pgfplotstable}
\usepackage{subfig}
\pgfplotsset{compat=1.7}
\usepackage[section]{placeins}
\usepackage[labelfont=bf]{caption}
\usepackage{siunitx}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage[font=small, labelfont=bf]{caption}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.18}
\usepackage[margin=25mm]{geometry}
\tolerance=1
\emergencystretch=\maxdimen
\hyphenpenalty=10000
\hbadness=10000
\setstretch{1.3}
\renewcommand{\contentsname}{Table of Contents}
\usepackage[a4paper,width=150mm,top=25mm,bottom=25mm,bindingoffset=6mm]{geometry}

\begin{document} \begin{figure} \centering \caption{Negative Campaigning in the Swedish Multi-Party System 1970-2022.} \begin{tikzpicture} \begin{axis}[x tick label style={/pgf/number format/.cd,% scaled x ticks = false, set thousands separator={}, fixed}, yticklabel={$\pgfmathprintnumber{\tick}%$}, grid=both, minor tick num=9, grid style={line width=.2pt, draw=black!10}, major grid style={line width=.4pt, draw=black!20}, enlargelimits={abs=0.1}, width=12cm, height=8cm, xmin=1970, xmax=2022, ymin=0, ymax=16, xlabel= \textbf{Time}, ylabel= \textbf{Degree of Negative Campaigning}, xtick={1970,1973,1976,1979,1982,1985,1988,1991,1994,1998,2002,2006,2010,2014,2018,2022}, every tick label/.append style={font=\tiny}] \addplot+[black, ybar interval, mark=no,fill = lightgray] plot coordinates { (1970,13.6363658) (1973,3.4666667) (1976,9.4786733) (1979,7.7683621) (1982,14.6520151) (1985,11.3144759) (1988,1.8472911) (1991,4.14201161) (1994,5.5229145) (1998,3.23846942) (2002,4.75448152) (2006,3.7750991) (2010,3.43) (2014,5.74) (2018,7.05) (2022,6.5)}; \end{axis} \end{tikzpicture} \end{figure} \end{document}

  • These might help: https://tex.stackexchange.com/questions/397462/pgfplot-add-the-mean-to-a-histogram-bar-plot , https://tex.stackexchange.com/questions/11368/bell-curve-gaussian-function-normal-distribution-in-tikz-pgf – ihsingh2 Nov 26 '22 at 13:09
  • Please accept the answers you have already received. – hpekristiansen Nov 28 '22 at 02:17
  • Your data is quite different. What is the position/formula for the desired lines? – hpekristiansen Nov 28 '22 at 03:11
  • The data ranges between 1.85 and 14.64, with a mean of 6.36 and a median of 5.64. The standard deviation is 3.64. To answer your question "What is the position/formula for the desired lines?" - I'm very new to this stuff, so I don't know the formula, what I do know though is that I want to add (1) a vertical line that shows the median and (2) a line that shows the distribution of the data (see second picture). I have no idea how to solve this, I checked the link ihsingh2 recommended, but couldn't solve it... –  Nov 28 '22 at 12:04

1 Answers1

1

Your question and comment contains insufficient and contradictory information, so you need to adjust this answer to your data.

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}[
declare function={
 mean=1987;
 variance=10;
 gauss(\x)=1/(variance*sqrt(2*pi))*exp(-((\x-mean)^2)/(2*variance^2));
}]
\begin{axis}[
x tick label style={/pgf/number format/.cd, scaled x ticks=false, set thousands separator={}, fixed},
yticklabel={$\pgfmathprintnumber{\tick}\%$},
grid=both,
minor tick num=9,
grid style={line width=.2pt, draw=black!10},
major grid style={line width=.4pt, draw=black!20},
enlargelimits={abs=0.1},
width=12cm,
height=8cm,
xmin=1970, xmax=2022,
ymin=0, ymax=16,
xlabel=\textbf{Time},
ylabel=\textbf{Degree of Negative Campaigning},
xtick={1970,1973,1976,1979,1982,1985,1988,1991,1994,1998,2002,2006,2010,2014,2018,2022},
every tick label/.append style={font=\tiny}
]
\addplot+[black, ybar interval, mark=none, fill=lightgray] plot coordinates {
(1970,13.6363658) 
(1973,3.4666667)
(1976,9.4786733)
(1979,7.7683621)
(1982,14.6520151)
(1985,11.3144759)
(1988,1.8472911)
(1991,4.14201161)
(1994,5.5229145)
(1998,3.23846942)
(2002,4.75448152)
(2006,3.7750991)
(2010,3.43)
(2014,5.74) 
(2018,7.05) 
(2022,6.5)};
\draw[red, ultra thick] (mean,0 |-current axis.above origin) --node[right]{Mean} (mean,0 |-current axis.below origin);
\addplot[red, ultra thick, domain=1970:2022, samples=100] (x,{100*gauss(x)});
\end{axis}
\end{tikzpicture}
\end{document}

Bar graph with line and bell curve