many histograms in the same plot

This is my dataframe:

df <- data.frame(

  confidence = c(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1),
  A = c(44615, 4767, 2035, 834, 374, 186, 86, 35, 9, 4, 4),
  B = c(133995, 3059, 1152, 404, 164, 70, 33, 18, 2, 0, 0),
  C = c(37998, 2844, 1127, 472, 241, 115, 64, 33, 11, 3, 3)
)

I want to make a plot in which I put in X bar the confidence scores (ranging from 0 to 1 with an increment of 0.1) and in every confidence point I want to put the 3 histograms of my 3 samples (A, B, C)

This is what I want to get

Thanks in advance! Have a great day.

Upvotes: 0

Views: 33

Answers (1)

Quinten
Quinten

Reputation: 41603

You should first convert your dataframe to a longer format using pivot_longer. After that, you could map your x value (should be not numerical) on the x aesthetic with values on y and your categorical to fill. To make sure you have grouped bars you could use position with "dodge" like this:

library(tidyverse)
df %>%
  pivot_longer(cols = A:C) %>%
  ggplot(aes(x = factor(confidence), y = value, fill = name)) +
  geom_col(position = "dodge") +
  labs(x = "confidence")

Created on 2023-03-29 with reprex v2.0.2

Upvotes: 1

Related Questions