getting error in geom_point with color argument as non-aesthtic

Hi All,
I am novice to ggplot topic. I am getting the error while executing the below ggplot script. Please help me on how to fix it. Below is my ggplot script.

library("dplyr")
library("ggplot2")
humans <- filter(starwars, species == "Human")
humans <- mutate(humans, BMI = mass/(height/100)^2)
ggplot(data = humans , mapping = aes(x = mass, y = height)) + geom_point(color = "BMI


")

If you want to color the point using the BMI value, try

ggplot(data = humans , mapping = aes(x = mass, y = height, color = BMI)) + geom_point()

Thanks @FJCC for the reply . I want to have color in geom_point() only as I have multiple geom and want to have different color each geom. In the above script, i have mentioned only one geom.
Coming to my issue: I see the error only when i add it outside of aesthetic as mentioned in the above script , but when i write the color inside the aesthetic as shown below, I am not getting.
ggplot(data = humans, mapping = aes(x = mass, y = height)) + geom_point(aes(color = "BMI"), size = 4)

Can someone please explain what exactly aesthetic will do present inside the geom_point. Aesthetic inside the plot will be inherited to geom but here it doesn't makes sense to write the script inside the geom.
Thanks.

aesthetics define a mapping between a charting feature , like colour, and something else; that something else can most usefully be a symbol, representing a column in your data; i.e. if your data has a BMI column, then each entry can get its own colour.
However, BMI is a symbol and "BMI" is a literal string of text that says BMI, and therefore only once colour can be associated with a single unchanging text value of "BMI"
do you want the BMI symbol or BMI as text ?

If you compare the two plots below, you can see that only the points change color in the second plot. Is that what you want?

library("dplyr")

library("ggplot2")

humans <- filter(starwars, species == "Human")
humans <- mutate(humans, BMI = mass/(height/100)^2)

ggplot(data = humans , mapping = aes(x = mass, y = height, color = BMI)) + 
  geom_line() +
  geom_point(size = 3)
#> Warning: Removed 13 rows containing missing values or values outside the scale range
#> (`geom_line()`).
#> Warning: Removed 13 rows containing missing values or values outside the scale range
#> (`geom_point()`).


ggplot(data = humans , mapping = aes(x = mass, y = height)) + 
  geom_line() +
  geom_point(aes(color = BMI), size = 3)
#> Warning: Removed 13 rows containing missing values or values outside the scale range
#> (`geom_line()`).
#> Removed 13 rows containing missing values or values outside the scale range
#> (`geom_point()`).

Created on 2024-07-05 with reprex v2.0.2

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.