0

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.

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

2 Comments

This worked really well. If you only wanted to plot one column (just "a") and you filter for "Y" then it drops extralimital hour values so the y-axis no longer has all 24 hours. Any suggestions for correcting this?
You can use scale_y_continuous /scale_y_discrete. Something like this stackoverflow.com/questions/41917761/… might work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.