覆盖 Ggplot2 中的颜色

Overriding Colors in Ggplot2

我用R做了下面的图(来自:https://t-redactyl.io/blog/2016/03/creating-plots-in-r-using-ggplot2-part-9-function-plots.html

library(grid)

p9 <- ggplot(data.frame(x = c(0, 1)), aes(x = x)) +

    stat_function(fun = dnorm, args = list(0.2, 0.08),
                  aes(colour = "Mean = 0.2, Standard Deviation = 0.08"), size = 1.5) +

  stat_function(fun = dnorm, args = list(0.4, 0.1),
                  aes(colour = "Mean = 0.04, Standard Deviation = 0.01"), size = 1.5) +

    stat_function(fun = dnorm, args = list(0.3, 0.05),
                  aes(colour = "Mean = 0.3, Standard Deviation = 0.05"), size = 1.5) +

 stat_function(fun = dnorm, args = list(0.7, 0.07),
                  aes(colour = "Mean = 0.7, Standard Deviation = 0.07"), size = 1.5) + 

stat_function(fun = dnorm, args = list(0.5, 0.06),
                  aes(colour = "Mean = 0.5, Standard Deviation = 0.06"), size = 1.5) +

    scale_x_continuous(name = "Probability",
                       breaks = seq(0, 1, 0.2),
                       limits=c(0, 1)) +
    scale_y_continuous(name = "Frequency") +
    ggtitle("Normal function curves of probabilities") +
    scale_colour_brewer(palette="Accent") +
    labs(colour = "Groups") +
    theme_bw() +
    theme(axis.line = element_line(size=1, colour = "black"),
          panel.grid.major = element_line(colour = "#d3d3d3"),
          panel.grid.minor = element_blank(),
          panel.border = element_blank(), panel.background = element_blank(),
          plot.title = element_text(size = 14, family = "Tahoma", face = "bold"),
          text=element_text(family="Tahoma"),
          axis.text.x=element_text(colour="black", size = 9),
          axis.text.y=element_text(colour="black", size = 9))

p9

通常情况下,我会像这样更改颜色 (Changing color of density plots in ggplot2):

require(ggplot2)
set.seed(2)
data <- rbind( data.frame(type="a", lr=rnorm(100)), data.frame(type="b", lr=rnorm(100,.5,1.2)))
m <- ggplot(data, aes(x=lr)) 
m <- m + geom_density(aes(fill=factor(type)), size=2, alpha=.4) 
m + scale_fill_manual( values = c("red","blue"))

但是在我上面发布的代码中,我不知道在哪里可以使用“scale_fill_manual”命令将“黄色”替换为“红色”。

谢谢!

您正在使用 RColorBrewer 中“强调”调色板中的五种颜色。您可以找到实际颜色,如下所示:

RColorBrewer::brewer.pal(5,"Accent")
"#7FC97F" "#BEAED4" "#FDC086" "#FFFF99" "#386CB0"

您可以定义 stat_colors,而不是使用 scale_colour_brewer(palette="Accent")(将“黄色”颜色替换为“红色”,并使用 names(stat_colors) 添加名称,如下所示:

stat_colors=c("#7FC97F", "#BEAED4", "#FDC086", "red", "#386CB0")
names(stat_colors) =  c("Mean = 0.04, Standard Deviation = 0.01",
                        "Mean = 0.2, Standard Deviation = 0.08",
                        "Mean = 0.3, Standard Deviation = 0.05",
                        "Mean = 0.5, Standard Deviation = 0.06",
                        "Mean = 0.7, Standard Deviation = 0.07"
)

然后,在你的情节中使用:

scale_color_manual(values= stat_colors)

完整代码:

stat_colors=c("#7FC97F", "#BEAED4", "#FDC086", "red", "#386CB0")
names(stat_colors) =  c("Mean = 0.04, Standard Deviation = 0.01",
                        "Mean = 0.2, Standard Deviation = 0.08",
                        "Mean = 0.3, Standard Deviation = 0.05",
                        "Mean = 0.5, Standard Deviation = 0.06",
                        "Mean = 0.7, Standard Deviation = 0.07"
)

p9 <- ggplot(data.frame(x = c(0, 1)), aes(x = x)) +
  
  stat_function(fun = dnorm, args = list(0.2, 0.08),
                aes(colour = "Mean = 0.2, Standard Deviation = 0.08"), size = 1.5) +
  
  stat_function(fun = dnorm, args = list(0.4, 0.1),
                aes(colour = "Mean = 0.04, Standard Deviation = 0.01"), size = 1.5) +
  
  stat_function(fun = dnorm, args = list(0.3, 0.05),
                aes(colour = "Mean = 0.3, Standard Deviation = 0.05"), size = 1.5) +
  
  stat_function(fun = dnorm, args = list(0.7, 0.07),
                aes(colour = "Mean = 0.7, Standard Deviation = 0.07"), size = 1.5) + 
  
  stat_function(fun = dnorm, args = list(0.5, 0.06),
                aes(colour = "Mean = 0.5, Standard Deviation = 0.06"), size = 1.5) +
  
  scale_x_continuous(name = "Probability",
                     breaks = seq(0, 1, 0.2),
                     limits=c(0, 1)) +
  scale_y_continuous(name = "Frequency") +
  scale_color_manual(values= stat_colors) +
  ggtitle("Normal function curves of probabilities") +
  #scale_colour_brewer(palette="Accent") +
  labs(colour = "Groups") +
  theme_bw() +
  theme(axis.line = element_line(size=1, colour = "black"),
        panel.grid.major = element_line(colour = "#d3d3d3"),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(), panel.background = element_blank(),
        plot.title = element_text(size = 14, family = "Tahoma", face = "bold"),
        text=element_text(family="Tahoma"),
        axis.text.x=element_text(colour="black", size = 9),
        axis.text.y=element_text(colour="black", size = 9))
p9