[英]ggplot2 custom theme : set default colour for plotting
I am trying to create a custom ggplot theme as follow :我正在尝试创建一个自定义 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)
}
The data used is the following :使用的数据如下:
MWE <- as.data.table(ggplot2::economics) %>%
melt(.,id="date")
I have therefore coded several colours, that I intend to be used for most purposes in ggplot2.因此,我编写了几种颜色,打算在 ggplot2 中用于大多数目的。 But I might not have found the best option in
scale_coulour_manual
, as it does not change the real basic color lines :但我可能没有在
scale_coulour_manual
找到最佳选择,因为它不会改变真正的基本颜色线:
My first colour is blue, and I can get the desired ouput this way :我的第一种颜色是蓝色,我可以通过这种方式获得所需的输出:
ggtest(data=MWE[variable == "pce",],
aes(x=date,y=value,colour=variable))+
geom_line(size=1)
But if I would ideally like that the standard behaviour without specifying the colour in the aes
would also be this blue, whereas for now it is black但是,如果我理想地喜欢标准行为而不指定
aes
的颜色也将是这种蓝色,而现在它是黑色
ggtest(data=MWE[variable == "pce",],
aes(x=date,y=value))+
geom_line(size=1)
You can make a custom scale
function.您可以制作自定义
scale
功能。 For scale_color_manual
, you'd need the names of the colors to correspond to names to your variables.对于
scale_color_manual
,您需要颜色的名称与变量的名称相对应。 In this way, it's more 'agnostic'.这样,它更“不可知”。
#-- 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)
Inpired by this blog plot受此博客情节的启发
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.