brc
brc

Reputation: 99

How to plot binary data via a dotplot?

I have a dataset that includes presence absence of a variable based on hour and day. I'd like to plot presence (i.e., just "Y") by hour and day with day on the x and hour on the y axis. I'm just not good at this stuff and it's giving me trouble. See example data:

df <- data.frame("day"= c(1, 1, 1, 2, 2, 2, 3, 3, 3),"hour" = c(1, 2, 3, 1, 2, 2, 1, 2, 3),"a" = c("Y", "Y", "N", "N", "N", "Y", "N", "N", "Y"), "b" = c("N", "N", "Y", "N", "Y", "Y", "Y", "Y", "Y"))

Would love any suggestions.

Upvotes: 0

Views: 488

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389235

You can try this approach :

Get the data in long format, filter only 'Y' values and plot scatterplot.

library(tidyverse)

df %>%
  pivot_longer(cols = a:b) %>%
  filter(value == 'Y') %>%
  mutate(across(c(day, hour), factor)) %>%
  ggplot() + aes(day, hour, color = name) + geom_point()

Upvotes: 1

Related Questions