R: Change geom_text labels to color based on group with RColorBrewer Package

Solution 1:

So what you are doing is saying to ggplot: "Please colour these dots according to the 'paired' value". As there is no 'paired' value it is assigning the same colour to each point as it can't differentiate. See example below on mtcars data:

library(tidyverse)

mtcars %>%
  ggplot(
    aes(
      x = wt,
      y = mpg,
      colour = "Paired"
    )
  ) + 
  geom_point()

This will give this plot: Bad assignment

Instead, put into the colour parameter of the aes asthetic the value you want to colour by. In my case I am going to factor the carb variable but you want to put in the value of your numbers.

Then you want to use the scale_color_brewer function to change your colour pallette. As your data is qualitative you want to add the parameter type = "qual". Here is an example using the mtcars data set:

library(tidyverse)

mtcars %>%
  ggplot(
    aes(
      x = wt,
      y = mpg,
      colour = factor(carb)
    )
  ) + 
  geom_point() +
  scale_color_brewer(type = "qual")

Which results in this picture, where the dots are coloured according to their carb value:

Good assignment

You may want to experiment with leaving out the type parameter also.

I don't have your data, so I don't know if it would work, but I think your code should end up looking something like this:

p <-ggplot(df, aes(x=Score,y=Day)) +
        geom_text(aes(label = sample, color=sample)) +
        scale_color_brewer(type = "qual")