[Tex/LaTex] Scatter Plot with Marginal Dot Diagram

pgfplots

I want to create a scatter plot that has marginal dot diagrams. I can find plenty of help on how to construct a scatter plot with marginal histograms, but not with marginal dot diagrams. Something like this:

enter image description here

This is the code I've used to do the scatter plot:

\documentclass{article} 
\usepackage{tikz,pgfplots} 

\begin{document} 
\begin{tikzpicture} 
\begin{axis}[% 
    scatter/classes={% 
        a={mark=o,draw=black}}] 
\addplot[scatter,only marks,% 
    scatter src=explicit symbolic]% 
    table[meta=label] { x y label 1 18.95 a 2 19 a 3 17.95 a 3 15.54 a 4 14 a 5 12.95 a 6 8.94 a 8 7.49 a 9 6 a 11 3.99 a }; 
\end{axis} 
\end{tikzpicture} 
\end{document}

Any help would be appreciated.

Best Answer

First, I discovered that dotplots or stripcharts are not easily used for marginal plots. (My R programming skills are not up to that). So as I used to remind my students, use histograms for discrete data and density plots for continuous data.

The following code generate both versions. (I did not resolve the binwidth issue and did not lookup how to suppress the warning)

\documentclass{article}

\begin{document}
<<echo=FALSE,warning=FALSE,error=FALSE,message=FALSE>>=
# Load
library("ggplot2")
library("ggExtra")

# Create some data
set.seed(1234)
x <- c(rnorm(50, mean = -1), rnorm(50, mean = 1.5))
y <- c(rnorm(50, mean = 1), rnorm(50, mean = 1.7))
df3 <- data.frame(x, y)

# Scatter plot of x and y variables and color by groups
sp2 <- ggplot(df3,aes(x, y),main="Plot Title",xlab="x label",ylab="y label") + geom_point()
@

\begin{center}
Title for Marginal Density Plot
\end{center}

<<echo=FALSE,warning=FALSE,error=FALSE,message=FALSE>>=
# Marginal density plot
 ggMarginal(sp2 + theme_gray())
@
\newpage
\begin{center}
Title for Marginal Histogram
\end{center}

<<echo=FALSE,warning=FALSE,error=FALSE,message=FALSE>>=
# Marginal histogram plot
ggMarginal(sp2 + theme_gray(), type = "histogram",
           fill = "steelblue", col = "darkblue")
@
\end{document} 

enter image description here enter image description here