我编写了以下代码,其中包含大量表格、图片和图表。输出中这些内容都显得杂乱无章且扭曲。有人能帮我解决吗?一开始,我编写了一个计数符号代码(这是正确的)。
\documentclass{article}
\usepackage{listings,booktabs,graphicx}
\usepackage[T1]{fontenc}
\usepackage{blindtext}
\usepackage{titling}
\usepackage[top=1in, bottom=1in, left=1.5in, right=1in]{geometry}
\usepackage{listings}
\usepackage{booktabs}
\usepackage[svgnames]{xcolor}
\usepackage{listings}
\usepackage{verbatim}
\usepackage{graphicx}
\usepackage{booktabs, xltabular}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{xcolor}
\newcount\tmpnum
\def\tallymarks#1{\leavevmode \lower1bp\vbox to9bp{}%
\tmpnum=#1
\loop \ifnum\tmpnum<5 \kern1bp \tallynum\tmpnum \else \tallyV \fi
\advance\tmpnum by-5
\ifnum\tmpnum>0 \repeat
}
\def\tallynum#1{\bgroup\tmpnum=#1\relax
\loop \ifnum\tmpnum>0
\kern1bp \tallyI \kern1bp
\advance\tmpnum by-1
\repeat
\egroup
}
\def\tallyI{\pdfliteral{q .5 w 0 -1 m 0 8 l S Q}}
\def\tallyV{\kern1bp\pdfliteral{q .5 w -1 0 m 9 7 l S Q}\tallynum4\kern1bp }
\lstset{language=R,
basicstyle=\small\ttfamily,
stringstyle=\color{DarkGreen},
otherkeywords={0,1,2,3,4,5,6,7,8,9},
morekeywords={TRUE,FALSE},
deletekeywords={data,frame,length,as,character},
keywordstyle=\color{blue},
commentstyle=\color{DarkGreen}
}
\begin{document}
In datasets, we often find variables taking a wide range of values whether these are categorical or quantitive in nature. To make sense of the data we want to organize them in a way such that we can catch a glimpse of how the data is distributed within the dataset and if there exists a pattern in the distribution of the values. These can be undertaken as follows. We can count the number of occurrences of each value for a variable and then demonstrate it through a frequency table or we can make use of bins and each observation that falls within the range of a specific bin is then added up and afterward displayed in a frequency table. I elaborate on these with two examples.
\vspace{\baselineskip}
\newline \textbf{Example 1}
\vspace{\baselineskip}
\newline
Suppose we have a categorical variable in which the blood type of participants in a survey is compiled. The observed values are shown in the table below. We are interested in the frequency of each blood type in this data, that is we want to count the number of occurrences of each blood type.
\vspace{\baselineskip}
\newline
\begin{table}[]
\centering
\begin{tabular}{|lllll|}
\hline
O & A & A & O & O \\
B & B & A & A & A \\
A & O & O & O & B \\
O & O & A & O & A \\
A & O & AB & B & O \\
AB & B & O & A & AB \\ \hline
\end{tabular}
\end{table}
We summarize the data in a frequency table by first inserting a column with the labels of the observed data, then tally the numbers of each blood group---concluding with the frequency of each blood group. This procedure is based on the approach that the frequencies are counted by hand---this can of course get complicated when the data set concerns a large sample size in which case we can use the \texttt{count()} function in R. The frequency table can also be visualized in a barplot, like below with the following R code.
\begin{lstlisting}
bloodgroup <- c("O", "A", "A", "O", "O",
"B", "B", "A", "A", "A",
"A", "O", "O", "O", "B",
"O", "O", "A", "O", "A",
"A", "O", "AB", "B", "O",
"AB", "B", "O", "A", "AB")
xx <- barplot(table(bloodgroup),ylim=c(0, 14))
coords <- as.numeric(table(bloodgroup))
text(x = xx, y = coords, label = coords,
cex = 0.8,pos = 3, col = "red")
\end{lstlisting}
\begin{center}
\begin{tabular}{@{} *5l @{}} \toprule
\textbf{Blood Group} & \textbf{Tally} & \textbf{Frequency} \\\midrule
A & \tallymarks{10} & 10 \\
B & \tallymarks{5} & 5 \\
AB & \tallymarks{3} & 3 \\
O & \tallymarks{12} & 12 \\\bottomrule
\hline
\end{tabular}
\includegraphics[scale=0.75]{example-image}
\end{center}
\textbf{Example 2}
\vspace{\baselineskip}
\newline In this example, we use a continuous variable BMI that stores the body mass index of 30 participants. We are aiming to determine how much of these participants fall in the category of underweight, healthy weight, overweight, and obese. The range of all these bins are construed as follows: below 18.5 is underweight, between 18.5 to 25 healthy weight, from 25 to 30 overweight and over 30 obese. The observed data is given below.
\begin{table}[]
\begin{tabular}{|llllll|}
\hline
18.2 & 31.2 & 24.3 & 21.5 & 23.8 & 31.8 \\
22.1 & 15.2 & 27.8 & 17.8 & 19.2 & 23.3 \\
26.1 & 17.3 & 18.6 & 19.4 & 16.2 & 19.6 \\
16.8 & 19.7 & 17.3 & 20.8 & 24.4 & 28.4 \\
23.8 & 20.1 & 25.9 & 33.2 & 26.7 & 24.7 \\ \hline
\end{tabular}
\end{table}
To create a frequency table for this data, we follow the steps as laid for the previous example---except that we use bins with a range as labeled above. For example for the observation 22.1 we lookup in the given ranges to find in which bin this value belongs; which is the second bin.
\begin{center}
\begin{tabular}{@{} *5l @{}} \toprule
\textbf{BMI Range} & \textbf{Tally} & \textbf{Frequency} \\\midrule
Underweight: <18.5 is & \tallymarks{10} & 10 \\
Healthyweight: 18.5---25 & \tallymarks{5} & 5 \\
Overweight: 25---30 & \tallymarks{3} & 3 \\
Obese: >30 & \tallymarks{12} & 12 \\\bottomrule
\hline
\end{tabular}
The barplot for this data is achieved in R, with the following code.
\begin{lstlisting}
bloodgroup <- c("O", "A", "A", "O", "O",
"B", "B", "A", "A", "A",
"A", "O", "O", "O", "B",
"O", "O", "A", "O", "A",
"A", "O", "AB", "B", "O",
"AB", "B", "O", "A", "AB")
barplot(prop.table(table(bloodgroup)))
\end{lstlisting}
\end{center}
\end{document}