Manually Setting Group Colors For Ggplot2

Manually setting group colors for ggplot2

You can associate each of your groups with a colour, then pass to the function:

group.colors <- c(A = "#333BFF", B = "#CC6600", C ="#9633FF", D = "#E2FF33", E = "#E3DB71")

simplePlot <- function(DT, tit)
ggplot(DT ,aes(x=Name, y=Value, fill=Group)) +
geom_bar(stat="identity") + xlab("") + ggtitle(tit) +
#Specify colours
scale_fill_manual(values=group.colors)

Then using your plots:

grid.arrange(ncol=2,  simplePlot(DT1, tit="Plot 1"), 
simplePlot(DT2, tit="Plot 2"))

Sample Image

I think the issue with your approach was that the colours weren't named, so scale_fill_manual() can't assoicate them. Compare:

ColorsDT <-  data.table(Group=LETTERS[1:5], Color=c("#333BFF", "#CC6600", "#9633FF", "#E2FF33", "#E3DB71"), key="Group")
ColorsDT
# Group Color
#1: A #333BFF
#2: B #CC6600
#3: C #9633FF
#4: D #E2FF33
#5: E #E3DB71

with:

ColorsDT.name <-  data.table(A = "#333BFF", B = "#CC6600", C = "#9633FF", D = "#E2FF33", E =  "#E3DB71")
ColorsDT.name
# A B C D E
# 1: #333BFF #CC6600 #9633FF #E2FF33 #E3DB71

Custom colors for groups using ggplot2

You need to map color to Species variable then use scale_color_manual (not fill)

require(MASS)
require(ggplot2)

data("iris")
my.data <- iris
model <- lda(formula = Species ~ ., data = my.data)
data.lda.values <- predict(model)
plot.data <- data.frame(X = data.lda.values$x[, 1], Y = data.lda.values$x[, 2], Species = my.data$Species)

my_colors <- c("yellow", "magenta", "cyan")
p <- ggplot(data = plot.data, aes(x = X, y = Y, color = Species)) +
geom_point() +
scale_color_manual(values = my_colors) +
theme_bw()
p

Sample Image

Probably better to use Set2 (colorblind safe, print friendly) from ColorBrewer

p <- ggplot(data = plot.data, aes(x = X, y = Y, color = Species)) +
geom_point() +
scale_color_brewer(palette = "Set2") +
theme_bw()
p

Sample Image

Created on 2019-03-10 by the reprex package (v0.2.1.9000)

How to manually specify color fill for two different grouping variables using ggplot?

A simple solution to achieve your desired result would be to map the car class on fill and the number of cylinders on alpha:

library(ggplot2)

ggplot(mpg) +
geom_bar(aes(x = class, fill = class, alpha = factor(cyl)),
position = position_dodge(preserve = 'single')) +
scale_alpha_manual(values = c(.25, .5, .75, 1))

Sample Image

geom line: set manual color for variables that are grouped

Try this:

library(ggplot2)

ggplot(data_sample, aes(x=BP, y=value,
group =variable,color=variable)) +
scale_color_manual(values= c("overall_diff_cases"="#9633FF" ,
"overall_diff_controls"="#E2FF33")) + geom_line(size=1.2)

Output:

Sample Image

With scale_color_manual() you can set the colors you want.

Set colors of Factors in R

If you don't label the colors manually then ggplot will use its default scheme, which changes as the number of factors changes. One option is to set up separate label schemes with equivalent colors for overlapping types:

library(tidyverse)

label_colors_4 <- c("Aorta" = "red", "Coronary" = "yellow", "Pulmonary" = "brown", "Vein" = "green")
label_colors_6 <- c(label_colors_4, "Kidney" = "blue", "Stomach" = "purple")

pca %>%
ggplot(aes(x = PC1, y = PC2, color = type)) +
geom_point(size = 5) +
scale_color_manual(values = label_colors_4)

pca2 %>%
ggplot(aes(x = PC1, y = PC2, color = type2)) +
geom_point(size = 5) +
scale_color_manual(values = label_colors_6)


Related Topics



Leave a reply



Submit