Home > Net >  Plotting bar plots with R
Plotting bar plots with R

Time:11-22

Am trying to create an R dodged geom bar with this data but am not getting a plot that i need

  Department              Male Female
  <chr>                  <int>  <int>
1 "Admin Offices"            3      6
2 "Executive Office"         0      1
3 "IT/IS"                   28     22
4 "Production       "       83    126
5 "Sales"                   16     15
6 "Software Engineering"     5      6

What i tried seems pretty wrong so anyone can help

CodePudding user response:

I can only guess what you need actually. However The result could be this:

library(dplyr)
library(tidyr)
library(ggplot2)    
df<-data.frame(Department = c("Admin Offices", 
                              "Executive Office" ,    
                              "IT/IS",                
                              "Production",           
                              "Sales",                
                              "Software Engineering" ),
               Male = c(3, 0, 28, 83, 16, 5),
               Female = c(6, 1, 22, 126, 15, 6))

df %>% pivot_longer(cols = c("Male", "Female")) %>% 
  transmute(Department, Gender = as.factor(name), Value = value) %>% 
  ggplot()   
  geom_bar(aes(x = Department, y = Value, fill = Gender), stat = "identity", position = position_dodge(0.9))

enter image description here

  •  Tags:  
  • r
  • Related