in R Language by
Scatter plot with groups in R language

1 Answer

0 votes
by

Sometimes, it can be interesting to distinguish the values by a group of data (i.e. factor level data).

ggplot(mtcars, aes(x = mpg, y = drat)) +
    geom_point(aes(color = factor(gear)))

Code Explanation

  • The aes() inside the geom_point() controls the color of the group. The group should be a factor variable. Thus, you convert the variable gear in a factor.
  • Altogether, you have the code aes(color = factor(gear)) that change the color of the dots.
...