ggplot:创建多个计数的条形图

时间:2018-04-23 15:39:40

标签: r ggplot2

使用具有user_idsnumber_of_visits的数据集,他们访问了特定位置(geo_location)。我正在尝试获得一个条形图,其中y轴反映total_number_of_usersx反映条件,例如:

  • 3次或更多次访问
  • 5次或更多次访问
  • 10次或更多次访问

    user_id, number_of_visits 0001, 12 0002, 3 0003, 1 0004, 34 0005, 11 0006, 8

使用count似乎是一种过度杀伤,因为它创建了逻辑表。由于没有涉及使用dplyr的分组似乎不是一种选择。

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

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

1 个答案:

答案 0 :(得分:2)

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