1

I'm trying to make a scatter plot of points with a continuous color scale. I'm setting up my own scale though, since my data is not linear and most of my data points have same color (and can't be discerned) with an automatic color scale.

My code right now looks like this:

cut.values <- c(4, 5, 5.5, 6.5, 7, 7.5)
iris$cuts <- cut(iris$Sepal.Length, cut.values)
library(dplyr)
pick <- function(condition){
  function(d) d %>% filter_(condition)
}
ggplot(iris, aes(x=Petal.Length, y=Petal.Width, color = cuts), label = Sepal.Length) +
  geom_point(data = pick(~Species == "setosa"), size = 5, shape = 16, show.legend = F) +
  geom_point(data = pick(~Species == "versicolor"), size = 5, shape = 17, show.legend = F) +
  scale_color_brewer("", palette = "Spectral") +
  theme_bw() 

Then I decided to change the shapes to 21 and 22 to make black borders for each point to make them stand out, and did this:

ggplot(iris, aes(x=Petal.Length, y=Petal.Width, fill = cuts), label = Sepal.Length) +
  geom_point(data = pick(~Species == "setosa"), size = 5, shape = 21, show.legend = F) +
  geom_point(data = pick(~Species == "versicolor"), size = 5, shape = 22, show.legend = F) +
  scale_color_brewer("", palette = "Spectral") +
  theme_bw() 

However, this seems to change the colors of points, even though I'm using the same "Spectral" palette. Is some part of the command overwriting the color scheme I'm trying to use? How can I keep the same color scheme while using shape 21 and 22?

(Because I'm defining my own color scale using cuts, I'm using the codes from the first answer of this question (Create discrete color bar with varying interval widths and no spacing between legend levels), and since this uses palette with scale_color_brewer command, I have to stick to using scale_color_brewer to keep the color scheme consistent between data points and the color bar I'm creating with the code. )

Jen
  • 331
  • 2
  • 11
  • Hi @Jen, which library is the pick() function in? – M.Viking Jun 29 '19 at 20:13
  • @M.Viking Sorry my bad! It's a filter function I added, I edited the question. – Jen Jun 29 '19 at 20:17
  • I see it. Nice. In the interim I changed the call to `...data = subset(iris, Species == "setosa")...`. I think I've got the answer. ... double checking. – M.Viking Jun 29 '19 at 20:21

1 Answers1

1

scale_color_brewer() defaults to the aesthetic "colour". If you assign to "fill" then your plot color palette will be correct!

ggplot(iris, aes(x=Petal.Length, y=Petal.Width, fill = cuts), label = Sepal.Length) +
  geom_point(data = pick(~Species == "setosa"), size = 5, shape = 21, show.legend = F) +
  geom_point(data = pick(~Species == "versicolor"), size = 5, shape = 22, show.legend = F) +
  scale_color_brewer(palette = "Spectral", aesthetics = "fill") +
  theme_bw() 
M.Viking
  • 5,067
  • 4
  • 17
  • 33