Home > Software design >  Generate a barplot with multiple facets
Generate a barplot with multiple facets

Time:03-03

I have a database with the following structure:

enter image description here

I want to create two bar plots, with two facets (Sin and T), in the X-axis the time and in the Y-axis the different A, B, C, D, and E columns (columns can be stacked or not).

How can I do that?

Thanks in advance.

CodePudding user response:

Something like this?

library(tidyverse)

df %>% 
  pivot_longer(
    -c(COND, Time)
  ) %>% 
  ggplot(aes(x=factor(Time), y = value, fill=name))  
  geom_col(position = position_dodge()) 
  facet_wrap(.~COND) 
  xlab("Time")

data:

df <- structure(list(COND = c("Sin", "Sin", "Sin", "Sin", "T", "T", 
"T", "T"), Time = c(0L, 1L, 6L, 8L, 0L, 1L, 6L, 8L), A = c(54L, 
202L, 155L, 202L, 244L, 321L, 149L, 155L), B = c(1536L, 732L, 
2577L, 1321L, 1744L, 1952L, 3857L, 1780L), C = c(34018L, 80476L, 
4173L, 119L, 33851L, 56320L, 2494L, 696L), D = c(10458L, 33655L, 
357L, 452L, 10869L, 30667L, 1839L, 3315L), E = c(3500L, 1904L, 
0L, 0L, 3035L, 2839L, 0L, 0L)), class = "data.frame", row.names = c(NA, 
-8L))

enter image description here

  • Related