based on the following code there are two legend and two labels in x and y , I need the graph to have one common legend season = dry and wet , Common x and y labels , have you an idea to fix the problem
# Install the required package if not already installed
# install.packages("ggplot2")
# Load the ggplot2 package
library(ggplot2)
library(readr)
# Create sample data
season <- rep(c("Dry", "Wet"), each = 4)
site <- rep(c("Site A", "Site B", "Site C", "Site D"), times = 2)
temperature <- c(20, 22, 18, 21, 19, 20, 21, 22)
turbidity <- c(5, 7, 4, 6, 8, 6, 5, 4)
electric_conductivity <- c(10, 8, 9, 11, 7, 9, 10, 11)
# Create a data frame
df <- data.frame(season, site, temperature, turbidity, electric_conductivity)
df <- tidyr::pivot_longer(df, cols = c(temperature, turbidity, electric_conductivity), names_to = "Parameter", values_to = "Value")
View(df)
# Convert the "Season" variable to a factor with correct ordering
df$season <- factor(df$season, levels = c("Dry", "Wet"))
data_wet<- df[df$season == "Wet", ]
data_dry<- df[df$season == "Dry", ]
graph_a <- ggplot(data_wet, aes(x = site, y = Value, fill = season)) +
geom_col(position = position_dodge()) +
labs(x = "Site", y = "Value") +
facet_wrap(~ Parameter, ncol = 3, scales = "free_x", strip.position = "bottom") +
scale_fill_manual(values = c("Dry" = "orange", "Wet" = "green"), name = "Season") +
theme_bw() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
graph_b <- ggplot(data_dry, aes(x = site, y = Value, fill = season)) +
geom_col(position = position_dodge()) +
labs(x = "Site", y = "Value") +
facet_wrap(~ Parameter, ncol = 3, scales = "free_x", strip.position = "bottom") +
scale_fill_manual(values = c("Dry" = "orange", "Wet" = "green"), name = "Season") +
theme_bw() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
# Display the graph
library(gridExtra)
grid.arrange(graph_a, graph_b, nrow = 1)