how to customize Bar graph

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)

image

Have you considered facets to handle one of the variables?

library(ggplot2)

# 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)
# Create the graph
ggplot(df, aes(x = Parameter, y = Value, fill = season)) +
  geom_col(position = "dodge")  +
  facet_wrap(~site) +
  labs(x = "Site", y = "Temperature") +
  scale_fill_manual(values = c("Dry" = "blue", "Wet" = "green")) +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5))

Created on 2023-06-24 with reprex v2.0.2

1 Like

I updated my question Could you help me

Is this kind of what you had in mind?

# to simplify facet labels make a single varible combing season and parameter
df$facet_var <- paste0(df$season, df$Parameter)

ggplot(df,
       aes(x = site,
           y = Value,
           fill = season)) +
  geom_col() +
  labs(x = "Site", y = "Value") +
  facet_wrap("facet_var", 
             ncol = 6, 
             strip.position = "bottom",
             # remove the "Dry" and "Wet" parts of the label, since that is
             # indicated by the fill colour
             labeller = as_labeller(~gsub("Dry|Wet", "", .))) +
  scale_fill_manual(values = c("Dry" = "orange", "Wet" = "green"), 
                    name = "Season") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

Exactly, I want slight modification , could you arrange parameter one label, for example Temperature could appear one times , in this case appears two time

Do you mean something like this?

ggplot(df,
       aes(x = site,
           y = Value,
           fill = season)) +
  geom_col() +
  labs(x = "Site", y = "Value") +
  facet_grid(season~Parameter) +
  scale_fill_manual(values = c("Dry" = "orange", "Wet" = "green"), 
                    name = "Season") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        # remove the dry/wet facet labels since we don't need them
        strip.text.y = element_blank())

or something like this?

ggplot(df,
       aes(x = site,
           y = Value,
           fill = season)) +
  geom_col(position = position_dodge()) +
  labs(x = "Site", y = "Value") +
  facet_wrap("Parameter") +
  scale_fill_manual(values = c("Dry" = "orange", "Wet" = "green"), 
                    name = "Season") +
  theme_bw()

or something else?

my intention is to plot graph like this

It looks like you need something like the ggh4x package. I've never used it so can't help you, but there appear to be examples on the GitHub page.

There are also some related Stack Overflow questions
https://stackoverflow.com/questions/40316169/nested-facets-in-ggplot2-spanning-groups
https://stackoverflow.com/questions/40732543/seeking-workaround-for-gtable-add-grob-code-broken-by-ggplot-2-2-0/40838827#40838827

You may be able to get the desired effect with something like the patchwork package but I'm not sure.

thank you for your help

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.