anyone could help me to change the color of points (using geom_point) in R?
I need to set, for example, different colors for points above and below 3 standard deviation of the dataset.
The plot is following:
anyone could help me to change the color of points (using geom_point) in R?
I need to set, for example, different colors for points above and below 3 standard deviation of the dataset.
The plot is following:
Given you didn't give a minimal example, I've used iris dataset to show you how I would do it.
Package
library(dplyr)
library(ggplot2)
Initially, let's find the mean and SD.
# Find mean and SD
iris %>%
summarise(mean_Sepal.Width = mean(Sepal.Width),
sd_Sepal.Width = sd(Sepal.Width))
> mean_Sepal.Width sd_Sepal.Width
>1 3.057333 0.4358663
Now, I would create a new column with mutate
and code if each observation is above or below 3SD WITH case_when
.
Finally, put color = color_group
(the new variable we created) inside aes()
, and you are good to go.
iris %>%
mutate(color_group = case_when(Sepal.Width >= 3.06+3*.436 ~ "above 3SD",
Sepal.Width <= 3.06-3*.436 ~ "below 3SD",
T ~ "around the mean")) %>%
ggplot(aes(x = Sepal.Length, y = Sepal.Width, color = color_group)) +
geom_point()
To automatize the process you can just calculate the mean and SD on the fly inside mutate. Both solutions give the same output.
iris %>%
mutate(color_group = case_when(Sepal.Width >= mean(Sepal.Width)+3*sd(Sepal.Width) ~ "above 3SD",
Sepal.Width <= mean(Sepal.Width)-3*sd(Sepal.Width) ~ "below 3SD",
T ~ "around the mean")) %>%
as_tibble() %>%
ggplot(aes(x = Sepal.Length, y = Sepal.Width, color = color_group)) +
geom_point()
In this case, we don't have many outliers, so we don't have many colors. But if we have more outliers, you will see the different colors there.