Ggplot - Display barplots by group

Hi everyone,

Here my small dataset :

report <- matrix(0, ncol=3, nrow = 5)
colnames(report) <- c("Month", "Var1","Var2")
report[,1] <- 1:5
report[,2] <- c(5,7,6,1,2)
report[,3] <- c(10,11,10,9,9)
report <- as.data.frame(report)
> report
 Month  Var1  Var2
     1     5    10
     2     7    11
     3     6    10
     4     1     9
     5     2     9

My goal is to display for each month (xlab = Month), the "value" of my 2 variables.
I don't have factory/levels to count or anyting else, just representing the bar of the values.

For example, for January, display 5 (var1 in red color) and 10 (var2 in blue color), then February in red/blue etc.. And have a legend for the 2 variables (fill option i suppose).

ggplot

Something in this appareance, but i don't have any factor to count.. just values

Is this what you mean?

library(tidyverse)

report <- data.frame(
       Month = c(1, 2, 3, 4, 5),
        Var1 = c(5, 7, 6, 1, 2),
        Var2 = c(10, 11, 10, 9, 9)
)

report %>% 
    pivot_longer(cols = starts_with("Var"), names_to = "Var", values_to = "Value") %>% 
    ggplot(aes(x = Month, y = Value, fill = Var)) +
    geom_col(position = "dodge")

Created on 2020-05-28 by the reprex package (v0.3.0)

1 Like

Yes exactly what i mean.

Thanks a lot

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.