Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

make color of one category grey in ggplot?

Tags:

r

ggplot2

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)
like image 437
curious Avatar asked Oct 23 '25 03:10

curious


1 Answers

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

like image 183
Jessica Burnett Avatar answered Oct 25 '25 16:10

Jessica Burnett



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!