Here is the code I have:
library("ggplot2") # Loading the req1uired packages
library("ggpubr")
rm(list=ls())
TeamsHalf <- read.table(file= "../data/TeamsHalf.txt", sep = ";", header = T) # Loading the data table
index = TeamsHalf$Half == 2 #Making a column with each teams' rank at the end of the season
TeamsHalf$FinalRank[index] <- TeamsHalf$Rank[index]
TeamsHalf$divID <- as.factor(TeamsHalf$divID) # changing the order that facet_wrap orders the divisions, so that "East" is on the right-hand side of the plot
TeamsHalf$divID = names(TeamsHalf$divID) = c("Western ", "Eastern")
levels(TeamsHalf$divID) <- c("Eastern ","Western")
TeamsHalf$Half <- as.factor(TeamsHalf$Half) # Changing the order of the stacked bars so that the 1st half of the season is below 2nd half.
levels(TeamsHalf$Half) <- c("2","1")
g.TeamsHalfNL <- ggplot(data = TeamsHalf[TeamsHalf$lgID == "NL",], # Extracting data for the Eastern division
aes (y = W, x = reorder(teamID, -FinalRank, na.rm = T), fill = factor(Half)))+ # setting the name of the variables, reordering x in order of rank at the end of the season, and setting the colorss for the different halfs.
geom_col(width = 0.8, position = "stack")+ # Setting the width of cols, and stacking them on top of each other.
scale_colour_manual(values = c( "#002a5b", "#ffc78a"), aesthetics = c("color", "fill"))+ # changing the colors of the columns.
labs(x = "National League", y = "Wins", fill = "Season Half")+ # changing x and y labels, and the legend title.
theme(
panel.background = element_rect(fill = NA), # setting a white background
panel.grid.major.y = element_line(colour = "grey50", size = 1/4), # Changing the size and color of the horizontal grid lines
panel.grid.major.x = element_blank())+ # removing the vertical gridlines
facet_wrap(.~divID, scales = "free") # splitting the plot into two graphs, one for each league
g.TeamsHalfAL <- ggplot(data = TeamsHalf[TeamsHalf$lgID == "AL",], # Extracting data for the Western division
aes (y = W , x = reorder(teamID, -FinalRank, na.rm = T), fill = factor(Half)))+ # setting the name of the variables, reordering x in order of rank at the end of the season, and setting the colorss for the different halfs.
geom_col(width = 0.8, position = "stack")+ # Setting the width of cols, and stacking them on top of each other.
scale_colour_manual(values = c( "#002a5b", "#ffc78a"), aesthetics = c("color", "fill"))+ # changing the colors of the columns.
labs(x = "American League", y="Wins")+ # changing x and y labels, and the legend title.
theme(
panel.background = element_rect(fill = NA), # setting a white background
panel.grid.major.y = element_line(colour = "grey50", size = 1/4), # # Changing the size and color of the horizontal grid lines
panel.grid.major.x = element_blank())+ # removing the vertical gridlines
facet_wrap(.~divID, scales = "free") # splitting the plot into two graphs, one for each league
FinalGraph <- ggarrange(g.TeamsHalfAL, g.TeamsHalfNL, ncol = 1, nrow = 2, # combinging the two graphs, stacking them vertically
common.legend = T, legend = "bottom", align = "hv") # creating a shared legend, moving the legend, and aligning the graphs.
annotate_figure(FinalGraph, top = text_grob("\nOverview of the 1981 baseball season for AL and NL", face = "italic"), # Here, I set the text for title, note, and figure nr. the Title is indented with a "\n" to adhere to the APA style.
bottom = text_grob("Note. The figure shows the number of wins for each team in the Western\n and Eastern division of the AL and NL. The teams are sorted by their final rank\n at the end of the season.", size = 8),
fig.lab = "Figure 1")
I want the labels of the facet_wrap to say "Eastern" and "Western" instead of E and W, so I use the names() function to change them in line 10. However, when I do this, the facet_wrap makes two plots, but with all the same teams, instead of splitting them into western and Eastern. When I don't change the name of the variables, and they remain named "E" and "W", it works perfectly fine. Does anyone know what the issue is here?