My Custom Color Palette just won't take - all grey

29 views Asked by At

This is what I am working with to express the graph in 3 set colors.

    # Define custom colors
    color_palette <- c("2020 Urban" = "darkred", "2020 Suburban" = "gold", "2020 Rural" = "darkblue")

    # Plot the data with custom color palette
    ggplot(average_Pb_monthly, aes(x = Month, y = avg_Pb, group = Sample_Type, color = Sample_Type)) +
      geom_line(size = 1) +
      geom_point(size = 3) +
      labs(title = "Average Monthly Concentration of Pb in Honey Samples (2020) by Land Use",
           x = "Month",
           y = "Average Pb Concentration",
           color = "Land Use and Time") + # Label for legend
      scale_fill_manual(values = color_palette) + # Remove name argument
      theme_minimal() + # Change theme to remove grey background
      theme(axis.text.x = element_text(angle = 45, hjust = 1))

1

There are 1 answers

2
Carl On
library(tidyverse)

df <- tibble(
  Month = rep(seq(ymd("2024-01-01"), ymd("2024-12-01"), by = "3 months"), 3),
  avg_Pb = c(1:4, 1, 3, 5, 7, 1, 4, 7, 10),
  Sample_Type = c(rep("2020 Urban", 4), rep("2020 Suburban", 4), rep("2020 Rural", 4))
)

color_palette <- c("2020 Urban" = "darkred", "2020 Suburban" = "gold", "2020 Rural" = "darkblue")

# Plot the data with custom color palette
df |> 
  # mutate(Sample_Type = factor(Sample_Type)) |> 
  ggplot(aes(x = Month, y = avg_Pb, group = Sample_Type, color = Sample_Type)) +
  geom_line(size = 1) +
  geom_point(size = 3) +
  labs(
    title = "Average Monthly Concentration of Pb in Honey Samples (2020) by Land Use",
    x = "Month",
    y = "Average Pb Concentration",
    color = "Land Use and Time"
  ) + # Label for legend
  scale_color_manual(values = color_palette) + # Remove name argument
  scale_x_date(date_breaks = "1 month", date_labels = "%b") + # Remove name argument
  theme_minimal() + # Change theme to remove grey background
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

Created on 2024-03-27 with reprex v2.1.0