brc
brc

Reputation: 99

How can I plot just a single response type from two different variables using ggplot?

I have data which represents presence/absence and is recorded as "Y" or "N". I want to plot all the Y responses from two different variables: birds and cars with hours being the explanatory term. I am just having a lot of trouble - my code plots the N which I don't want to do. I'm here for advice on both fixing this problem and better visualizing my data. Repeatable code:

bird <-c("Y", "Y", "N", "Y", "Y", "N")
car <- c("N", "N", "Y", "N", "Y", "Y")
hour <- c(1, 2, 3, 4, 5, 6)
a <- data.frame(hour, bird, car)

ggplot(a) + 
  geom_dotplot(aes(x = car, y = hour, fill = "blue"), 
               binaxis='y', stackdir='center', stackratio=1.5, dotsize=1.2) + 
  geom_dotplot(aes(x = bird, y = hour), 
               binaxis='y', stackdir='center', stackratio=1.5, dotsize=1.2)

I realize this code is amess, open to all suggestions.

Upvotes: 0

Views: 336

Answers (2)

zx8754
zx8754

Reputation: 56189

Convert from wide-to-long then plot:

library(dplyr)
library(tidyr)

pivot_longer(a, cols = 2:3) %>% 
  filter(value == "Y") %>% 
  ggplot(aes(x = value, y = hour, fill = name)) + 
  geom_dotplot(binaxis = "y", stackgroups = TRUE, binwidth = 1,
               binpositions = "all", stackdir = "center") 

enter image description here

Upvotes: 1

det
det

Reputation: 5232

Are you looking for something like this?

a %>% 
  pivot_longer(cols = c(bird, car)) %>% 
  ggplot(aes(hour, name)) + 
  geom_point(aes(shape = value), color = "blue", size = 6, fill = "blue") + 
  scale_shape_manual(values = c("N" = 0, "Y" = 22)) + 
  scale_x_continuous(breaks = sort(a$hour)) +
  theme_minimal()

enter image description here

ggplot2 favores long data format because columns can be mapped to different aesthetics.

Upvotes: 0

Related Questions