Search code examples
rdplyrtidyversetidyr

how to plot a combined histogram with different datasets in r


I have 2 datasets and I am trying to find a better way of visualizing my results. Can you please help me to plot the 2 histograms on 1 histogram so it is easier to compare?

data1:

count <- c('10', '15', '16','16','29')
drug <- c('ibuprofen','aleve','tylenol','claritin', 'ozempic')

data2:

count <- c('150', '1000', '15','1','234','452','692')
drug <- c('ibuprofen','aleve','tylenol','xanax','gilenya','humira','tums')

This is what I have tried but it is not showing a histogram as I want.

ggplot() + 
  geom_point(data = data1, aes(x=count, y=reorder(drug, count), color = "red")) + 
  geom_point(data = data2, aes(x=count, y=reorder(drug, count)))

Can you please help me produce a histogram that has both histograms so I can visualize the drugs side by side, add the count to the bars and also have the datasets color be black and gray. Thank you!


Solution

  • Here is an alternative solution:

    library(dplyr)
    library(tidyr)
    library(ggplot2)
    
    data1 <- data.frame(count = c(10, 15, 16, 16, 29), 
                        drug = c("ibuprofen", "aleve", "tylenol", "claritin", "ozempic"))
    data2 <- data.frame(count = c(150, 1000, 15, 1, 234, 452, 692), 
                        drug = c("ibuprofen", "aleve", "tylenol", "xanax", "gilenya", "humira", "tums"))
    
    combined_data <- data1 %>% 
      mutate(data = "data1") %>% 
      bind_rows(data2 %>% 
                  mutate(data = "data2")) %>%
      complete(drug, data, fill = list(count = 0))  
    
    
    ggplot(combined_data, aes(x = drug, y = count, fill = data)) +
      geom_col(position = position_dodge(width = 0.9)) +
      geom_text(aes(label = count), vjust = -0.5, position = position_dodge(width = 0.9)) +
      scale_fill_manual(values = c("steelblue3", "red3"))+
      theme_minimal()+
      theme(text = element_text(size = 12))
    
    

    enter image description here