curious
curious

Reputation: 730

make color of one category grey in ggplot?

Trying to make this one group "unknown" grey and have a grey key in the legend

data <- fread(data_path)
bgrd <- data[data$pop == 'Unknown',]
ref <- data[data$pop != 'Unknown',]
p <- ggplot(data, aes(x=PC1, y=PC2, fill=pop))
p <- p + geom_point(data=bgrd,  color='grey')
p <- p +  geom_point(data=ref, color='black', pch=21)

gives me this: enter image description here

Close, but "unknown" is pink not grey in legend. What am I missing?

more of a reproducible examples:

bgrd <- iris[iris$Species == 'virginica',]
ref <- iris[iris$Species != 'virginica',]
p <- ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, fill=Species))
p <- p + geom_point(data=bgrd, color='grey')
p <- p + geom_point(data=ref, color='black', pch=21)

Upvotes: 4

Views: 2069

Answers (1)

Jessica Burnett
Jessica Burnett

Reputation: 420

Here is an example solution (based on the accepted answer for this question) for automatically assigning the color to a reference factor level. This example, however, does not adjust the PCH. This also assumes the grouping variable is a factor.

library(RColorBrewer)
library(ggplot2)
data(iris)


ref = "virginica"
myColors <- brewer.pal(length(levels(iris$Species)),"Set1")
names(myColors) <- levels(iris$Species)
myColors[names(myColors)==ref] <- "grey"
colScale <- scale_colour_manual(name = "grp",values = myColors)

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species))+
  geom_point()+
  colScale

enter image description here

Upvotes: 3

Related Questions