Hi
I'm trying to do a simple histogram with the biomass of a specific insect over the course of a year. Despite the order of the data in my spreadsheet, R is putting the x-axis in alphabetical order.
I've searched for some solutions here but no solution so far.
Code and Plot below:
library(ggplot2)
library(forcats)
library(dplyr)
#x axis change test 2
# data
df <- data.frame(
Month = Triplectides_biomass$Month,
Biomass = Triplectides_biomass$Biomass,
Area = Triplectides_biomass$Area
)
# Mean and standard error
df_summary <- df %>%
group_by(Month, Area) %>%
summarise(
Mean_Biomass = mean(Biomass),
SE_Biomass = sd(Biomass) / sqrt(n())
)
# Plot
ggplot(df_summary, aes(x = Month, y = Mean_Biomass, fill = Area)) +
geom_bar(stat = "identity", position = position_dodge()) +
geom_errorbar(
aes(ymin = Mean_Biomass - 1.96 * SE_Biomass,
ymax = Mean_Biomass + 1.96 * SE_Biomass),
width = 0.2,
position = position_dodge(width = 0.9) # Center the error bars
) +
labs(x = "Months", y = "Biomass (0.001g)", title = "Average Biomass of Triplectides") +
theme_minimal()
your dates are not data objects, they're text strings, so they can't be sorted chronologically. Convert them to dates, then use scale_x_date(). Something like this:
I believe a small tweak to your df, similar @mduvekot's suggestion, from your original code should work. Here is the tweak along with the rest of your original code:
df <- data.frame(
Month = Triplectides_biomass$Month,
Biomass = Triplectides_biomass$Biomass,
Area = Triplectides_biomass$Area
) |>
# add a 'date' column that expresses 'Month' as date
# (the value "%b %y" captures your strings of the form "Apr 23")
# (%b stands for "a[b]brevaition" and %y stands for two-digit [y]ear)
mutate(date = parse_date(Month, format = "%b %y")) |>
# use 'date' column to give "Month" column chronological order
mutate(Month = fct_reorder(Month, date))
# Mean and standard error
df_summary <- df %>%
group_by(Month, Area) %>%
summarise(
Mean_Biomass = mean(Biomass),
SE_Biomass = sd(Biomass) / sqrt(n())
)
# Plot
ggplot(df_summary, aes(x = Month, y = Mean_Biomass, fill = Area)) +
geom_bar(stat = "identity", position = position_dodge()) +
geom_errorbar(
aes(ymin = Mean_Biomass - 1.96 * SE_Biomass,
ymax = Mean_Biomass + 1.96 * SE_Biomass),
width = 0.2,
position = position_dodge(width = 0.9) # Center the error bars
) +
labs(x = "Months", y = "Biomass (0.001g)", title = "Average Biomass of Triplectides") +
theme_minimal()