add-semi-colons
add-semi-colons

Reputation: 18830

ggplot: Create a bar plot of multiple counts

Working with a data set that has user_ids and number_of_visits they have visited a specific location (geo_location). I am trying to get a bar plot where y axis reflecting total_number_of_users and x reflecting condition such as:

using count seems like an overkill since it creates logical table. Since no grouping involve use of dplyr seems like not an option.

count(df, number_of_visits >= 3, number_of_visits >=5)

df %>% number_of_visits(count3 = number_of_visits >= 3)

Upvotes: 0

Views: 311

Answers (1)

LetEpsilonBeLessThanZero
LetEpsilonBeLessThanZero

Reputation: 2403

library(tidyverse)

df = data.frame(user_id = c("0001", "0002", "0003", "0004", "0005", "0006"),
                number_of_visits = c(12,3,1,34,11,8))

df = df %>%
  mutate(threeOrMore = ifelse(number_of_visits >= 3, ">=3", "<3")) %>%
  mutate(fiveOrMore = ifelse(number_of_visits >= 5, ">=5", "<5")) %>%
  mutate(tenOrMore = ifelse(number_of_visits >= 10, ">=10", "<10"))

plot = ggplot(df) +
  geom_bar(aes(threeOrMore))

print(plot)

enter image description here

Upvotes: 2

Related Questions