# How to plot binary (presence/absence - 1/0) data against continuous variables [duplicate]

I am trying to plot presence/absence (1/0) of a sample species against various environmental variables.

I have put presence/absence on the y-axis and the environmental variable (in this case barometric pressure) on the x axis, however the resulting plot looks terrible.

Is there a better way to do this? I thought of plotting presence/absence against the frequency of the environmental variable, would this be possible?

• Two histograms or two stripplots or two quantile plots? Jun 17 '18 at 15:23
• Typical approach is to visualize the distribution within factor level e.g. by boxplots and then compare. Jun 17 '18 at 18:04
• Those points look like they only come at discrete levels, or were rounded. Is that true? Are there multiple data points that are stacked on top of each other, or are represented by a single plotted point in this figure? Jun 18 '18 at 1:23

If I understood the question correctly - you might want to use a "conditional density plot".

Such a plot provides a smoothed overview of how a categorical variable changes across various levels of continuous numerical variable.

Example

For a real-world example here is the distribution of Sepal Width across 3 different species in the iris dataset:

cdplot(Species ~ Sepal.Width, data=iris)


Interpretation

These plots represent smoothed proportions of each category within various levels of the continuous variable. In order to interpret them you should look across at the x-axis and see how the different proportions for each category (represented by different colors) change with the different values of the numerical variable.

For example consider the picture above: it is quite easy to see that when sepal width reaches 3.5 or above you are most likely dealing with setosa type of flower. At sepal width 2.0 the versicolor dominates. And at 3.0 there are about 20% setosa, 35% versicolor and 45% virginica (judging by eye according to the scales on the y-axis on the right.)

For another discussion about interpretation of such plots consider reading answers in this question: Interpretation of conditional density plots

Of course in your case you would have 2 categories on the y-axis. So the final picture would look closer to this example:

set.seed(14)

presence <- factor(rbinom(20, 1, 0.5))
presence
[1] 0 1 1 1 1 1 1 0 0 0 1 0 0 1 1 1 0 1 1 1
Levels: 0 1

pressure <- runif(20, 1000, 1035)
pressure
[1] 1012.282 1014.687 1021.619 1024.159 1026.247 1021.663 1013.469
1018.317 1024.054 1002.747 1028.396 1004.806 1033.906 1022.898
1033.127 1004.378 1019.386 1016.432 1030.160 1021.567

cdplot(presence ~ pressure)


Interpretation stays the same, except you will be dealing with a binary categorical variable. In this particular case the plot would suggest that the presence (1, light grey area) is increasing with increasing values of pressure (x-axis).

Much better to turn your plot around: put presence on the horizontal and pressure on the vertical axis. Then plot pressure as a dotplot. If overplotting is an issue, jitter the dots horizontally.

If you want to emphasize the distribution and/or summary statistics, overlay a boxplot or a beanplot.

Of course you can plot these horizontally, too, if you insist, but for just two groups, one usually sees the vertical versions below.

library(beanplot)

set.seed(1)
n_per_group <- 30
pressure <- data.frame(F=rnorm(n_per_group,1000,20),T=rnorm(n_per_group,1000,20))

boxplot(pressure,outline=FALSE,ylim=range(pressure),xaxt="n",col="gray",
xlab="Presence",ylab="Pressure (hPa)")
axis(1,c(1,2),c("FALSE","TRUE"))
points(as.vector(cbind(runif(n_per_group,.7,1.3),runif(n_per_group,1.7,2.3))),
unlist(pressure),pch=19)

beanplot(pressure,what=c(0,1,0,0),col="gray",xaxt="n",xlab="Presence",ylab="Pressure (hPa)")
axis(1,c(1,2),c("FALSE","TRUE"))
points(as.vector(cbind(runif(n_per_group,.7,1.3),runif(n_per_group,1.7,2.3))),
unlist(pressure),pch=19)

• I always do these horizontally in this case, just to maintain a closer analogy. Jun 18 '18 at 1:25