callin
callin

Reputation: 49

How can I plot points as colors in R?

I have a CSV file that has 50 rows and three columns. I want to plot variable 1 against variable 2, while coloring each point depending on variable 3 (which is only one of 4 values).

I can successfully do this with the following code, however the points are all shades of blue (i.e. a gradient) instead of 4 unique colors. As such, it's hard to distinguish between the points and would be much more useful if the 4 values of variable three were red, green, yellow, etc.

This is really basic but I can't figure out why it would default to a gradient and not 4 random colors?

  sample<- read_csv("Sample Data.csv")
  ggplot(data=sample, mapping = aes(x=var_1, y=var_2))+
  geom_point(mapping = aes(color=var_3))

Upvotes: 0

Views: 47

Answers (1)

akrun
akrun

Reputation: 887118

Perhaps, can change the 'var_3' to factor

library(dplyr)
library(ggplot2)
sample %>%
      mutate(var_3 = factor(var_3)) %>%
      ggplot(aes(x = var_1, y = var_2, color = var_3)) +
            geom_point()

Upvotes: 4

Related Questions