I have a table that looks like this
tibble (person = c ("Paul", "Paul", "Paul", "Sarah", "Sarah", "Sarah", "Alex", "Alex", "Alex"),
salary = c(8, 10, 3, 6, 6, 6, 1, 1, 3))
how would I build a barplot with two sets of bars side-by-side that shows:
I wouldn't advise trying to do this in ggplot directly. Rather, you should precompute your summaries and plot those:
library(tidyverse)
df <- tibble(person = c ("Paul", "Paul", "Paul", "Sarah", "Sarah", "Sarah", "Alex", "Alex", "Alex"),
salary = c(8, 10, 3, 6, 6, 6, 1, 1, 3))
salary_counts <- df |>
summarize(
total_salary = n(),
high_salary = sum(salary > 5),
.by = person
) |>
pivot_longer(cols = c(total_salary, high_salary), names_to = 'count_type', values_to = 'count')
salary_plot <- salary_counts |>
ggplot(data = _, aes(x = person, y = count, fill = count_type)) +
geom_col(position = 'dodge')
print(salary_plot)