0

I would like to merge two stacked histograms into one. I need to merge the two graphs into one to represent the percentage difference between the two tools.

Chart 1: Histogram 1

Chart 2: Histogram 2

my code :

library(ggplot2)
library(readxl)
library(dplyr)

data <- read_excel("data.xlsx") 

df <- data %>% filter_all(any_vars(!is.na(.)))

head(df)
df
p1 <- ggplot(df, aes(x = methylation, y = percentage))+
  geom_col(aes(fill = location), width = 0.7)
p1

data2 <- read_excel("data2.xlsx") 

df2 <- data2 %>% filter_all(any_vars(!is.na(.)))

head(df2)
df2
p1 <- ggplot(df2, aes(x = methylation, y = percentage))+
  geom_col(aes(fill = location), width = 0.7)
p1


my data :

  tool         location methylation percentage
  <chr>        <chr>    <chr>            <dbl>
1 methyldackel CHR      CG               5.79 
2 methyldackel MIT      CG               0.440
3 methyldackel CHL      CG               0.240
4 methyldackel CHR      CHG              0.587
5 methyldackel MIT      CHG              0.394
6 methyldackel CHL      CHG              0.259
7 methyldackel CHR      CHH              0.405
8 methyldackel MIT      CHH              0.565
9 methyldackel CHL      CHH              0.339
# A tibble: 9 x 4
  tool      location methylation percentage
  <chr>     <chr>    <chr>            <dbl>
1 megalodon CHR      CG              3.58  
2 megalodon MIT      CG              0.354 
3 megalodon CHL      CG              0.280 
4 megalodon CHR      CHG             0.07  
5 megalodon MIT      CHG             0     
6 megalodon CHL      CHG             0.037 
7 megalodon CHR      CHH             0.0579
8 megalodon MIT      CHH             0.022 
9 megalodon CHL      CHH             0.0242

Like the following example. excel exemple

chemdork123
  • 10,461
  • 1
  • 14
  • 29
  • 1
    Basically you want a stacked and dodged barchart. I just answered a similar question: https://stackoverflow.com/questions/71853717/how-to-combine-two-data-frames-into-one-graph-where-i-will-have-two-stacked-bars. For more options see https://stackoverflow.com/questions/12715635/ggplot2-bar-plot-with-both-stack-and-dodge – stefan Apr 13 '22 at 08:52

0 Answers0