How to choose colors from a predefined set of colors in ggplot2

Suppose I have a dataset, for example:

set.seed(1)
dataset <- data.frame(x = sort(rnorm(100)), 
                      y = sort(rlnorm(100))+1:4, 
                      group=rep(letters[1:4], 25))

I would like to create a graph using ggplot2. Instead of manually selecting colors, I use a predefined set of colors Paired:

ggplot(dataset, aes(x = x, colour = group)) + 
geom_line(aes(y=y)) + 
scale_colour_brewer(palette="Paired")

I get a graph as shown below: data points for groups aand bare located in two shades of blue, while the data points for groups cand dare located in two shades of green.

enter image description here

Suppose now that I would only like to build the data corresponding to the groups cand d, and I would like to use two shades of green. If I just do the following:

ggplot(dataset[dataset$group %in% c("c", "d"),], aes(x = x, colour = group)) + 
geom_line(aes(y=y)) + 
scale_colour_brewer(palette="Paired")

(. ), Paired.

enter image description here

, : , ggplot2

+4
1
require(RColorBrewer)
ggplot(dataset[dataset$group %in% c("c", "d"),], aes(x = x, colour = group)) + 
  geom_line(aes(y=y)) + 
  scale_colour_manual(values = brewer.pal(4, "Paired")[3:4])

enter image description here

, . .

+8

Source: https://habr.com/ru/post/1524621/


All Articles