Plotting the value of all column names for each row

Viewed 62

Suppose I have the following dataframe

observations <- data.frame( "Individuals" = c("Individual1", "Individual2", "Individual3"),
"PresentDay1" = c("0","1","1"),
"PresentDay2" = c("O","1","1"))

What I want to do is to have individuals on the y axis, and then have "PresentDay1" and "PresentDay2" be my x-axis ticks, and then, I have a point either black or white depending on whether individual was present (1) or not present (0). on a given day.

I have tried so many things on ggplot, but cant seem to figure out how to do this.

Thanks a lot!

1 Answers

To achieve your desired result reshape your data to long or tidy format using e.g. tidyr::pivor_longer:

library(ggplot2)
library(tidyr)

observations <- observations %>% 
  pivot_longer(-Individuals, names_to = "name", values_to = "value")

ggplot(observations, aes(name, Individuals, color = value)) +
  geom_point() +
  scale_color_manual(values = c("0" = "white", "1" = "black"))

Related