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
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))
And the same with barplot
from base R:
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))
barplot(t(df[,-1]), beside=T,
names.arg=df$Department,
legend.text=names(df[,-1]))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.