stats_noob
stats_noob

Reputation: 5935

Overriding Colors in Ggplot2

I made the following graph in R (from: 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

Normally, I would have changed the colors like this (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"))

But in the code I posted above, I do not know where exactly I can use the "scale_fill_manual" command to replace "yellow" with "red".

Thank you!

Upvotes: 1

Views: 156

Answers (1)

langtang
langtang

Reputation: 24845

You are using five colors from the "Accent" palette from RColorBrewer. You can find the actual colors, like this:

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

Instead of using scale_colour_brewer(palette="Accent"), you can instead define stat_colors (replacing the "yellow" color with "red", and adding names using names(stat_colors) like this:

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

Then, in your plot use:

scale_color_manual(values= stat_colors)

p9

Full Code:

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

Upvotes: 2

Related Questions