0

我正在尝试创建一个自定义 ggplot 主题,如下所示:

library(data.table)
library(tidyverse)


# On definit les couleurs
CouleurTitre = "#006CE5" #Bleu titre
Couleur_QuadrillagePrimaire ="#B9CDE5"
Couleur01 = rgb(000,112,192, maxColorValue = 255) # Bleu
Couleur02 = rgb(255,102,000, maxColorValue = 255) # Orange
Couleur03 = rgb(000,176,080, maxColorValue = 255) # Vert
Couleur04 = rgb(255,204,000, maxColorValue = 255) # Jaune d'or
Couleur05 = rgb(192,000,000, maxColorValue = 255) # Rouge
Couleur06 = rgb(128,128,128, maxColorValue = 255) # Gris
Couleur07 = rgb(000,176,240, maxColorValue = 255) # Bleu ciel
Couleur08 = rgb(102,255,051, maxColorValue = 255) # Vert pomme
Couleur09 = rgb(102,255,051, maxColorValue = 255) # Violet
Couleur10 = rgb(247,150,070, maxColorValue = 255) # Saumon

Palette_Test = sapply((sprintf("Couleur%02d", 1:10)),function(x) get(x),USE.NAMES = FALSE)


# Theme Tresor
Attempted_Theme <- theme_classic()+ 
  theme(plot.title = element_text(size=18, face="bold", hjust=0.5, color=CouleurTitre))+
  theme(panel.grid.major = element_line(colour=Couleur_QuadrillagePrimaire, size=0.5, lineend = "butt"))+ 
  theme(axis.title.x = element_blank(),axis.title.y = element_blank())+
  theme(panel.background = element_rect(colour = "Black",size=1.5)) +
  theme(legend.title=element_blank(),legend.position="bottom")+
  theme(axis.ticks=element_blank())

ggtest <- function(...){
  ggplot(...) +
    Attempted_Theme + 
    scale_colour_manual(values =Palette_Test)
}

使用的数据如下:

MWE <- as.data.table(ggplot2::economics) %>%
  melt(.,id="date") 

因此,我编写了几种颜色,打算在 ggplot2 中用于大多数用途。但我可能没有找到最好的选择scale_coulour_manual,因为它不会改变真正的基本颜色线:

我的第一种颜色是蓝色,我可以通过这种方式获得所需的输出:

ggtest(data=MWE[variable == "pce",],
       aes(x=date,y=value,colour=variable))+
  geom_line(size=1)

结果1

但是,如果我理想情况下希望不指定颜色的标准行为aes也是蓝色,而现在它是黑​​色

ggtest(data=MWE[variable == "pce",],
       aes(x=date,y=value))+
  geom_line(size=1)

在此处输入图像描述 我怎样才能做到这一点?

4

1 回答 1

0

您可以制作自定义scale功能。对于scale_color_manual,您需要颜色的名称与变量的名称相对应。通过这种方式,它更加“不可知”。

#-- Make palette function
names(Palette_Test) <- c("blue", "orange", "green", "gold", "red", "gray", "skyblue",
                         "applegreen", "violet", "salmon")
palette_fun <- function(primary = "blue", other = "orange", direction = 1) {
    stopifnot(primary %in% names(Palette_Test))
    
    function(n) {
        if (n > 6) warning("Color Palette only has 6 colors.")
        
        if (n == 2) {
            other <- if (!other %in% names(Palette_Test)) {
                other
            } else {
                Palette_Test[other]
            }
            color_list <- c(other, Palette_Test[primary])
        } else {
            color_list <- Palette_Test[1:n]
        }
        
        color_list <- unname(unlist(color_list))
        if (direction >= 0) color_list else rev(color_list)
    }
}

#-- Test it
palette_fun()(2)
#> [1] "#FF6600" "#0070C0"

#-- Make scale function for ggplot
scale_color_test <- function(primary = "blue", other = "orange", direction = 1, ...) {
    ggplot2::discrete_scale("color", "test", palette_fun(primary, other, direction))
}

#-- Add it to the ggtest function:
ggtest <- function(...){
    ggplot(...) +
        Attempted_Theme + 
        scale_color_test()
}

#-- Test results with custom palette
MWE <- as.data.table(ggplot2::economics) %>%
    melt(.,id="date") 
ggtest(data=MWE[variable == "pce",],
       aes(x=date,y=value,colour=variable))+
    geom_line(size=1)

在此处输入图像描述

受此博客情节的启发

于 2020-11-24T09:05:50.887 回答