Reputation: 21
# A tibble: 6 × 7
No Region District `Name of School` Male Female Total
<chr> <fct> <fct> <chr> <dbl> <dbl> <dbl>
1 1 South East Gaborone Bokamoso CJSS 545 516 1061
2 2 South East Gaborone Marang CJSS 550 567 1117
3 3 South East Gaborone Nanogang CJSS 493 558 1051
4 4 South East Gaborone Maikano CJSS 440 388 828
5 5 South East Gaborone Motswedi CJSS 443 475 918
6 6 South East Gaborone Gaborone West 415 401 816
i want to declare and add a new categorical variable Gender to my data. the gender variable should depend on the values of males and females already provided in the dataset. The point is to plot a grouped bar chart where Region is divided into gender categories in the x axis
# A tibble: 6 × 7
No Region District `Name of School` Male Female Total
<chr> <fct> <fct> <chr> <dbl> <dbl> <dbl>
1 1 South East Gaborone Bokamoso CJSS 545 516 1061
2 2 South East Gaborone Marang CJSS 550 567 1117
3 3 South East Gaborone Nanogang CJSS 493 558 1051
4 4 South East Gaborone Maikano CJSS 440 388 828
5 5 South East Gaborone Motswedi CJSS 443 475 918
6 6 South East Gaborone Gaborone West 415 401 816
Upvotes: 2
Views: 62
Reputation: 7979
Adding a basic approach in base R:
X = df[, c("Name.of.School", "Male", "Female")]
X[-1] |>
t() |>
`colnames<-`(X$Name.of.School) |>
barplot(beside = TRUE, col = c("lightblue", "red2"), legend = c("M", "F"))
Data
df = data.frame(
No = c("1", "2", "3", "4", "5", "6"),
Region = factor(rep("South East", 6)), District = factor(rep("Gaborone", 6)),
`Name of School` = c("Bokamoso CJSS", "Marang CJSS", "Nanogang CJSS",
"Maikano CJSS", "Motswedi CJSS", "Gaborone West"),
Male = c(545, 550, 493, 440, 443, 415), Female = c(516, 567, 558, 388, 475, 401),
Total = c(1061, 1117, 1051, 828, 918, 816))
Upvotes: 1
Reputation: 26238
You need to learn tidy
data principles first
library(tidyverse)
# Create the dataframe df
df <- tibble(
No = c("1", "2", "3", "4", "5", "6"),
Region = factor(rep("South East", 6)),
District = factor(rep("Gaborone", 6)),
`Name of School` = c("Bokamoso CJSS", "Marang CJSS", "Nanogang CJSS", "Maikano CJSS", "Motswedi CJSS", "Gaborone West"),
Male = c(545, 550, 493, 440, 443, 415),
Female = c(516, 567, 558, 388, 475, 401),
Total = c(1061, 1117, 1051, 828, 918, 816)
)
df %>%
select(-Total) %>%
pivot_longer(c(Male, Female), names_to = "gender", values_to = "schools") %>%
ggplot(aes(`Name of School`, y = schools, fill = gender)) +
geom_col(position = "dodge")
Created on 2024-03-07 with reprex v2.0.2
Upvotes: 0