我的自定义调色板无法接受 - 全灰色

问题描述 投票:0回答:1

这就是我正在用 3 种颜色来表达图表的方法。

    # 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))

r ggplot2 color-palette
1个回答
0
投票

scale_color_manual
而不是填充。

library(tidyverse)

df <- tibble(
  Month = 1:12,
  avg_Pb = 1:12,
  Sample_Type = rep(c("2020 Urban", "2020 Suburban", "2020 Rural"), 4)
)

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

# Plot the data with custom color palette
ggplot(df, 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
  theme_minimal() + # Change theme to remove grey background
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

创建于 2024-03-27,使用 reprex v2.1.0

© www.soinside.com 2019 - 2024. All rights reserved.