Huan Lu
Huan Lu

Reputation: 15

ggplot side by side bar plot with two datasets

I have these 2 vectors that could be reformed to be 2 datasets:

V1 = c(0.1920, 0.0002, 0.0000, 0.3020, 0.0010, 0.0000, 0.0310, 0.2458, 0.0436, 0.0228, 0.3160, 0.1108)
V2 = c(0.1160, 0.0000, 0.0000, 0.4092, 0.0000, 0.0000, 0.9658, 0.0836, 0.0092, 0.9746, 0.2312, 0.0284)

I used for loop and par and base::barplot to draw like this: enter image description here

I need to do a single bar plot for corresponding value in both matrix cell, and then plot all the barplot into a matrix with same number of rows and columns.

Is there anyone knows how to do this with ggplot? Thank you for you great help in advance!

Upvotes: 0

Views: 391

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389265

To use ggplot2 try :

library(tidyverse)

data.frame(V1, V2) %>%
  mutate(facet_col = row_number()) %>%
  pivot_longer(cols = -facet_col) %>%
  ggplot() + aes(name, value, fill = name) +
  geom_col() + facet_wrap(~facet_col, nrow = 3, dir="v") + 
  scale_fill_manual(values = c('red', 'green')) + 
  guides(fill = FALSE) + 
  theme_classic()

enter image description here

Upvotes: 2

Related Questions