How to Change the Color Value of Just One Value in Ggplot2's Scale_Fill_Brewer

How do I change the color value of just one value in ggplot2's scale_fill_brewer?

The package RColorBrewer contains the palettes and you can use the function brewer.pal to return a colour palette of your choice.

For example, a sequential blue palette of 5 colours:

library(RColorBrewer)
my.cols <- brewer.pal(5, "Blues")
my.cols

[1] "#EFF3FF" "#BDD7E7" "#6BAED6" "#3182BD" "#08519C"

You can get a list of valid palette names in the ?brewer.pal help files. These names correspond with the names at the ColorBrewer website.

You can now use or modify the results and pass these to ggplot using the scale_manual_fill as you suggested:

my.cols[1] <- "#000000"

library(ggplot2)
df <- data.frame(x=1:5, type=1:5)
ggplot(df, aes(x=x, fill=factor(type))) +
geom_bar(binwidth=1)+
scale_fill_manual(values = my.cols)

Sample Image

ggplot2 : Color One Category Separately

based on this post you can get ggplot colors with this function:

gg_color_hue <- function(n) {
hues = seq(15, 375, length = n + 1)
hcl(h = hues, l = 65, c = 100)[1:n]
}

So in your case :

cols = gg_color_hue(length(levels(iris$Species)))

Then you reprogramm the color you want to change and use it for your plot:

cols[levels(iris$Species)=="virginica"]="gray"
p2 <- p1 + scale_color_manual(values=cols)

Sample Image

ggplot change fill colour without losing colour gradient

You want to adjust the scale, in particular the continuous scale of fill colors, hence the function scale_fill_continuous().

ggplot(data = melt, aes(x = variable, y = value, fill = value)) +
geom_bar(width = .8, stat = "identity") +
labs(x = "Samples", y = "Expression", title = "Gapdh") +
theme(plot.title = element_text(face = "bold", size = 12),
axis.text.x = element_text(angle = 45, hjust = 1, size = 10),
axis.text.y = element_text(size = 10)) +
scale_fill_continuous(low = "firebrick4", high = "firebrick1")

(I slightly modified your plotting code: you can call theme once with multiple arguments, and I find labs nicer than a bunch of individual labeling calls.)

One other option is to use the palettes from the RColorBrewer package (which are incorporated into ggplot2). The scale_fill_brewer() scale if for discrete color scales, you can "distill" them into continuous scales with scale_fill_distiller(). For example

scale_fill_distiller(type = "seq", palette = "Reds")

To see all of the available scales, run RColorBrewer::display.brewer.all().

How to set fixed continuous colour values in ggplot2

Do I understand this correctly? You have two plots, where the values of the color scale are being mapped to different colors on different plots because the plots don't have the same values in them.

library("ggplot2")
library("RColorBrewer")
ggplot(subset(mtcars, am==0), aes(x=wt, y=mpg, colour=carb)) +
geom_point(size=6)

Sample Image

ggplot(subset(mtcars, am==1), aes(x=wt, y=mpg, colour=carb)) + 
geom_point(size=6)

Sample Image

In the top one, dark blue is 1 and light blue is 4, while in the bottom one, dark blue is (still) 1, but light blue is now 8.

You can fix the ends of the color bar by giving a limits argument to the scale; it should cover the whole range that the data can take in any of the plots. Also, you can assign this scale to a variable and add that to all the plots (to reduce redundant code so that the definition is only in one place and not in every plot).

myPalette <- colorRampPalette(rev(brewer.pal(11, "Spectral")))
sc <- scale_colour_gradientn(colours = myPalette(100), limits=c(1, 8))

ggplot(subset(mtcars, am==0), aes(x=wt, y=mpg, colour=carb)) +
geom_point(size=6) + sc

Sample Image

ggplot(subset(mtcars, am==1), aes(x=wt, y=mpg, colour=carb)) + 
geom_point(size=6) + sc

Sample Image

How to recycle colours in a colorbrewer palette using line symbols

Made data frame containing 20 levels (as letters).

df<-data.frame(group=rep(c(LETTERS[1:20]),each=5),x=rep(1:5,times=20),y=1:100)

You can use scale_colour_manual() to set colors for lines - in example I used five SET1 and repeated them four times (total number is 20). Then to set shapes added geom_point() and scale_shape_manual() and five different shapes and repeated each of them four times (total number again is 20).

library(RColorBrewer)
ggplot(df,aes(x,y,colour=group))+geom_line()+geom_point(aes(shape=group),size=5)+
scale_colour_manual(values=rep(brewer.pal(5,"Set1"),times=4))+
scale_shape_manual(values=rep(c(15,16,17,18,19),each=5))

Sample Image

How to break background color for continuous variable in ggplot2?

scale_fill_brewer is for a discrete scale, for a continuous scale based on the same palette you can use scale_fill_distiller. Here is an example (with color instead of fill - switch back to fill for your use case) on the same 0 to 50 scale as your data.

x = seq(0, 50, by = 2)
dd = data.frame(x = x, y = x)

gridExtra::grid.arrange(g + scale_color_distiller(palette = "RdYlGn"),
g + scale_color_distiller(palette = "PiYG"),
g + scale_color_distiller(palette = "YlGn"))

Sample Image

You can use RColorBrewer::display.brewer.all() to see all the RColorBrewer palette options.

One other option, since your data seems to be concentrated near 0 would be to log or square root transform for the scale. Square root will be more natural since your data contains 0, but this will help spread out the lower colors and compress the higher colors. Just add trans = "sqrt" to any scale_fill function. For a more extreme transformation (maybe needed since your data goes up to 800) you could log(RMKP + 1), which is implemented with trans = "log1p".

Here is the same plots from above but with trans = "sqrt" added to the scales:

Sample Image



Related Topics



Leave a reply



Submit