1

I'm doing a scatterplot, have x and y and another variable (n=20) that I want to be sorted by colour. R chooses these colours automatically in a spectrum, so the resulting plot isn't easy to read, especially for the colourblind. The palettes I could find only have up to 8-10 colours) and I need more for the numbers of values in my variable.

I was able to add black stroke around the data points, but I'm trying to find a way to change fill. And preferably assign it a colour of my choosing.

+geom_point(aes(fill=genus), pch=21, size=5, colour="black", alpha=0.8)

Hope this makes sense, I'm new to R.

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
hdx
  • 11
  • 1

1 Answers1

0

There are multiple way to achieve what you want in R. One is mentioned in the link in Tung's comment.

Another way to approach this is create a color varialbe and assign values to it and create a vector of colors with names is corresponded with the variables you use with scale_color_manual

# Sample dataframe
x <- data.frame(x = seq(1, 10, by = 1),
        y = seq(1, 10, by = 1),
        color = c("color 1", "color 2", "color 3", "color 4", "color 5",
                  "color 6", "color 7", "color 8", "color 9", "color 10"))
# color map
color_map <- c("red", "green", "yellow", "orange", "black",
               "grey50", "blue", "pink", "cyan", "purple")
names(color_map) <- x[["color"]]

library(ggplot2)

ggplot() + geom_point(data = x,
                      mapping = aes(x = x, y = y, size = 5, color = color)) +
  scale_color_manual(values = color_map)

enter image description here

# Another example with less colors and using Hex code instead of color name
x <- data.frame(x = seq(1, 10, by = 1),
        y = seq(1, 10, by = 1),
        color = c("color 1", "color 2", "color 3", "color 4", "color 5",
                  "color 1", "color 2", "color 3", "color 4", "color 5"))
#              Red        Green      Yellow    Orange    Black
color_map <- c("#FF0000", "#00FF00", "FFFF00", "FFA500", "000000")
names(color_map) <- c("color 1", "color 2", "color 3", "color 4", "color 5")

library(ggplot2)

ggplot() + geom_point(data = x,
                      mapping = aes(x = x, y = y, size = 5, color = color)) +
  scale_color_manual(values = color_map)

enter image description here

Sinh Nguyen
  • 4,277
  • 3
  • 18
  • 26